Is there a way to configure the compiler to emit b...
# compiler
m
Is there a way to configure the compiler to emit bytecode that has
@Nullable
for a method that returns a nullable type, etc, so that in plain Java code the IDE can provide the same suggestions that it would for a
@Nullable
Java method?
i
It already does this.
Copy code
$ echo "fun foo(): String? = null" > 1.kt
$ kotlinc 1.kt
$ javap -v _1Kt.class
yields:
Copy code
...
   #7 = Utf8               Lorg/jetbrains/annotations/Nullable;
...
{
  public static final java.lang.String foo();
    descriptor: ()Ljava/lang/String;
    flags: ACC_PUBLIC, ACC_STATIC, ACC_FINAL
    Code:
      stack=1, locals=0, args_size=0
         0: aconst_null
         1: areturn
      LineNumberTable:
        line 1: 0
    RuntimeInvisibleAnnotations:
      0: #7()
}
...
m
Good to know — in various projects in the middle of being transformed into Kotlin, we have Java code that interoperates with Kotlin, and we don’t seem to be getting the nullability inspections in Java that one might get from calling Java with
@Nullable
. Is that expected, or should we perhaps try to package that up into a self contained repro?
i
@mp Yes, it would be great, if you file an issue at https://youtrack.jetbrains.com/issues/KT.
m
OK, will see if I can repro it and file an issue.
Resurrecting this ancient thread -- followed up with another team and they were able to repro at least one of the issues they saw, which was around Guice. On a nullable
?
type, it's fine to inject null, but Guice will barf unless you annotate with
@Nullable
that's available at runtime. However, the emitted annotations have
@Retention(RetentionPolicy.CLASS)
. Is there a way to emit ones that use RUNTIME?