5

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 :-((( )

kulatamicuda
  • 1,603
  • 2
  • 21
  • 40
  • 1
    I've seen the Null checks working in Eclipse with Java8, but there is a different version of the annotation library being used as far as I remember. The NotNull annotations are coming from org.eclipse.jdt.annotation. The Java8 version needs version 2 of this JAR file, whereas the previous language versions are happy with version 1.1 of that lib. Can you check if this is related to your problem? – Martin Lippert Nov 21 '14 at 10:40
  • @MartinLippert Yes, this seems to be relevant. The version 2 annotations has ElementType.TYPE_USE, but version 1.1 not. Not sure why this is required for java 8. The pitty is that NotNull annotation from javax.validation.constraints is not declared as ElementType.TYPE_USE but only as Target(value={METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER}) which makes it unusable for null analysis under java 8 :-(. – kulatamicuda Nov 21 '14 at 14:01
  • I didn't know this. Good question, kulatamicuda! – hfontanez Nov 21 '14 at 14:09
  • @hfontanez Thanks. For the others - the only workaround for me now is to use version-2 default library annotations from Eclipse and make all packages NonNullByDefault and than use NotNull where we want (but without check as check is performed implicitly thanks to NonNullByDefault). This workaround works for my project, but question still persist as some projects cannot do so much refactoring as I can and they are probably using more nulls to make NonNullDefault for them no so good solution. – kulatamicuda Nov 21 '14 at 14:16

1 Answers1

1

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.

Stephan Herrmann
  • 7,963
  • 2
  • 27
  • 38
  • I'd like to have Nullcheck in my CI using Findbugs. Using the eclipse compiler instead of javac is no option hence I cannot use eclipse null check (or do I missing something?). Hence I need to use Findbugs Annotations (or jsr305) and cannot use your selfmade annotations. So we need to have compatibility of jsr305 annotations. Is that also fixed in Mars? – cornz Jul 13 '15 at 21:31
  • @cornz: it would be interesting to know, why using the eclipse compiler on CI is not an option. It integrates well with all major build technology, or am I missing anything? – Stephan Herrmann Jul 14 '15 at 09:49
  • @cornz: none of the annotations in this field are more standard than the others. JSR 305 has never taken off (it's dead for several years now), using the namespace javax.annotation is pretentious, not backed by any standardization process. That said, ecj can be configured to use any set of annotations. When using the FindBugs annotations be sure to *avoid* `@Nullable` (which has unclear/confusing semantics) and use `@CheckForNull` instead, see also http://types.cs.washington.edu/checker-framework/current/checker-framework-manual.html#findbugs-nullable. – Stephan Herrmann Jul 14 '15 at 10:00
  • As mentioned in the answer, if a project uses Java 8 (or plans to migrate any time soon), I highly recommend to migrate also to using Java-8-style type annotations for much higher precision. To the best of my knowledge null analysis with type annotations is only supported by the Checkers Framework and Eclipse. In particular the FindBugs annotations cannot be used as type annotations. – Stephan Herrmann Jul 14 '15 at 10:06