views:

99

answers:

1

Hello!

Note: those annotations, I'm talking about, are specified by JSR305.

I have the latest Findbugs (1.3.9) and it finds errors correctly when some field, annotated with @Nonnull, is assigned to null.

But, in my project, the "non-null logic" is the default case. I would say that null is explicitely allowed only in 5% of cases.

So, it would be very inconvenient to annotate 95% of fields with @Nonnull. I would rather prefer to annotate those 5% of fields with @Nullable.

I tried to annotate a whole package with @Nonnull, it doesn't change anything.

So, it is somehow possible to specify the default logic?

+1  A: 

I am not sure if Fiundbug can deal with the following annotation, but if you want to annotate a all package with "NonNull", you may want to use:

@ParametersAreNonnullByDefault

/**
 * This annotation can be applied to a package, class or method to indicate that
 * the method parameters in that element are nonnull by default unless there is:
 * <ul>
 * <li>An explicit nullness annotation
 * <li>The method overrides a method in a superclass (in which case the
 * annotation of the corresponding parameter in the superclass applies)
 * <li> there is a default parameter annotation applied to a more tightly nested
 * element.
 * </ul>
 * 
 */
@Documented
@Nonnull
@TypeQualifierDefault(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface ParametersAreNonnullByDefault {
}

See also this article.

Note: at least that annotation is present in some FindBugs test cases.

VonC
Thank you, tried it, Findbugs ignores it (on types AND on packages). Must be a bug in Findbugs ;) JSR305 is inactive anyway. Perhaps I should look around for other solutions.
ivan_ivanovich_ivanoff