Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Why does Eclipse think something cannot be null?
Why does Eclipse think something cannot be null? [message #1765613] Mon, 12 June 2017 15:24 Go to next message
Eclipse UserFriend
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 #1765627 is a reply to message #1765613] Mon, 12 June 2017 17:14 Go to previous messageGo to next message
Eclipse UserFriend
The "Unsafe interpretation ..." warning is quite a subtle but important issue, when using null annotations in conjunction with generics.
We wrote about this


Is readMethod of type java.lang.reflect.Method? If so, the solution for you would be to use external annotations to mark the return type of that method as @Nullable.
See


HTH,
Stephan
Re: Why does Eclipse think something cannot be null? [message #1765637 is a reply to message #1765627] Mon, 12 June 2017 19:39 Go to previous messageGo to next message
Eclipse UserFriend
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.
Re: Why does Eclipse think something cannot be null? [message #1766213 is a reply to message #1765637] Sat, 17 June 2017 17:07 Go to previous messageGo to next message
Eclipse UserFriend
Quote:
... 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.


I tried it in a recent build and it works correctly when I follow your steps.
What's your version of Eclipse / JDT ?

We fixed a few issues in this area during Oxygen, nothing that would directly match your use case, though...
Re: Why does Eclipse think something cannot be null? [message #1766261 is a reply to message #1766213] Mon, 19 June 2017 11:14 Go to previous messageGo to next message
Eclipse UserFriend
I'm using Neon.3. Looking in installation details, it appears I have version 3.12.3.v20170301-0400 of the JDT.
Re: Why does Eclipse think something cannot be null? [message #1766285 is a reply to message #1766261] Mon, 19 June 2017 17:51 Go to previous message
Eclipse UserFriend
Can you try the SDK release candidate from http://download.eclipse.org/eclipse/downloads/drops4/S-4.7RC4a-201706120950/ ?
Otherwise, if you need a specific installation package, I recommend you wait until Wednesday next week, which is when Oxygen will be released (containing JDT 3.13.0).

Other question would be: does it fail in a fresh workspace, too?
Previous Topic:Change in Neon - packages taken from target platform instead of the workspace
Next Topic:Java classes and Jenkins CI
Goto Forum:
  


Current Time: Thu May 22 13:47:51 EDT 2025

Powered by FUDForum. Page generated in 0.05878 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top