Custom nullability annotations


#1

I use the Minecraft plugin framework Sponge, and one of the design decisions is to use null only when completely necessary. Rather than annotate every single method, argument, and class in the framework, they have elected to make their own @NonnullByDefault annotation which gets applied to packages in package-info.java. Obviously, since it’s custom, Kotlin doesn’t recognize it as a nonnullability annotation and therefore leaves all platform types alone in interop.
Is there a way to create a compiler plugin to recognize Sponge’s @NonnullByDefault annotation and infer nonnullability on everything that isn’t explicitly annotated @Nullable? Compiler plugins in general seem completely undocumented.


#2

They are… As far as I know the Kotlin team plans on releasing a public version of the compiler plugins at some point in the future, but they never said when. Right now plugins are an internal feature without a stable API.


As to your nullability problem, right now I would ignore it as best as you can. You can treat platform types as if they are non nullable so I don’t think this should really bother you too much.


#3

I know I can ignore them, but there’s a lot of warnings that I end up suppressing.


#4

Ok. I thought you could simply do something like this

val someValue: PlatformType = getPlatformType()  // now someValue is not nullable in this case

I think this should even create an error if getPlatformType is annotated with @Nullable.


Which warnings do you get exactly? I never really encountered any working with platform types (not that I know of anyways). Maybe there is something else you can do.


#5

When I don’t add a type definition (for example val query = DataQuery.of("ExamplePath") ), I will get a warning that the type is inferred, and that this is bad. But if I add !! to the end, it will tell me that there’s no need for it. Something that is basically a literal should not need a type definition, same as if I wrote val query = "ExamplePath".


#6

How often do you encounter such problems? If it is not that often, you can work around it by defining external annotations for the Minecraft API.


#7

I get them basically every time I create anything, since Sponge uses builder and .of patterns, so everything’s a return type instead of a constructor.