Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this for individual packages, but I haven't found a way to have it propagate to subpackages. For method parameters use the built-in package annotation <a href="http://jsr-305.googlecode.com/svn/trunk/javadoc/javax/annotation/ParametersAreNonnullByDefault.html" rel="noreferrer"><code>@ParametersAreNonnullByDefault</code></a>. Apply the annotation to the package in its <code>package-info.java</code> file inside the package's directory.</p> <blockquote> <p>Note that I'm using the <code>javax.annotation</code> annotations from <a href="http://mvnrepository.com/artifact/com.google.code.findbugs/jsr305/2.0.1" rel="noreferrer">JSR-305</a> which FindBugs honors.</p> </blockquote> <p><strong><code>com/example/foo/package-info.java</code></strong></p> <pre><code>/** * Package that doesn't allow null values as method parameters. */ @ParametersAreNonnullByDefault package com.example.foo; import javax.annotation.ParametersAreNonnullByDefault; </code></pre> <p>For fields and method return values you'll need to create your own annotations. I did this by copying the source for <code>ParametersAreNonnullByDefault</code> and changing the <code>ElementType</code> enum.</p> <p><strong><code>com/example/util/FieldsAreNonnullByDefault.java</code></strong></p> <pre><code>package com.example.util; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import javax.annotation.Nonnull; import javax.annotation.meta.TypeQualifierDefault; /** * Applies the {@link Nonnull} annotation to every class field unless overridden. */ @Documented @Nonnull @TypeQualifierDefault(ElementType.FIELD) // &lt;-- use METHOD for return values @Retention(RetentionPolicy.RUNTIME) public @interface FieldsAreNonnullByDefault { // nothing to add } </code></pre> <p>I began rewriting a fairly complex system from scratch a couple months ago, and every package has these three annotations applied (fields, parameters, and return values). One benefit that's come out of the incentive to avoid <code>null</code> values is using the Null Object pattern where appropriate. That combined with favoring final fields as much as possible and small classes that do one thing only has really kept the code clean.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload