I'm experimenting with Eclipse null annotations and I'm getting an error. I have a class A (example below) of which I do not have the source. I want to extend it for a class which is annotated by @NonNullByDefault
. This throws an error because the @NonNull
constraint does not match the unconstrained (nullable as inherited) parameters of the parent class.
Eclipse recommends adding @Nullable
but this does not make the error go away.
Am I doing something wrong?
Class A:
public class A {
public void SomeMethod(
String[] a)
{
}
}
Class 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
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. This is because the default, unconstrained behavior for java, is to allow nulls.
The confusion here is a result of a misplacement of the @Nullable
annotation. The placement in the original question suggests that the array contents should not be null rather than the array itself.
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.
The correct placement for arrays is:
public void SomeMethod(
String @Nullable[] a)
{
}