ParametersAreNonnullByDefault

ParametersAreNonnullByDefault

本文介绍了字段和方法的javax.annotation的FindBugs DefaultAnnotation的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用

@DefaultAnnotation(NonNull.class)
package jobs;

import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;

但是不建议使用@ edu.umd.cs.findbugs.annotations.DefaultAnnotation注释: http://findbugs.sourceforge.net/api/edu /umd/cs/findbugs/annotations/DefaultAnnotation.html

however the annotation @edu.umd.cs.findbugs.annotations.DefaultAnnotation is deprecated:http://findbugs.sourceforge.net/api/edu/umd/cs/findbugs/annotations/DefaultAnnotation.html

他们建议使用javax.annotation.ParametersAreNonnullByDefault但是,DefaultAnnotation不仅针对参数,而且还针对字段和方法.

They propose to use javax.annotation.ParametersAreNonnullByDefaultHowever, DefaultAnnotation not only targets parameters, but also fields and methods.

那么,默认情况下将字段和方法设置为Nonnull的javax.annotation替代方法是什么?

So, what is the javax.annotation alternative for setting fields and methods to Nonnull by default?

推荐答案

据我所知没有.想要同一件事,我复制了ParametersAreNonnullByDefault 的> source转换为我自己的FieldsAreNonnullByDefaultMethodsAreNonnullByDefault,并更改了@TypeQualifierDefault值以匹配(分别为FIELDMETHOD). FindBugs完美地拾取了这些新注释.

As far as I know there is none. Wanting the same thing, I copied the source for ParametersAreNonnullByDefault into my own FieldsAreNonnullByDefault and MethodsAreNonnullByDefault and changed the @TypeQualifierDefault values to match (FIELD and METHOD respective). FindBugs picks up these new annotations perfectly.

这是FieldsAreNonnullByDefault的示例:

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' fields in that element are nonnull by default unless there is
 * <ul>
 *   <li>an explicit nullness annotation
 *   <li>a default field annotation applied to a more tightly nested element.
 * </ul>
 */
@Documented
@Nonnull
@TypeQualifierDefault(ElementType.FIELD)  // <-- METHOD for return values
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldsAreNonnullByDefault {
}

这篇关于字段和方法的javax.annotation的FindBugs DefaultAnnotation的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:06