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.
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:
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With