SpotBugs¶
Java | Home | Release Notes | Plugin
Info
SpotBugs is a successor project to deprecated FindBugs project.
Migration guide. If you were using custom
findbugs config before then rename it's folder to spotbugs
.
Warning
In contrast to other plugins, spotbugs plugin is not bundled with gradle,
but quality plugin will bring it as a dependency (v 2.0.1) and activate automatically.
To use newer spotbugs plugin version simply enable plugin manually (in plugins
section).
By default, plugin activates if java sources available (src/main/java
).
SpotBugs configuration differ from other tools (checkstyle, pmd): instead of exact rules configuration
it uses efforts level. Deeper level could reveal more bugs, but with higher mistake possibility.
Default settings (max
effort and medium
level) are perfect for most cases. Some checks were disabled in the default
filter file
Note
Special xsl file used for manual html report generation because spotbugs plugin could generate either xml or html report and not both.
Output¶
2 (0 / 2 / 0) SpotBugs violations were found in 2 files [Performance | URF_UNREAD_FIELD] sample.(Sample.java:8) [priority 2 / rank 14] >> Unread field: sample.Sample.sample This field is never read. Consider removing it from the class. ...
Counts in braces show priorities (p1/p2/p3).
Note
There is no link to spotbugs site (like other tools), because report already contains everything from there.
Tip
Both priority
and rank are shown for violations: [priority 2 / rank 14]
.
Priority relates to spotbugsLevel
setting and rank to spotbugsMaxRank
.
Config¶
Tool config options with defaults:
quality { spotbugsVersion = '3.1.12' spotbugs = true // false to disable automatic plugin activation spotbugsEffort = 'max' // min, less, more or max spotbugsLevel = 'medium' // low, medium, high spotbugsMaxRank = 20 // 1-4 scariest, 5-9 scary, 10-14 troubling, 15-20 of concern spotbugsMaxHeapSize = '1g' }
Attention
Gradle 5 reduced default memory settings and so default memory for
spotbugs task become 512mb
(instead of 1/4 of physical memory
as it was before).
To reduce the impact (as spotbugs task is memory-consuming), quality plugin sets now default
memory to 1g
. If your project requires more memory for spotbugs, increase it with spotbugsMaxHeapSize
option:
spotbugsMaxHeapSize='2g'
Note that quality pligin setting is applied only if sotbugs task was not configured manually, for example, with
spotbugsMain.maxHeapSize = '2g'
.
Suppress¶
To suppress violations you can use filter file. In this case you need to override default filter file.
Or you can use annotations. SpotBugs use custom annotations and so you need to add
com.github.spotbugs:spotbugs-annotations:3.1.2
dependency (with provided scope if possible) and use:
@SuppressFBWarnings("URF_UNREAD_FIELD")
Abstract
Spotbugs can't use default @SuppressWarnings
annotation because it's a source annotation
and not available in bytecode.
Plugins¶
You may add additional spotbugs checks by declaring spotbugs plugins.
Warning
As, by default, spotbugs plugin applied automatically after configuration read, spotbugsPlugins
configuration can't be used directly
You can register plugins using quality extension shortcut:
quality { spotbugsPlugin 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.10.0' spotbugsPlugin 'com.mebigfatguy.fb-contrib:fb-contrib:7.4.7' }
Note
Rules from plugins would be identified in console output:
[fb-contrib project | Correctness | FCBL_FIELD_COULD_BE_LOCAL] sample.(Sample.java:11) [priority 2 / rank 7] >> Class sample.Sample defines fields that are used only as locals This class defines fields that are used in a locals only fashion, specifically private fields or protected fields in final classes that are accessed first in each method with a store vs. a load. This field could be replaced by one or more local variables.
Alternatively, you can use afterEvaluate
to register directly in spotbugsPlugins
configuration:
afterEvaluate { dependencies { spotbugsPlugins 'com.mebigfatguy.fb-contrib:fb-contrib:7.4.7' } }
Or declare spotbugs plugin manually (it will be still configured by quality plugin)
and use spotbugsPlugins
configuration directly:
plugins { id 'com.github.spotbugs' version '2.0.1' } dependencies { spotbugsPlugins 'com.mebigfatguy.fb-contrib:fb-contrib:7.4.7' }
Tip
All these approaches could work together, but better stick to one.
Available plugins¶
quality { spotbugsPlugin 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.10.0' }
fb-contrib: A FindBugs auxiliary detector plugin (site)
qualtiy { spotbugsPlugin 'com.mebigfatguy.fb-contrib:fb-contrib:7.4.7' }
Annotations¶
Use spotbugs-annotations to guide spotbugs nullability checks (@NonNull
and @Nullable
).
Add com.github.spotbugs:spotbugs-annotations:3.1.2
dependency (with provided scope if possible).
Warning
Before, annotations from Jsr-305 were used
(com.google.code.findbugs:jsr305
), but now it is dead.
Remove jsr-305 jar if it were used and use undeprecated
@edu.umd.cs.findbugs.annotations.NonNull
and @edu.umd.cs.findbugs.annotations.Nullable
Pay attention becuase libraries still bring-in jsr-305 jar (e.g. guava does): do not use
javax.annotation.Nullable
because it may lead to split package problem on java9 and above
(not always)
Another alternative is chaker framework annotations:
org.checkerframework:checker-qual:3.0.0
. Guava already switched
to use them, so if you use it you may already have these annotations.
Using checker framework annotations should be preferable because it's on the track to community acceptance as default jsr-305 replacement. Besides, it's the only advanced java types system extension and validation tool.
Hint
Even if you will use other annotations, people using checker framework with your library would still benefit from your annotations because checker framework understands almost all of them.
Summary:
- If checker framework available (
org.checkerframework:checker-qual
) use it:org.checkerframework.checker.nullness.qual.Nullable
- Otherwise, use spotbugs-annotations (
com.github.spotbugs:spotbugs-annotations
):edu.umd.cs.findbugs.annotations.Nullable
- Avoid using jsr-305 directly (
com.google.code.findbugs:jsr305
):javax.annotation.Nullable
Example¶
Here is an example, which will force you to use nullability annotations.
When you use guava functions or predicates you may receive this:
[NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE] input must be nonnull but is marked as nullable
The reason for this is that guava use @Nullable
annotation, which is @Inherited
, so
even if you not set annotation on your own function or predicate it will still be visible.
The simplest workaround is to set @NonNull
annotation on your function or predicate:
public boolean apply(@NonNull final Object input) {
Hint
NP_METHOD_PARAMETER_TIGHTENS_ANNOTATION
check was disabled because it does not allow this workaround to work