4

In eclipse, it's possible to set not null by default for a single package. Is there any way to set it across all packages, or for a tree of packages?

Edit

I'm talking about the @Nonnull annotations that eclipse respects and polices. At the moment, we are putting them on every function, every variable every parameter. I know we can just set them package wide, by putting the attribute in package-info - but we have hundreds of packages, and it's easy to miss one. I am hoping to be able to set eclipse to say "assume @Nonnull by default, unless @Nullable is specified".

lakshman
  • 2,641
  • 6
  • 37
  • 63
Darren Oakey
  • 2,894
  • 3
  • 29
  • 55
  • 2
    Not null for what? There is no such thing as a null package. – Sotirios Delimanolis Jun 03 '13 at 03:53
  • what do you mean by set not null by default? Please provide more explanation, so that it helps us to understand your problem and easy to answer – vineet Jun 03 '13 at 03:53
  • 3
    I'm talking about the @@Nonnull annotations that eclipse respects and polices. At the moment, we are putting them on every function, every variable every parameter. I know we can just set them package wide, by putting the attribute in package-info - but we have hundreds of packages, and it's easy to miss one. I am hoping to be able to set eclipse to say "assume @@Nonnull by default, unless @@Nullable is specified" (@@ is an attempt to escape @) – Darren Oakey Jun 03 '13 at 05:29
  • 1
    Please add clarifications to the original question. That ensures everyone sees them, gives you more room to type and a better editor with more formatting tools. Seeing that you're new, I did it for you this time. – meriton Jun 03 '13 at 06:19

3 Answers3

1

Eclipse can help you detect any packages that are lacking a @NonNullByDefault if you enable the compiler option "Missing '@NonNullByDefault' annotation on package". This option is set to "Ignore" by default, set to "Warning" or "Error" as you like.

See http://help.eclipse.org/luna/topic/org.eclipse.jdt.doc.user/reference/preferences/java/compiler/ref-preferences-errors-warnings.htm?cp=1_4_2_0_3_1

Stephan Herrmann
  • 7,963
  • 2
  • 27
  • 38
0

Try this cute little project:

https://code.google.com/p/notnullcheckweaver/

It inserts null checks around every element annotated with @NotNull

Its default setting is @Nullable, though, so you should still annotate your (root) package, to get it to enforce everything except @Nullable annotated elements.

Drawbacks:

  • It introduces yet another @NotNull annotation and does not allow others (e.g. java's own @Nonnull, spring annotations, etc.)
  • It works through bytecode manipulation and java-agents
Volker E.
  • 5,911
  • 11
  • 47
  • 64
-3

Are you sure you want to add @Notnull to every function, every variable and every parameter in your project (keeping in mind that you have hundreds of packages)?

What if you function needs to return null or parameter needs to be null?

I don't think this is a good idea.

FazoM
  • 4,777
  • 6
  • 43
  • 61
  • > What if you function needs to return null or parameter needs to be null? You'd annotate with a nullable annotation. Instead of the other way around of everything nullable by default, it's a more sane approach that other languages are also adopting. – Technomad Dec 31 '21 at 16:13