2

I am using a library that is written and java with @Nonnull and @Nullable annotations on all of the return values and parameters. Unfortunately, the annotations are incorrect in a few places. There is an interface with this method:

interface Expiry<K, V> {
  long expireAfterCreate(@NonNull K key, @NonNull V value, long currentTime);
}

Unfortunately in my case, @NonNull V value can actually be null (since it is a generic type). How can I get Kotlin compiler to ignore the java annotation and allow me to override the method with the proper type:

object Foo : Expiry<String, Bar?> {
  override fun expireAfterCreate(key: String, value: Bar?, currentTime: Long): Long {
    TODO()
  }
}
Burg
  • 1,988
  • 1
  • 18
  • 22
  • 1
    I don't believe there is any way to ignore this. That annotation is interpreted as type information, and kotlin is strongly typed and does not allow you to ignore type information. This is a useful feature in a majority of cases. – Trevor Aug 04 '20 at 17:23
  • Duplicated question. https://stackoverflow.com/questions/52221783/how-to-disable-nonnull-nullable-annotations-in-kotlin-generated-java-code – Pemassi Aug 04 '20 at 17:56
  • Note that Kotlin will not stop you from null-checking a value it thinks cannot be null — it gives a warning, but compiles the check anyway. – gidds Aug 04 '20 at 17:57

0 Answers0