Stack Overflow link: https://stackoverflow.com/questions/63789139/custom-null-safety-annotation-is-invalid-in-kotlin
0
Infomation about custom Null-Safety annotation is from Kotlin official:
https://kotlinlang.org/docs/reference/java-interop.html
My java codes:
@TypeQualifierNickname
@Nonnull(when = When.ALWAYS)
@Retention(RetentionPolicy.RUNTIME)
public @interface NN {
}
@NN
public static <T> T notNull(@Nullable T nullable, String message) throws NullPointerException {
return Objects.requireNonNull(nullable, message);
}
Error calling:
fun max(comparator: Comparator<in T>): T {
val maxOrNull: T? = ...
// IDEA error:
// Type mismatch.
// Required: T
// Found: T?
return Require.notNull(maxOrNull, "Collection is empty.")
}
But if I replace @NN
to @org.jetbrains.annotations.NotNull
, no error:
@org.jetbrains.annotations.NotNull
public static <T> T notNull(@Nullable T nullable, String message) throws NullPointerException {
return Objects.requireNonNull(nullable, message);
}