Skip to content

Commit

Permalink
add spotbugs-annotations dependency automatically with compileOnly,
Browse files Browse the repository at this point in the history
add new configuration quality.spotbugsAnnotations, enabled by default
  • Loading branch information
xvik committed Feb 1, 2024
1 parent c39f5e0 commit 9bdedbf
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Update spotbugs plugin to 5.2.5 (#94)
- Remove spotbugsShowStackTraces option because it's not used by spotbugs anymore
- Custom xsl file is not used for html report - native html report generation used instead
- Add spotbugs-annotations dependency automatically with compileOnly (to simplify @SuppressFBWarnings usage)
Could be disabled with quality.spotbugsAnnotations = false configuration
* Update checkstyle 10.6.0 -> 10.12.7 (gradle metadata fix applied)
- Fix links to checkstyle site (site structure changed)
* Update spotbugs 4.7.3 -> 4.8.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ class QualityExtension {
*/
Set<String> spotbugsPlugins = []

/**
* Apply spotbugs annotations dependency with compileOnly scope. This dependency is required for
* suppression of warnings ({@code @SuppressFBWarnings}). Dependency version would be the same as
* used spotbugs version (as described in spotbugs plugin recommendation).
*/
boolean spotbugsAnnotations = true

/**
* Javac lint options to show compiler warnings, not visible by default.
* By default enables deprecation and unchecked options. Applies to all JavaCompile tasks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import javax.inject.Inject
* @see de.aaschmid.gradle.plugins.cpd.CpdPlugin
*/
@CompileStatic
@SuppressWarnings('ClassSize')
abstract class QualityPlugin implements Plugin<Project> {

public static final String TOOL_CHECKSTYLE = 'checkstyle'
Expand Down Expand Up @@ -265,6 +266,14 @@ abstract class QualityPlugin implements Plugin<Project> {
maxHeapSize.convention(extension.spotbugsMaxHeapSize)
}

// spotbugs annotations to simplify access to @SuppressFBWarnings
// (applied according to plugin recommendation)
if (extension.spotbugsAnnotations) {
dependencies {
compileOnly "com.github.spotbugs:spotbugs-annotations:${extension.spotbugsVersion}"
}
}

// plugins shortcut
extension.spotbugsPlugins?.each {
project.configurations.getByName('spotbugsPlugins').dependencies.add(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ru.vyarus.gradle.plugin.quality.tools.spotbugs

import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome
import ru.vyarus.gradle.plugin.quality.AbstractKitTest

/**
* @author Vyacheslav Rusakov
* @since 01.02.2024
*/
class SpotbugsAnnotationsAppliedKitTest extends AbstractKitTest {

def "Check spotbugs annotations applied"() {
setup:
build("""
plugins {
id 'java'
id 'ru.vyarus.quality'
}
quality {
checkstyle false
pmd false
strict false
}
repositories { mavenCentral() }
""")

fileFromClasspath('src/main/java/sample/SBSuppressSample.java', '/ru/vyarus/gradle/plugin/quality/java/sample/SBSuppressSample.java')

when: "run check task with java sources"
BuildResult result = run('check')

then: "all plugins detect violations"
result.task(":check").outcome == TaskOutcome.SUCCESS
result.output.contains('SpotBugs violations were found')
result.output.contains('1 (0 / 1 / 0) SpotBugs violations were found in 1 files')
!result.output.contains('NP_BOOLEAN_RETURN_NULL')
result.output.contains('URF_UNREAD_FIELD')
}

def "Check spotbugs annotations not applied"() {
setup:
build("""
plugins {
id 'java'
id 'ru.vyarus.quality'
}
quality {
checkstyle false
pmd false
strict false
spotbugsAnnotations false
}
repositories { mavenCentral() }
""")

fileFromClasspath('src/main/java/sample/SBSuppressSample.java', '/ru/vyarus/gradle/plugin/quality/java/sample/SBSuppressSample.java')

when: "run check task with java sources"
BuildResult result = runFailed('compileJava')

then: "all plugins detect violations"
result.task(":compileJava").outcome == TaskOutcome.FAILED
result.output.contains('src/main/java/sample/SBSuppressSample.java:8: error: cannot find symbol')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sample;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.ArrayDeque;
import java.util.Deque;

@SuppressFBWarnings("NP_BOOLEAN_RETURN_NULL")
public class SBSuppressSample {

private String sample;

public SBSuppressSample(String sample) {
this.sample = sample;
}

public static void main(String[] args) {
final Deque res = new ArrayDeque();
System.out.println(res);
}

public Boolean someting() {
return null;
}
}

0 comments on commit 9bdedbf

Please sign in to comment.