`MapProperty` questions: - Is it possible to have ...
# plugin-development
m
MapProperty
questions: • Is it possible to have
null
as a value? • Having
MapProperty<String, Any?>
displays an error in my IDE but compiles fine. Where is the disconnect? How come the KIJP knows that the bound is non nullable?
v
In 8.14.3 the IDE does not show an error. But with 9+ the nullable annotations were changed to use the JSpecify annotations which provide better results. The
org.gradle.api.provider
package is annotated with
@NullMarked
which means that anything that is not annotated as nullable is non-null and so the V is a non-nullable type.
The
put
already shows an error in IDE already if you give
null
with 8.14.3 as there the old nullable annotations already work too.
And a
provider { null }
would anyway not work, as that is an absent provider, so even if you could have
null
as value, then only as direct value not as provider.
m
In 8.14.3 the IDE does not show an error.
This is what gets me confused, I compile against
8.0
so where does the IDE gets that information from?
The
org.gradle.api.provider
package is annotated with
@NullMarked
TIL about package level annotations til Turns out all of `org.gradle` is now annotated as `NullMarked` ?
That explains the error using 9+, thanks!
I'm still not getting where the IDE gets that information from, maybe I have several
gradle-api
in my compile classpath šŸ¤”
v
Maybe, if I just have an 8.0.2 build I also do not get IDE complaint about the declaration but about the usage
m
Trying a minimal reproducer
v
But well, having the error is more correct than not having the error actually
Unless of course
null
would be a valid value
m
Exactly, with Gradle 9 some previously "valid" code now fails to compile or so
"broke my workflow"
restore the heating space bar šŸ™ƒ
v
That's sad, but you also couldn't add
null
before anyway
As you see in the screenshot, there is an error on the
put
and it also does not compile with 8.14.3
m
Yeaa, it's probably all for the best
v
Or well, it compiles but fails at runtime
Copy code
> Configure project :
w: file:///D:/Sourcecode/other/showcase/build.gradle.kts:55:16: Type mismatch: inferred type is Any? but Any was expected

FAILURE: Build failed with an exception.

* Where:
Build file 'D:\Sourcecode\other\showcase\build.gradle.kts' line: 55

* What went wrong:
Cannot add an entry with a null value to a property of type Map.
šŸ‘ 1
m
Alright, tried a minimal reproducer with only
dev.gradleplugins:gradle-api:8.0
in the compile classpath, the IDE error actually depends on the Gradle wrapper version 🤷
Probably an IJ issue applying the package level annotation from the wrapper version
v
Possible