当我使用 Kotlin 反射来获取Kotlin属性的可空性时,就像val nullableString: String?
我在. 它也适用于“内部”类型,例如不仅整个列表可以为空,而且单个元素也可以为.isMarkedNullable
KType
val nullableList: List<String?>?
null
我也想对Java类型使用相同的机制。它部分有效,例如当我有类似public @javax.annotation.Nullable String text;
. 但@javax.annotation.Nullable
不能这样使用,public List<@Nullable String> list;
因为它没有TYPE_USE
目标。所以我想使用@org.checkerframework.checker.nullness.qual.Nullable
,但是 Kotlin 反射忽略了这个注释。
这是一些测试:
public class KotlinReflectionTest {
public static void main(String[] args) {
for (Field field : TestClass.class.getFields()) {
final KProperty<?> kProperty = ReflectJvmMapping.getKotlinProperty(field);
System.out.println(kProperty.getName() + " nullable: " + kProperty.getReturnType().isMarkedNullable());
}
}
public static class TestClass {
public String text; // false
public @javax.annotation.Nullable String textJavax; // true
public @org.jetbrains.annotations.Nullable String textJetbrains; // false
public @org.checkerframework.checker.nullness.qual.Nullable String textChecker; // false
}
}
产生以下输出:
text nullable: false
textJavax nullable: true
textJetbrains nullable: false
textChecker nullable: false
是否可以告诉 Kotlin 反射使用哪些注释?他们是否计划支持 Checker 框架?我会很感激任何评论。谢谢