Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NonNullByDefault on a package

My compiler warns me about A default nullness annotation has not been specified for the package. I'm using Juno with Java 7.

The documentation stats you should add @NonNullByDefault as a package option, but I don't know what that means.

Can I somehow set NonNullByDefault for the whole package? In Eclipse I can't right click on a package and say add option or something similar.

Or do I just add @NonNullByDefault to any class in a package and then it's valid for all beans in that package? Or do I have to create some meta info file in the pakage to add package options? I must be blind because I can't find any help online....

A small example would be great.

like image 822
Franz Kafka Avatar asked May 16 '12 17:05

Franz Kafka


2 Answers

Summary:
Getting the above advice to work was not intuitive to me. Nor was the actual solution I just figured out. In the package-info.java file:
a. put the annotation ABOVE the package declaration
b. put the NonNullByDefault annotation import AFTER the package declaration


Details:
I don't know if I am the only person who read the above answers and couldn't figure out how to get this working. So, just to be extra clear, here's the (non-obvious nor intuitive) steps I took to get this to finally work in Eclipse-Juno:

  1. In Package Explorer, goto the root Java package showing the warning (or error) message starting with "A default nullness annotation has not been specified for the package..." - for this example, please assume the package name of "org.public_domain"
  2. On the Java package, activate the right-click-menu and select the "New -> Package" option - yes, I know your package already exists, consider it an eclipse idiosyncrasy - it's not going to do anything to your existing package
  3. When the "New Java Package" dialog appears, check the "Create package-info.java" checkbox and click "Finish"
  4. Your Java editor will now have a new file titled "package-info.java" displayed with the following content:

    /**
     * 
     */
    /**
     * @author xyz
     *
     */  
    package org.public_domain;
    
  5. Now, rearrange the file content to look like this:

    /**
     * 
     */
    /**
     * @author xyz
     *
     */  
    @NonNullByDefault  
    package org.public_domain;  
    import org.eclipse.jdt.annotation.NonNullByDefault;
    

    ...or like this...

    /**
     * 
     */
    /**
     * @author xyz
     *
     */  
    @org.eclipse.jdt.annotation.NonNullByDefault
    package org.public_domain;  
    
  6. Enjoy the awesomeness that is eclipse @NonNullByDefault

My challenge was figuring out that I was to put the annotation ABOVE the package declaration. And to put the NonNullByDefault annotation import AFTER the package declaration. For me, that was not intuitive.

Hope this saves someone else the time I've ended burning up chasing down this particular pathway.

like image 181
chaotic3quilibrium Avatar answered Oct 22 '22 17:10

chaotic3quilibrium


If you simply want to get rid of the warning and ignore this feature because you're not making use of it, you can simply disable it in the Eclipse preferences:

Window > Preferences > Java > Compiler > Errors/Warnings > Null analysis > Missing '@NonNullByDefault' annotation on package = [Ignore]

like image 26
Yago Méndez Vidal Avatar answered Oct 22 '22 18:10

Yago Méndez Vidal