java - 如何在 Kotlin 生成的 Java 代码中禁用 @NonNull/@Nullable 注释
问题描述
我需要@NonNull/@Nullable
在 Kotlin 生成的 Java 代码中禁用注释,因为某些注释适配器(代码生成器)无法正确处理某些带注释的字段
你知道怎么做吗?一些 Kotlin 注释或编译器指令
问题:科特林类:
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()
注释
解决方案
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.
推荐阅读
- javascript - 提交最后一次搜索的 jquery-queryBuilder 初始化规则
- python - Python concurrent.futures - 类型错误:zip 参数 #1 必须支持迭代
- javascript - 如何有条件地监听房间消息集合(firestore)中的更新?
- mysql - 从 MySQL 复制到 SQL Server
- javascript - 如何从 javascript 调用 WCF 服务
- angular - 如何访问我的 HTML 中的静态变量并对其进行迭代
- typescript - 获取另一种类型的键的类型(嵌套)
- css - 如何使按钮适合 CSS 网格容器的单元格?
- ios13 - 为 PencilKit (iOS) 将橡皮擦类型设置为位图
- php - 查询生成器在 laravel (pgsql) 中不起作用