Is @ParametersAreNonnullByDefault applies to method return values too?

一曲冷凌霜 提交于 2019-12-05 06:05:11

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

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 {
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!