Why does Eclipse think something cannot be null? [message #1765613] |
Mon, 12 June 2017 15:24  |
Eclipse User |
|
|
|
I've written a class that's using reflection to look at types of properties, along with annotations on read methods. The code is working fine, but I noticed an unexpected warning from Eclipse on one line.
The block in question is the following:
if (actualTypeArgument.isAssignableFrom(String.class)) {
int length = 1;
Size sizeAnnotation = readMethod.getAnnotation(Size.class);
if (sizeAnnotation != null) { // redundant null check
if (sizeAnnotation.min() > length) {
length = sizeAnnotation.min();
}
}
List<String> newValue = new ArrayList<>();
for (int ctr = 0; ctr < length; ++ ctr) {
newValue.add("0");
}
writeMethod.invoke(getObject(), newValue);
}
On the line with the "redundant null check" comment, Eclipse yellows the expression and says "Redundant null check: The variable sizeAnnotation cannot be null at this location".
This doesn't make sense to me. There don't appear to be validation annotations on the Method class (neither javax.validation nor org.eclipse.jdt.annotation), and the javadoc for "Method.getAnnotation(Class)" even says the return value can be null (although Eclipse can't know that).
What could be causing this is the warning I see on the line previous to that, which is:
Unsafe interpretation of method return type as '@NonNull' based on substitution 'T=@NonNull Size'. Declaring type 'Method' doesn't seem to be designed with null type annotations in mind
I don't understand why I'm getting this either.
|
|
|
|
Re: Why does Eclipse think something cannot be null? [message #1765637 is a reply to message #1765627] |
Mon, 12 June 2017 19:39   |
Eclipse User |
|
|
|
Ok, reading the instructions at the "help.eclipse.org" page, I created a directory tree on my disk to hold this information, and I created an empty hierarchy down to a directory for "rt", which is where the "Method" class is.
I then edited the JRE entry, adding the external annotation path ("External location") to the "rt.jar" file.
I note that in the "Creating external annotations" section, it starts with the following text:
Quote:Pre-requisites: External annotations require a library with source attachment. Additionally, annotation based null analysis must be enabled for the current project, an external annotation location must be defined for the library and that location must be a directory (not a zip file) within the workspace.
I'm not sure how to interpret several details in this. This seems to imply that I can't use "External location".
In any case, I then navigated to the "java.lang.reflect.Method" class, and the "getAnnotation(Class<T> arg0)" method. It says I can select the "method return type", which is "T" in this case.
I then right-clicked and selected "Annotate", and Eclipse just dinged at me. No error message, no log entry.
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05878 seconds