如何在Kotlin生成的Java代码中禁用@ NonNull/@ Nullable批注 [英] How to disable @NonNull/@Nullable annotations in Kotlin generated Java code

查看:71
本文介绍了如何在Kotlin生成的Java代码中禁用@ NonNull/@ Nullable批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Kotlin生成的Java代码中禁用@NonNull/@Nullable注释,因为某些注释适配器(代码生成器)无法正确处理某些注释字段

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

您知道怎么做吗?某些Kotlin批注或编译器指令

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

问题: 科特琳课:

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

)

生成的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();
}
}

我要删除:@org.jetbrains.annotations.NotNull()批注

推荐答案

您不能.生成的Java代码工具显示了类的Java代码.结果,它将包括@Nullable和@NotNull;它们是语言的核心部分.他们在那里处理零安全.

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@NotNull public String x相同(需要有效的初始化和分号,但您知道了).除非您编写自己的编译器,否则您不能从编译后的代码中自动删除它们(但这确实很痛苦).

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).

如果由于注释而有问题,请改用Java.注释不会在那里添加,您也不会有任何问题.您需要混合使用各种语言,但是它是为此而设计的,所以应该没事.

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.

这篇关于如何在Kotlin生成的Java代码中禁用@ NonNull/@ Nullable批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆