1

In Java, I use the 'null analysis' using annotations. I don't use Eclipse's default annotations and I have defined my owns.

For each package, I used to add a global NonNullByDefault annotation in the package-info.java file.

With Java version <= Java7, the code below has no problem, everything works fine.

@my.package.annotations.NonNullByDefault
package my.foo.package;

But when I set Eclipse to be Java8 compliant (the only JDK installed on my system is Java8), then an error occurs:

A default nullness annotation has not been specified for the package my.foo.package - Java Problem

My annotation is declared in the following way:

@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.PACKAGE, ElementType.TYPE, 
          ElementType.METHOD, ElementType.CONSTRUCTOR })
public @interface NonNullByDefault { 
    // marker annotation
}

What's new in Java8 about that? Can someone explain me the good way to use my annotations?

2
  • Please provide a full definition of your NonNullByDefault annotation. Commented May 9, 2015 at 10:08
  • The annotation is just used to check if it's present or not. There is no code in. @Retention(RetentionPolicy.CLASS) @Target({ ElementType.PACKAGE, ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR }) public @interface NonNullByDefault { // marker annotation } And it's works fine using the Java7 compliance Commented May 9, 2015 at 10:30

2 Answers 2

1

JDT's own annotations have significantly changed between versions 1.x and 2.x of org.eclipse.jdt.annotation. To fully leverage the greater precision of type annotations in Java 8, @NonNullByDefault now has an attribute of type DefaultLocation[], specifying where exactly the default should apply.

Rebuilding your own annotations with the same level of details and making JDT understand your own annotations will be cumbersome.

Still, an attribute-less @NonNullByDefault should be understood in the "default" way (see the default value of org.eclipse.jdt.annotation.NonNullByDefault.value). Apparently there's a bug in the evaluation of such custom annotations in Java 8 projects.

Sign up to request clarification or add additional context in comments.

Comments

0

Can you please try the following:

  1. Put a file name package-info.java into each of your Java packages
  2. Enter the following in the file:

    @NonNullByDefault package your.package; import org.eclipse.jdt.annotation.NonNullByDefault;

This will specify that you don't want to accept null anywhere for any argument and you will also not be putting loads of @NonNull in the code. You may get a warning like A default nullness annotation has not been specified for the package my.foo.package if you create a new package but forget to copy the file.

2 Comments

That is exactly what I am already doing. I juste use my own annotation instead of org.eclipse.jdt.annotation.NonNullByDefault. My code has no problem using the Java7 compliance.
Okay. It should be working because when I had similar problem this was what I did and it worked for me. Anyways then I may not find any other problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.