How to disable @NonNull/@Nullable annotations in Kotlin generated Java code

不问归期 提交于 2021-01-29 09:11:40

问题


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


回答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.



来源:https://stackoverflow.com/questions/52221783/how-to-disable-nonnull-nullable-annotations-in-kotlin-generated-java-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!