简体   繁体   中英

How to Set a FindBugs Filter for fields with a specific Annotation?

I have the a bug reported by FindBugs but I know better :) see the following example:

public class MyClass extends BaseClass {

    @CustomInjection
    private Object someField;

    public MyClass() {
        super();
        someField.someMethod(); // Bug is here because FindsBugs thinks this is always null
    }
}

In my BaseClass constructor I inject all fields with the @CustomInjection annotation with the correct object so my annotated fields are not null in my case.

I don't want to suppress the warning with 'suppresswarnings' because that will clutter the code to much. I prefer making a filter like findbugs explains here , but i can't figure out how to filter bugs for fields annotated with a certain interface. I also don't want to filter all null bug warnings. I think it should be something like :

<Match>
  <Bug code="UR">  
  <Field annotation="CustomInjection">
</Match>

I've found a workaround to fix this problem. It seems detection for annotation based injection is hard coded in FindBugs, see this piece of source code:

public static boolean isInjectionAttribute(@DottedClassName String annotationClass) {
    if (annotationClass.startsWith("javax.annotation.")
            || annotationClass.startsWith("javax.ejb")
            || annotationClass.equals("org.apache.tapestry5.annotations.Persist")
            || annotationClass.equals("org.jboss.seam.annotations.In")
            || annotationClass.startsWith("javax.persistence")
            || annotationClass.endsWith("SpringBean")
            || annotationClass.equals("com.google.inject.Inject")
            || annotationClass.startsWith("com.google.") && annotationClass.endsWith(".Bind")
            && annotationClass.hashCode() == -243168318
            || annotationClass.startsWith("org.nuxeo.common.xmap.annotation")
            || annotationClass.startsWith("com.google.gwt.uibinder.client")
            || annotationClass.startsWith("org.springframework.beans.factory.annotation")
            || annotationClass.equals("javax.ws.rs.core.Context")) {
        return true;
    }
    int lastDot = annotationClass.lastIndexOf('.');
    String lastPart = annotationClass.substring(lastDot + 1);
    if (lastPart.startsWith("Inject")) {
        return true;
    }
    return false;
}

So annotations like @EJB are not shown as UR bugs but my annotation @CustomInjection will always be a UR bug.

But in the end of the function there is an escape for all annotationnames starting with "Inject", so if I rename @CustomInjection to @InjectCustom the UR bug disappears. So to avoid this problem just rename your annotation to @InjectSomething .

If you can't do it directly, write a pre-processor that detects the annotation and generates code that initializes the field with something that would make its uses look valid to FindBugs. Feed the pre-processor output to FindBugs, instead of the original source code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM