Null analysis in eclipse compatibility break between 7 and 8

728 views Asked by At

I have encountered strange behaviour of nullcheck analysis under Spring tool suite 3.6.2 (Eclipse clon) on windows 7 64bit with Oracle java 8u25. The same maven project with java 7 source compatibility successfully finds NPE error in eclipse, but when I change compilation version in maven to java 1.8, the eclipse is unable to find this error.

My nullcheck analysis configuration in Eclipse (Java->Compiler->Errors/Warnings->Null analysis) is: Include asserts in null analysis true Enable annotation based analysis true NotNull custom annotation is properly set to javax.validation.constraints.NotNull etc. (everything seems to be OK, as it works under java 7)

My maven pom is here http://pastebin.com/pF1yJSG2 , as mentioned above when java.version in pom is 1.7 null check works, when is 1.8 null check does not work.

Sample source code is:

package test.nullcheckbug.core;

import javax.validation.constraints.NotNull;

public class Program {

/**
 * This will fail to compile under java 7 (null analysis works in STS
 * 3.6.2). But under java 8 it does not show error in Eclipse Markers -
 * static analysis does not work ?!
 * 
 * @return null which is not allowed
 */
@NotNull
public String fail() {
    return null;
}

/**
 * Simple main.
 * 
 * @param args
 *            ignored args
 */
public static void main(String[] args) {
}
}

Does anybody know where is problem and how to enable nullcheck to work under jdk 1.8 compatibility ?

EDITED: Maven seems to be not involved. The same problem simulated on non maven project with the same source code and compatibility level of compiler set to 1.7. Is it a bug ?

EDITED-2: After more examination I have found that the following difference in annotation makes difference : java.lang.annotation.ElementType.TYPE_USE , when annotation does not have this, the nullcheck is not detected under Java 8, but is detected under Java 7. But why ?! Why there is so different behaviour ?!

EDITED-3: After research from MartinLippert and tested by me it seems that nullcheck API has drastically changed between java 7 and java 8. Null detection requires (as seen from version 2.0 of eclipse libraries) java.lang.annotation.ElementType.TYPE_USE, the types @Target(value={METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER}) are ignored in analysis. SO THE QUESTION IS NOW AS FOLLOWS: WHY NULL ANALYSIS UNDER JAVA 8 REQUIRES AND WORKS ONLY UNDER NEW ELEMENT TYPE ? (I understand that with java 8 it is good to fully utilise new language features, but why was needed to break compatibility ? For example javax.validation @NotNull is now unusable as nullchecking annotation :-((( )

1

There are 1 answers

4
Stephan Herrmann On BEST ANSWER

For Eclipse Luna, the development effort was focused on the "typical" combinations:

  • Java 7 and declaration annotations
  • Java 8 and type annotations

In this version the combination Java 8 and declaration annotations (what is requested in this question) was not fully implemented. This has been fixed via https://bugs.eclipse.org/bugs/show_bug.cgi?id=435805

The fix is available in milestone builds towards Eclipse Mars since M4.

OTOH, I can only encourage projects using Java 8 to upgrade to type annotations for much greater expressiveness - enabling much more precise null type checking.