1
votes

Gradle provides the following class in its API: https://docs.gradle.org/5.6.4/javadoc/org/gradle/api/tasks/ScalaRuntime.html

This class has a simple constructor which accepts a Project instance. However, in my case I don't have a Project on hand, but I still want to use other methods of this class which don't really depend on this value, so I decided to pass a null value to the constructor:

val scalaRuntime = ScalaRuntime(null)

However, the compiler fails with the following error:

Null can not be a value of a non-null type Project

I'm not sure what happens here, because clearly this is a Java class, not Kotlin, and there are no nullability annotations. How come Kotlin rejects this code? I'd assume that it would expect a value of type Project! (i.e. platform type), not a non-nullable type.

The only way to work around this now is to use reflection to invoke this constructor, which naturally works perfectly fine, so it's not like this constructor expects a truly non-nullable parameter.

What am I missing here? Why Kotlin assumes that the constructor argument is non-null?

1
I guessing here, but some of the methods are annotated with @Nullable, so Kotlin likely concludes that any methods without the annotation are non-nullable.Andreas
@Andreas that's what I actually meant under "there are no nullability annotations" - there are indeed none: github.com/gradle/gradle/blob/v5.6.4/subprojects/scala/src/main/…Vladimir Matveev
@Andreas No, it's an annotation on the package.Alexey Romanov

1 Answers

3
votes

The package org.gradle.api.tasks is annotated with @org.gradle.api.NonNullApi:

Marks a type or a whole package as providing a non-null API by default. All parameter and return types are assumed to be Nonnull unless specifically marked as Nullable. All types of an annotated package inherit the package rule. Subpackages do not inherit nullability rules and must be annotated.