How to use NonNullByDefault on a package
compiler-warnings, eclipse, java
Solution
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:
- 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"
- 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
- When the "New Java Package" dialog appears, check the "Create package-info.java" checkbox and click "Finish"
Your Java editor will now have a new file titled "package-info.java" displayed with the following content:
/**
*
*/
/**
* @author xyz
*
*/
package org.public_domain;
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;
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.
Problem
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.