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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With