7

Google is failing me. There used to be this annotation: ReturnValuesAreNonnullByDefault.

But this is now deprecated and the javadoc does not indicate which new annotation to use. @Nonnull on the whole class does not apply to return values because I just tested that out and I get no warning for a method returning null. I don't want to have to specifically annotate every single return value, so is there a good option out there?

malana
  • 5,045
  • 3
  • 28
  • 41
Selena
  • 2,208
  • 8
  • 29
  • 49
  • In the package summary it says "This annotations are mostly deprecated and replaced by JSR 305 annotations defined in javax.annotation. ". However, I'm not sure which javax.annotation you should use as a replacement for ReturnValuesAreNonnullByDefault. http://findbugs.sourceforge.net/api/edu/umd/cs/findbugs/annotations/package-summary.html – Marco Jun 07 '16 at 13:11

2 Answers2

2

You can use this answer to build your own simple @EverythingIsNonnullByDefault annotation to apply at the package/class level to cover all cases, or this one which shows you how to create separate annotations to govern fields and method return values. We opted to use them all but tend to apply the "everything" version at the package level.

If you're in a real hurry, copy-n-paste the deprecated annotation and remove the deprecation.

package com.sample;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.annotation.meta.TypeQualifierDefault;

/**
 * This annotation can be applied to a package or class to indicate that the 
 * classes' methods in that element all return nonnull values by default
 * unless there is
 * <ul>
 *   <li>an explicit nullness annotation
 *   <li>a default method annotation applied to a more tightly nested element.
 * </ul>
 */
@Documented
@Nonnull
@TypeQualifierDefault(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ReturnValuesAreNonnullByDefault {
    // feel free to name it MethodsAreNonnullByDefault; I find that confusing
}
Community
  • 1
  • 1
David Harkness
  • 35,992
  • 10
  • 112
  • 134
2

If you are using the Checker Framework, then you can use @DefaultQualifier. For example, you could write

@DefaultQualifier(value=NonNull.class, locations=DefaultLocation.RETURNS)

However, you do not need to do this because the Checker Framework's Nullness Checker already uses that default. (As you have discovered, the best default is to assume that every method returns null.)

An advantage of the Nullness Checker is that it detects more null-pointer-related errors than FindBugs does.

The Nullness Checker is compatible with FindBugs's annotations, so you can try the Nullness Checker without having to change existing FindBugs annotations in your code.

mernst
  • 7,437
  • 30
  • 45
  • I was using Findbugs since that is what other teams at my company are using. I will look into the Checker framework since it has more bug finding power. Thanks for letting me know about it! – Selena Feb 05 '15 at 01:49
  • Ah, I see. The original question didn't specify FindBugs. Good luck! – mernst Feb 05 '15 at 10:18