Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I tell findbugs to ignore classes I am unable to add?

Tags:

java

findbugs

Findbugs reports this:

findbugs:
 [findbugs] Executing findbugs from ant task
 [findbugs] Running FindBugs...
 [findbugs] The following classes needed for analysis were missing:
 [findbugs]   com.company.OptionalClass
 [findbugs] Warnings generated: 11
 [findbugs] Missing classes: 2
 [findbugs] Calculating exit code...
 [findbugs] Setting 'missing class' flag (2)
 [findbugs] Setting 'bugs found' flag (1)
 [findbugs] Exit code set to: 3
 [findbugs] Java Result: 3
 [findbugs] Classes needed for analysis were missing
 [findbugs] Output saved to findbugs.xml
BUILD SUCCESSFUL

This OptionalClass is referenced from a third-party jar for which I do not have the source code, and for which I do not want a findbugs analysis. It refers to a class I do not have in my classpath, or anywhere else. This class is probably used in certain cases, when our third-party jar is configured in a certain way.

Is there a way to tell findbugs to ignore this class?

Note that it completes the analysis and produces a findbugs.xml report, so this is a minor issue.

like image 994
Nicolas Avatar asked Jul 30 '10 17:07

Nicolas


People also ask

What is FindBugs used for?

FindBugs is an open-source static code analyser created by Bill Pugh and David Hovemeyer which detects possible bugs in Java programs. Potential errors are classified in four ranks: (i) scariest, (ii) scary, (iii) troubling and (iv) of concern. This is a hint to the developer about their possible impact or severity.

How do I stop Spotbugs warnings?

To suppress violations you can use filter file. In this case you need to override default filter file. Spotbugs can't use default @SuppressWarnings annotation because it's a source annotation and not available in bytecode.

How to run FindBugs?

The preferred method of running FindBugs is to directly execute $FINDBUGS_HOME /lib/findbugs. jar using the -jar command line switch of the JVM (java) executable. (Versions of FindBugs prior to 1.3. 5 required a wrapper script to invoke FindBugs.)


1 Answers

This message appears because somewhere in your binary classes you are referencing that class. It does not necessarily mean that FindBugs was trying to analyse com.company.OptionalClass to report on it; rather it is more likely FindBugs was analysing it to get more information about the class which references it.

Consider some of your source code, which you do want analysed:

package my.actual.codebase;
import com.some.third.party.stuff.OptionalClass;

class ReferencesOptionalClass extends OptionalClass {}

This kind of example is common with test classes, which reference e.g. org.junit.@Test but the JUnit jar is not included in what you want to analyse.

In the above example you do want results for ReferenceOptionalClass, but in order to get them, some parts of FindBugs need to look at OptionalClass. If it can't do that, because it's not there, it reports that the class is missing[1]. When FindBugs can't find that class, it generally just aborts the specific kind of analysis it was trying to do, and carries on with the rest of the analysis without problem.

The simple answer is if you are happy with the level of analysis FindBugs is giving you, then just ignore the error.

[1] Note that it's likely that this could even be a transitive reference i.e. your code includes class A, which references B in an auxiliary JAR, which then references OptionalClass.

like image 142
Grundlefleck Avatar answered Sep 28 '22 06:09

Grundlefleck