且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

字段和方法的javax.annotation的FindBugs DefaultAnnotation的替代方法

更新时间:2023-12-04 16:04:29

据我所知没有.想要同一件事,我复制了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 {
}