8

The documentation for @ParametersAreNonnullByDefault says, that:

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 ...

I don't consider a method's return type/value to be it's parameter. It is only part of its signature, so this is kind of ambiguous for me.

The Java tutorial for methods seems to think like me.


As Joachim Sauer pointed out for me in the comments section of his answer, the name @ParametersAreNonnullByDefault (parameters) should've clearly indicated for me that this annotation doesn't apply to methods' return types/values. I was blind! :) Thanks Joachim!

In light of this I can only says that an @EverythingIsNonnullByDefault should exist somwhere. :)

Community
  • 1
  • 1
Kohányi Róbert
  • 9,791
  • 4
  • 52
  • 81
  • So what's your reason to think that this annotation _would_ apply to return values? – David Z Oct 05 '11 at 08:06
  • I don't have a reason for why it would do this, instead I would like it to be doing this. (See my comment for [this answer](http://stackoverflow.com/questions/7658353/is-parametersarenonnullbydefault-applies-to-method-return-values-too/7658375#7658375).) – Kohányi Róbert Oct 05 '11 at 08:12

2 Answers2

12

No, @ParametersAreNonnullByDefault applies only to a method's parameters--the values it accepts from the caller (between the parentheses). The method is still free to return a null value.

Here's a class that combines all three places where you can apply @Nonnull, though in our code I still use three separate annotations, one of which is supplied by JSR-305.

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, class or method to indicate that all
 * class fields and method parameters and return values in that element are nonnull 
 * by default unless overridden.
 */
@Documented
@Nonnull
@TypeQualifierDefault({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface EverythingIsNonnullByDefault {
}
David Harkness
  • 35,992
  • 10
  • 112
  • 134
3

I don't see a reason why @ParametersAreNonnullByDefault should apply to return values.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • My reason it that `@Nonnull` can be applied to method return values too. At least the [documentation for FindBugs' `@NonNull` annotation](http://findbugs.sourceforge.net/manual/annotations.html) (which is almost, if not the same thing as `@Nonnull`) says just that. – Kohányi Róbert Oct 05 '11 at 08:11
  • @Psycho_Dad: you specify the nullity of a return type, yes. But that doesn't mean that **this** default annotation influences it if you don't set one. – Joachim Sauer Oct 05 '11 at 08:13
  • Sorry but I didn't understand you clearly. My question was if the annotation at hand applies to methods' return values too **if I set it**. I couldn't care less what it does if I don't set it. :) – Kohányi Róbert Oct 05 '11 at 08:21
  • By "don't set one" I meant "don't specify `@Nonnull@` or `@Nullable` on the return type explicitly". – Joachim Sauer Oct 05 '11 at 08:22
  • Yeah OK, but still I want to know that if I set `@ParametersAreNonnullByDefault` on a class would that implicitly apply a `@Nonnull` annotation on methods' return types or not. Because in my read applying it so implicitly applies `@Nonnull` on method parameters. – Kohányi Róbert Oct 05 '11 at 08:35
  • 2
    @Psycho_Dad: **why should it**? A return value is *not a parameter*. The documentation (and name!) of `@ParametersAreNonnullByDefault` clearly states that it applies to *parameters*. – Joachim Sauer Oct 05 '11 at 08:36
  • OK, I got it at last! :D I didn't really pay attention to the annotation's name. Thanks! – Kohányi Róbert Oct 05 '11 at 08:51