I am having a problem with using reserved keywords of Java in Kotlin. As not all the reserved keywords are available in Kotlin, Kotlin compiler can not (does not?) detect the reserved keywords that are only in Java.
For example, take the example of default
. It is a Java reserved keyword. That means you can not do as follows.
public class UserJava {
public String default;
}
However, it is NOT a reserved keyword in Kotlin, so you can use it as follows
class UserKotlin {
var default: String? = null
}
For someone who does not know Java, it would be difficult to know that they are reserved keyword. As Kotlin compiler does not give you an error, or any kind of warning that you are using a reserved keyword, it may cause some problems later, which you would not expect.
One of the problems that it may encounter is during AnnotationProcessing which are the libraries that you include in your gradle with kapt
or annotationProcessor
. As Kotlin uses Java compiler when you, for example, try to get the fields of UserKotlin
class, you would get an empty list as Java compiler does NOT "understand" the field default
and ignores it (skips it?).
Real life example would be if you use Room library and have a entity as below
@Entity
class User{
@PrimaryKey
var id: Int = 0
var default: String? = null
}
Room annotation processor will generate an SQLite create query of
CREATE TABLE IF NOT EXISTS `User` (`id` INTEGER NOT NULL, PRIMARY KEY(`id`))
So, it basically skipped the default
field.
Is there any way that Kotlin gives warning when I try to use Java reserved keywords? Or is a bug in Kotlin which is yet to be fixed? Or is there any documentation that mentioned this problem.
If I know Java, then I might be relucted to use such keywords as I know that these are reserved in Java. But what if I don't know Java and can't figure out what the problem is?