The J2K converter assumes some ambiguous fields/pa...
# intellij
c
The J2K converter assumes some ambiguous fields/params (inherited, generics etc) to be nullable and after conversion adds
?
to them. Is there a way we can avoid this? Does
@ParametersAreNonnullByDefault
work?
1
youtrack 1
a
my general experience is that it will respect any recognised nullability annotations https://kotlinlang.org/docs/java-interop.html#nullability-annotations
@ParametersAreNonnullByDefault
isn’t listed as a supported one though https://github.com/JetBrains/kotlin/blob/v1.8.22/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/JvmAnnotationNames.kt
c
Unfortunately it doesn’t. For a java class like:
Copy code
class TestChild extends TestParent<Object> {
    public TestChild(String a) {
        super(a);
    }
}
J2K converter outputs:
Copy code
internal class TestChild(a: String?) : TestParent<Any?>(a)
which is incorrect.
a
help me out, what’s incorrect?
c
Ah! Sorry for the confusion. We use ErrorProne NullAway so we assume all unannotated types are non-null by default. But J2K converter assumes it as nullable.
So the output in this case should be:
Copy code
internal class TestChild(a: String) : TestParent<Any>(a)
a
ahh okay!
how does NullAway work? Does it add nullability annotations during compilation?
I would guess that j2k takes the source code and then converts it to Kotlin, so the `@NonNull`/`@Nullable` annotations would have to be explicitly added to the source code
c
Given these annotations, NullAway performs a series of type-based, local checks to ensure that any pointer that gets dereferenced in your code cannot be
null
. NullAway is similar to the type-based nullability checking in the Kotlin and Swift languages, and the Checker Framework and Eradicate null checkers for Java. source
Yea guess we need to explicitly add
@NonNull
. Strangely the “old” J2K converter works as per our requirement but the “new” one takes a safer route.
c
settings
is not being used in NewJavaToKotlinConverter.kt
h
Oh you are right. Strange, maybe it is a bug.