I get the error
Null type safety (type annotations): The expression of type 'String' needs unchecked conversion to conform to '@NonNull String'
at statement A of this class:
package org.abego.util;
public class MyClass {
private String[] names = new String[]{"Alice", "Bob", "Charlie"};
public String getName(int index) {
String name = names[index];
return name; /* statement A */
}
}
The package org.abego.util
defines the default nullness to be "@NonNull":
@org.eclipse.jdt.annotation.NonNullByDefault
package org.abego.util;
When adding @NonNull
to the array definition:
package org.abego.util;
import org.eclipse.jdt.annotation.NonNull;
public class MyClass {
private @NonNull String[] names = new @NonNull String[]{"Alice", "Bob", "Charlie"};
public String getName(int index) {
String name = names[index];
return name; /* statement A */
}
}
the warning at statement A goes away, however I get a new warning
The nullness annotation is redundant with a default that applies to this location
for the type @NonNull String[]
in the array definition.
I found no way to make this code warning-free.
Could the 'redundant' warning be wrong? It is my understanding the NonNullByDefault
declaration will make sure a type definition String[]
will be interpreted as String @NonNull[]
, but not as @NonNull String[]
or as @NonNull String @NonNull[]
. So the explicit nullness annotation in @NonNull String[]
is not redundant, but necessary to get the effective type @NonNull String @NonNull[]
.
(I am using Eclipse 4.5 (Mars) and jdk1.8.0_60.)
Your expectations are correct. @NonNullBeDefault
doesn't affect any details of an array type (unless you include DefaultLocation.ARRAY_CONTENTS in the annotation's value ).
I believe this to be a variant of https://bugs.eclipse.org/440398
Thanks for the concise example, btw.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.