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

查看:27
本文介绍了如何在 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天全站免登陆