parent 的有效注释对 child 无效
Valid annotation for parent is invalid for child
你好,我有 parent class 参数带有可为 null 的注释。
class Parent {
@Nullable
String name;
Parent(@Nullable Strign name) {
this.name = name;
}
Driver createDriver() {
return new CommonDriver(name);
}
}
我有多个 children class,对于其中的大多数,"name" 参数可以为 null,但有些不能。
class ChildC extends Parent {
ChildC(@NotNull String name){
super(name);
}
@Override
Driver createDriver() {
return new ChildCDriver(name);
}
}
现在我在 ChildCDriver 中遇到问题(来自 intelliJ 的代码检查),其中名称是 @NotNull
这能以某种方式解决吗?
这是合理的代码,但 IntelliJ 还不够强大,无法证明代码是正确的。您需要抑制警告。单击该行,按 Alt+Enter
,然后在该菜单或子菜单中找到 "Suppress"。
Nullness Checker can verify your code. The complete code appears below. The @FieldInvariant
annotation expresses that the field has a more precise type in the subclass.
没有 @FieldInvariant
注释,Nullness Checker 在第 27 行发出此警告:
error: [argument.type.incompatible] incompatible types in argument.
return new ChildCDriver(name);
^
found : @Initialized @Nullable String
required: @Initialized @NonNull String
使用 @FieldInvariant
注释,Nullness Checker 证明代码是正确的。
下面的代码示例使用了 Checker Framework 的 @NonNull
和 @Nullable
注释,但 Nullness Checker also supports @NotNull
因此您可以继续在代码中使用 JetBrains 注释。
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.FieldInvariant;
class Parent {
final @Nullable String name;
Parent(@Nullable String name) {
this.name = name;
}
Driver createDriver() {
return new CommonDriver(name);
}
}
@FieldInvariant(qualifier = NonNull.class, field = "name")
class ChildC extends Parent {
ChildC(@NonNull String name) {
super(name);
}
@Override
Driver createDriver() {
return new ChildCDriver(name);
}
}
interface Driver {}
class CommonDriver implements Driver {
CommonDriver(@Nullable String name) {}
}
class ChildCDriver implements Driver {
ChildCDriver(@NonNull String name) {}
}
你好,我有 parent class 参数带有可为 null 的注释。
class Parent {
@Nullable
String name;
Parent(@Nullable Strign name) {
this.name = name;
}
Driver createDriver() {
return new CommonDriver(name);
}
}
我有多个 children class,对于其中的大多数,"name" 参数可以为 null,但有些不能。
class ChildC extends Parent {
ChildC(@NotNull String name){
super(name);
}
@Override
Driver createDriver() {
return new ChildCDriver(name);
}
}
现在我在 ChildCDriver 中遇到问题(来自 intelliJ 的代码检查),其中名称是 @NotNull
这能以某种方式解决吗?
这是合理的代码,但 IntelliJ 还不够强大,无法证明代码是正确的。您需要抑制警告。单击该行,按 Alt+Enter
,然后在该菜单或子菜单中找到 "Suppress"。
Nullness Checker can verify your code. The complete code appears below. The @FieldInvariant
annotation expresses that the field has a more precise type in the subclass.
没有 @FieldInvariant
注释,Nullness Checker 在第 27 行发出此警告:
error: [argument.type.incompatible] incompatible types in argument.
return new ChildCDriver(name);
^
found : @Initialized @Nullable String
required: @Initialized @NonNull String
使用 @FieldInvariant
注释,Nullness Checker 证明代码是正确的。
下面的代码示例使用了 Checker Framework 的 @NonNull
和 @Nullable
注释,但 Nullness Checker also supports @NotNull
因此您可以继续在代码中使用 JetBrains 注释。
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.FieldInvariant;
class Parent {
final @Nullable String name;
Parent(@Nullable String name) {
this.name = name;
}
Driver createDriver() {
return new CommonDriver(name);
}
}
@FieldInvariant(qualifier = NonNull.class, field = "name")
class ChildC extends Parent {
ChildC(@NonNull String name) {
super(name);
}
@Override
Driver createDriver() {
return new ChildCDriver(name);
}
}
interface Driver {}
class CommonDriver implements Driver {
CommonDriver(@Nullable String name) {}
}
class ChildCDriver implements Driver {
ChildCDriver(@NonNull String name) {}
}