# Custom nullability annotations

**URL:** <https://discuss.kotlinlang.org/t/custom-nullability-annotations/8264>\
**Category:** Support\
**Created:** [June 24, 2018, 12:40pm UTC](https://discuss.kotlinlang.org/t/custom-nullability-annotations/8264 "2018-06-24T12:40:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pie\_flavor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pie_flavor/32/5262_2.png) [@pie\_flavor](https://discuss.kotlinlang.org/u/pie_flavor)\
**Post date:** [June 24, 2018, 12:40pm UTC](https://discuss.kotlinlang.org/t/custom-nullability-annotations/8264/1 "2018-06-24T12:40:19Z")

</div>

I use the Minecraft plugin framework [Sponge](https://spongepowered.org), 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.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 24, 2018, 12:49pm UTC](https://discuss.kotlinlang.org/t/custom-nullability-annotations/8264/2 "2018-06-24T12:49:14Z")

</div>

> [@pie\_flavor](#):
>
> Compiler plugins in general seem completely undocumented.

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.

---

<div class="post-metadata">

**Author:** ![pie\_flavor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pie_flavor/32/5262_2.png) [@pie\_flavor](https://discuss.kotlinlang.org/u/pie_flavor)\
**Post date:** [June 24, 2018, 1:23pm UTC](https://discuss.kotlinlang.org/t/custom-nullability-annotations/8264/3 "2018-06-24T13:23:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 24, 2018, 2:41pm UTC](https://discuss.kotlinlang.org/t/custom-nullability-annotations/8264/4 "2018-06-24T14:41:38Z")

</div>

Ok. I thought you could simply do something like this

```kotlin
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.

---

<div class="post-metadata">

**Author:** ![pie\_flavor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pie_flavor/32/5262_2.png) [@pie\_flavor](https://discuss.kotlinlang.org/u/pie_flavor)\
**Post date:** [August 12, 2018, 11:30pm UTC](https://discuss.kotlinlang.org/t/custom-nullability-annotations/8264/5 "2018-08-12T23:30:48Z")

</div>

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"`.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [August 13, 2018, 4:50am UTC](https://discuss.kotlinlang.org/t/custom-nullability-annotations/8264/6 "2018-08-13T04:50:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pie\_flavor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pie_flavor/32/5262_2.png) [@pie\_flavor](https://discuss.kotlinlang.org/u/pie_flavor)\
**Post date:** [August 13, 2018, 7:17am UTC](https://discuss.kotlinlang.org/t/custom-nullability-annotations/8264/7 "2018-08-13T07:17:38Z")

</div>

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.
