In kotlin, if we use a when block on an enum while exhausting all the enum values but without adding an else branch, we get this warning: "Enum argument can be null in Java, but exhaustive when contains no null branch".
Android Studio then suggest to add the suppress flag #Suppress("WHEN_ENUM_CAN_BE_NULL_IN_JAVA") either to the statement, function, class or file scope.
Is there any way to add this to the project scope ? Since my project is 100% Kotlin I don't need this warning and would like to disable it globally.
Related
I have a requirement in my Android project where I need to validate an argument's annotation.
For eg in below function , I need to ensure if block has the required annotation otherwise throw compilation error or lint error
fun doSomething(block : () -> Unit){}
Is there some annotation processing that could help me here
Or can lint check help me
Thank you.
To keep your code cleaner I would suggest to use lint checks, however there is a way to achieve this programatically.
You can use reflection on parameters.
val isAnnotated: Boolean = block::check.hasAnnotation<SomeAnnotation>()
if (!isAnnotated) throw Exception()
The following code shows up "Property must be initialized or be abstract" error.I understand that I can use lateinit in such cases but I wanted to know the reason behind the restriction .
class Student{
val s:String
}
In the case of a non-nullable property like in your example, the reason is necessity. Java implicitly gives member variables a value of null. In Kotlin, a non-nullable property cannot have a value of null, so you have to give it an instance of something to be the starting value.
But even if you declared it as nullable String?, Kotlin will require you to specify the starting value. Kotlin avoids making implicit assumptions about the intent of your code. Kotlin's design goals are to make code more readable and robust. The designers have done research on common causes of bugs in other languages, and have made Kotlin more restrictive in areas that have been frequent sources.
in my class extend ConstraintLayout. I have defined val-type variables in the class.
After running the program in the debug mode, I see that all the variables are zero.
Why is this going to happen?
When you define variables with initializers, the Kotlin compiler will generate a constructor which will execute all the initializers in sequence. Before the initializers are executed, the values of the corresponding fields will be 0, and this is the state that is shown on your screenshot.
For values such as IMEOPTIONS_ACTION_DONE, which seem to be constants, you shouldn't use regular class properties. Instead, you should put them into a companion object or on the top level of a file.
Android Lint picks up the string constant in this code sample as a spelling error on "dWQGSCDx". According to the docs I should use #SupressLint("Typos") to suppress it but that doesn't achieve that. I see others have suggested using #SuppressWarnings but that is not working either.
/**
* Constants.kt
*/
import android.annotation.SuppressLint
#SuppressLint("Typos")
#SuppressWarnings("SpellCheckingInspection")
const val SOME_STRING_VALUE = "...dWQGSCDx..."
Note this is a file-scoped global constant, it is not inside a class so an annotation cannot be placed on a containing class.
How do I suppress spell-checking of this constant definition without disabling spellcheck entirely and without adding the "mispelt" text to the dictionary?
In Kotlin you can suppress this warning using #Suppress instead of #SuppressWarnings with the following annotation
#Suppress("SpellCheckingInspection")
Sometimes lint produces false warnins if field is operated via bunch of annotations. For a common example:
#SerializedName("id") #Expose private Integer id;
field id is assigned only via gson.fromJson(). Such operation is invisible for lint, thus it throws warning variable id is never assigned
So I want to configure lint, in this particular case, to ignore checks if field is ever assigned, if it is annotated with #SearializedName (please dont suggest raw #SuppressWarnings("unused") which has to be set manualy for every field and will block checks, if field is ever used)
Gson serialises fields using reflection, which takes place at runtime. That UnusedAssignment inspection finds variables which meet any of the following criteria:
the variable never gets read after assignment
the value is always overwritten with another assignment before the next variable read
the variable initializer is redundant (for one of the above two reasons) - the variable is never used.
Your only option to get rid of warnings for this specific inspection is to suppress them, either by updating your lint.xml, or via #SuppressWarnings("unused"). If you really don't want to do either of those things, then it may be possible to write a custom Lint inspection that ignores fields with a #SerializedName annotation.
The disadvantage of this approach is if you want to use #SerializedName in a class that isn't serialised by Gson, you will no longer get a warning if a variable is unused. It is also much more complicated than adding #SuppressWarnings("unused") to the top of your GSON model classes.