What’s the annotation that can help in this case, ...
# announcements
g
What’s the annotation that can help in this case, for Java interoperability?
org.jetbrains.annotations.NotNull
doesn’t work on Generic param types 😞
Copy code
fun someFun(list: List<@NotNull String>)
a
It should work. Try to use some more recent annotations package like "org.jetbrainsannotations20.0.0"
g
@Alexey Belkov [JB] Thanks you are right… I was experimenting on a unit test and I forgot to add this entry
testCompileOnly("org.jetbrains:annotations:20.1.0")
It now works without an exception, but I see no warning or runtimeexception when I did this. Aren’t these annotations supposed to help IDE at compile time?
Copy code
@Test
  void notNull() {
      //someFun(null);
      var list = new ArrayList<String>();
      list.add("");
      list.add(null);
      NotNullKt.someFun(list);
  }
Copy code
fun someFun(list: List<@NotNull String>) {
    list.map { println(it) }
}
I hv this option as well
If it’s only for compile time, is there any significance to use it for java interoperability when Kotlin already has a way to express nullability?
a
You're right, it's pointless to annotate anything in Kotlin code. These annotations are intended for Java code. For example, you can do this: import org.jetbrains.annotations.NotNull; import java.util.ArrayList; public class J { public static void main(String[] args) { ArrayList<@NotNull String> list = new ArrayList<@NotNull String>(); list.add(null); // Passing 'null' argument to parameter annotated as @NotNull } }
g
Thanks @Alexey Belkov [JB]!