如何规避检查器框架 type.invalid 错误?
How to circumvent checker framework type.invalid error?
我们有一个我们编写的库,在引入检查器框架 NullnessChecker 来验证其代码后,它无法编译(如预期)。我已经修复了所有明显的错误,但是我不知道如何修复这个错误...
这是违规函数的签名:
private static @Nullable char[] getChars(char ch)
以及发生错误的调用站点:
@Nullable char[] replacement = getChars( string.charAt( index ) );
谁能告诉我如何让 checker 接受这个?在我看来是正确的代码。
编辑
错误:
[type.invalid] [@Initialized, @Nullable] may not be applied to the type "@Initialized @Nullable char"
错误信息
[@Initialized, @Nullable] may not be applied to the type "@Initialized @Nullable char"
如果只是
会更清楚
@Nullable may not be applied to the type "char"
问题是 char
是原始类型。说 @Nullable char
或 @NonNull char
没有意义。 Nullness 仅适用于对象(非原始)类型。初始化也是一样。
如果要指定 char
的可空数组——也就是说,变量 replacement
为 null 或者是 char
的数组——那么这样写方式:
char @Nullable [] replacement = ...;
如果你写
@Nullable char [] replacement = ...;
那么就是一个@Nullable char
.
的数组
这是 Java 类型注释语法的标准部分,并非特定于 Checker Framework。但是,有一个 FAQ about this in the Checker Framework manual.
我们有一个我们编写的库,在引入检查器框架 NullnessChecker 来验证其代码后,它无法编译(如预期)。我已经修复了所有明显的错误,但是我不知道如何修复这个错误...
这是违规函数的签名:
private static @Nullable char[] getChars(char ch)
以及发生错误的调用站点:
@Nullable char[] replacement = getChars( string.charAt( index ) );
谁能告诉我如何让 checker 接受这个?在我看来是正确的代码。
编辑
错误:
[type.invalid] [@Initialized, @Nullable] may not be applied to the type "@Initialized @Nullable char"
错误信息
[@Initialized, @Nullable] may not be applied to the type "@Initialized @Nullable char"
如果只是
会更清楚@Nullable may not be applied to the type "char"
问题是 char
是原始类型。说 @Nullable char
或 @NonNull char
没有意义。 Nullness 仅适用于对象(非原始)类型。初始化也是一样。
如果要指定 char
的可空数组——也就是说,变量 replacement
为 null 或者是 char
的数组——那么这样写方式:
char @Nullable [] replacement = ...;
如果你写
@Nullable char [] replacement = ...;
那么就是一个@Nullable char
.
这是 Java 类型注释语法的标准部分,并非特定于 Checker Framework。但是,有一个 FAQ about this in the Checker Framework manual.