0

I need to disable @NonNull/@Nullable annotations in Kotlin generated Java code because some annotation adapters (code generators) cant handle properly some annotated fields

Do you know how it could be done? Some Kotlin annotation or compilator directive

Problem: kotlin class:

open class TestModel(
    var test: ByteArray = ByteArray(0)

)

generated java:

public class TestModel {
@org.jetbrains.annotations.NotNull()
private byte[] test;

@org.jetbrains.annotations.NotNull()
public final byte[] getTest() {
    return null;
}

public final void setTest(@org.jetbrains.annotations.NotNull()
byte[] p0) {
}

public TestModel(@org.jetbrains.annotations.NotNull()
byte[] test) {
    super();
}

public TestModel() {
    super();
}
}

I want to remove : @org.jetbrains.annotations.NotNull() annotation

3
  • You can't. This is one of the main reasons Kotlin exists: to provide null-safety and have that be interoperable with Java. If you can't have those annotations, don't use Kotlin. Commented Sep 7, 2018 at 11:41
  • Can't you just do: open class TestModel( var test: ByteArray? = ByteArray(0) ) Commented Sep 7, 2018 at 12:12
  • then I will have @Nullable annotation. I do not want any of them Commented Sep 7, 2018 at 12:27

1 Answer 1

1

You can't. The generated Java code tool shows what the Java code for a class looks like. As a result, it will include @Nullable and @NotNull; they're a core part of the language. They're there to handle null safety.

val x: String is the same as @NotNull public String x (needs initialization and semi-colons to be valid, but you get the idea). You can't remove them automatically from the compiled code, unless you write your own compiler (but that would be a real pain).

If you have problems because of the annotations, just use Java instead. The annotations don't get added there, and you won't have any problems. You'd need to mix the languages, but it's designed for it, so you should be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm doing it now, but it's only Java code in my codebase and it's kind of workaround, so I simply don't like it. But thanks!, good to know that it just how it works. The real problem is with Annotation processor that returns strange type for ByteArray. MirrorType for this field is (@org.jetbrains.annotations.NotNull :: byte)[] instead of simply byte[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.