

jsr305.jar) a compile dependency for the library consumers. ) value is When.ALWAYS, the annotated type is treated as non-null When.MAYBE and When.NEVER denote a nullable type and When.UNKNOWN forces the type to be platform one.Ī library can be compiled against the JSR-305 annotations, but there's no need to make the annotations artifact (e.g. The annotation defined in JSR-305 is supported for denoting nullability of Java types. For example, if has both TYPE_USE and METHOD targets, the Java method signature String f() becomes fun f(): Array! in Kotlin. Note: If a nullability annotation supports other targets that are applicable to a type in addition to the TYPE_USE target, then TYPE_USE takes priority.
#ADD INTEGER TO LIST JAVA CONSTRUCTOR CODE#
Pass the -Xtype-enhancement-improvements-strict-mode compiler option to report errors in Kotlin code that uses nullability which deviates from the nullability annotations from Java. The feature requires that the nullability annotations support the TYPE_USE target ( supports this in version 15 and above).

So passing nullable type as a type argument or type parameter produces a warning.Īnnotating type arguments and type parameters works with the Java 8 target or higher. Type argumentsĬonsider these annotations on a Java declaration: You can annotate the type arguments and type parameters of generic types to provide nullability information for them as well.Īll examples in the section use JetBrains nullability annotations from the package. Annotating type arguments and type parameters
#ADD INTEGER TO LIST JAVA CONSTRUCTOR FULL#
See the full list of supported nullability annotations in the Kotlin compiler source code.

Use the compiler option In the argument, specify the fully qualified nullability annotations package and one of these report levels: You can specify whether the compiler reports a nullability mismatch based on the information from specific types of nullability annotations. JSR-305 ( javax.annotation, more details below)įindBugs ( .findbugs.annotations) JetBrains ( and from the package)Īndroid ( and ) The compiler supports several flavors of nullability annotations, including:

Java types that have nullability annotations are represented not as platform types, but as actual nullable or non-null Kotlin types. (Mutable)Collection! means "Java collection of T may be mutable or not, may be nullable or not",Īrray! means "Java array of T (or a subtype of T), nullable or not" Nevertheless, the compiler and IDE need to display them sometimes (for example, in error messages or parameter info), so there is a mnemonic notation for them: Notation for platform typesĪs mentioned above, platform types can't be mentioned explicitly in the program, so there's no syntax for them in the language. Overall, the compiler does its best to prevent nulls from propagating far through the program although sometimes this is impossible to eliminate entirely, because of generics. Assertions are also emitted when you pass platform values to Kotlin functions expecting non-null values and in other cases. This prevents Kotlin's non-null variables from holding nulls. If you choose a non-null type, the compiler will emit an assertion upon assignment. Val notNull: String = item // allowed, may fail at runtime Val nullable: String? = item // allowed, always works
