[英]Can I not mix @NonNullByDefault with inherited classes that have no null annotation constraint?
I'm experimenting with Eclipse null annotations and I'm getting an error. 我正在试验Eclipse null注释 ,我收到了一个错误。 I have a class A (example below) of which I do not have the source.
我有一个A类(下面的例子)我没有源。 I want to extend it for a class which is annotated by
@NonNullByDefault
. 我想将它扩展为一个由
@NonNullByDefault
注释的@NonNullByDefault
。 This throws an error because the @NonNull
constraint does not match the unconstrained (nullable as inherited) parameters of the parent class. 这会引发错误,因为
@NonNull
约束与父类的无约束(可为空的可继承)参数不匹配。
Eclipse recommends adding @Nullable
but this does not make the error go away. Eclipse建议添加
@Nullable
但这不会使错误消失。
Am I doing something wrong? 难道我做错了什么?
Class A: A类:
public class A {
public void SomeMethod(
String[] a)
{
}
}
Class B: B级:
@NonNullByDefault
public class B extends A {
@Override
public void SomeMethod(
@Nullable String[] a)
{
}
}
The error I receive is: 我收到的错误是:
Illegal redefinition of parameter a, inherited method from A does not constrain this parameter
非法重新定义参数a,从A继承的方法不会约束此参数
It is possible to use @NonNullByDefault
with inherited, unconstrained classes, but an explicit @Nullable
constraint must be applied to method parameters of any method which is overriden. 可以将
@NonNullByDefault
与继承的非约束类一起使用,但必须将显式的@Nullable
约束应用于任何被覆盖的方法的方法参数。 This is because the default, unconstrained behavior for java, is to allow nulls. 这是因为java的默认无约束行为是允许空值。
The confusion here is a result of a misplacement of the @Nullable
annotation. 这里的混淆是
@Nullable
注释错位的结果。 The placement in the original question suggests that the array contents should not be null rather than the array itself. 原始问题中的位置表明数组内容不应该为null而不是数组本身。
There is a bug but it's not with the annotation; 有一个错误,但它没有注释; it's a result of Eclipse (as of 4.6) placing the annotation in the wrong location with the Quick Fix feature, reinforcing an erroneous placement which does not correct the error.
这是Eclipse(截至4.6)的结果,使用快速修复功能将注释放在错误的位置,加强了错误的放置,无法纠正错误。
The correct placement for arrays is: 数组的正确位置是:
public void SomeMethod(
String @Nullable[] a)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.