I want to create custom annotations to suppress individual FindBugs warnings, to make them easier to use with code completion. For example, it ignores constructors that do not set all @Nonnull fields.
@TypeQualifierDefault(ElementType.CONSTRUCTOR) @SuppressFBWarnings("NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR") @Retention(RetentionPolicy.CLASS) public @interface SuppressNonnullFieldNotInitializedWarning { }
However, I still see a warning when using annotations.
public class User { @Nonnull private String name; @SuppressNonnullFieldNotInitializedWarning public User() {
I tried different storage policies and item types, putting annotation in the constructor and class, and even @TypeQualifierNickname .
The same template works to apply @Nonnull to all fields of a class.
@Nonnull @TypeQualifierDefault(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface FieldsAreNonnullByDefault { }
FindBugs correctly displays a warning for code that assigns a null name .
@FieldsAreNonnullByDefault public class User { private String name; public UserModel() { name = null;
I believe the problem is that @SuppressFBWarnings not tagged @TypeQualifier , but @Nonnull is, and thus @TypeQualifierDefault and @TypeQualifierNickname do not apply to it. But there must be some other mechanism for applying one annotation using another.
java annotations findbugs
David harkness
source share