Skip to content

Commit 0cb89f2

Browse files
authored
Merge pull request #1196 from JuditKnoll/choosevisitors
add support for SpotBugs' chooseVisitors parameter
2 parents 6ceadf8 + 8450561 commit 0cb89f2

File tree

5 files changed

+240
-0
lines changed

5 files changed

+240
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright 2005-2025 the original author or authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
invoker.goals = clean compile site --no-transfer-progress
18+
19+
# The expected result of the build, possible values are "success" (default) and "failure"
20+
invoker.buildResult = success

src/it/chooseVisitors/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2005-2025 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<parent>
26+
<groupId>spotbugs-maven-plugin.it</groupId>
27+
<artifactId>common</artifactId>
28+
<version>testing</version>
29+
<relativePath>../common.xml</relativePath>
30+
</parent>
31+
32+
<artifactId>chooseVisitors</artifactId>
33+
<name>chooseVisitors</name>
34+
35+
<reporting>
36+
<excludeDefaults>true</excludeDefaults>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-jxr-plugin</artifactId>
41+
<version>@jxr.plugin@</version>
42+
</plugin>
43+
<plugin>
44+
<groupId>com.github.spotbugs</groupId>
45+
<artifactId>spotbugs-maven-plugin</artifactId>
46+
<configuration>
47+
<xmlOutput>true</xmlOutput>
48+
<chooseVisitors>-FindDeadLocalStores,+TestASM,-UnreadFields</chooseVisitors>
49+
</configuration>
50+
</plugin>
51+
</plugins>
52+
</reporting>
53+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2005-2025 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<site name="${project.name}" xmlns="http://maven.apache.org/SITE/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/SITE/2.0.0 https://maven.apache.org/xsd/site-2.0.0.xsd">
21+
22+
<bannerLeft name="SpotBugs Maven Plugin" />
23+
24+
<skin>
25+
<groupId>org.apache.maven.skins</groupId>
26+
<artifactId>maven-fluido-skin</artifactId>
27+
<version>2.1.0</version>
28+
</skin>
29+
30+
<custom>
31+
<fluidoSkin>
32+
<topBarEnabled>false</topBarEnabled>
33+
<sideBarEnabled>false</sideBarEnabled>
34+
<breadcrumbDivider>»</breadcrumbDivider>
35+
<gitHub>
36+
<projectId>spotbugs/spotbugs-maven-plugin</projectId>
37+
<ribbonOrientation>right</ribbonOrientation>
38+
<ribbonColor>red</ribbonColor>
39+
</gitHub>
40+
<ohloh>
41+
<projectId>spotbugs-maven-plugin</projectId>
42+
<widget>thin-badge</widget>
43+
</ohloh>
44+
</fluidoSkin>
45+
</custom>
46+
47+
<publishDate format="yyyy-MM-dd" position="right"/>
48+
<version position="right"/>
49+
50+
</site>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2005-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import groovy.xml.XmlSlurper
18+
import groovy.xml.slurpersupport.GPathResult
19+
import groovy.xml.slurpersupport.NodeChild
20+
21+
import java.nio.file.Files
22+
import java.nio.file.Path
23+
24+
Path spotbugsHtml = basedir.toPath().resolve('target/site/spotbugs.html')
25+
assert Files.exists(spotbugsHtml)
26+
27+
Path spotbugsXdoc = basedir.toPath().resolve('target/spotbugs.xml')
28+
assert Files.exists(spotbugsXdoc)
29+
30+
Path spotbugsXml = basedir.toPath().resolve('target/spotbugsXml.xml')
31+
assert Files.exists(spotbugsXml)
32+
33+
println '******************'
34+
println 'Checking HTML file'
35+
println '******************'
36+
37+
String effortLevel = 'default'
38+
39+
assert spotbugsHtml.text.contains('<i>' + effortLevel + '</i>')
40+
41+
XmlSlurper xmlSlurper = new XmlSlurper()
42+
xmlSlurper.setFeature('http://apache.org/xml/features/disallow-doctype-decl', true)
43+
xmlSlurper.setFeature('http://apache.org/xml/features/nonvalidating/load-external-dtd', false)
44+
45+
// Temporarily allow DOCTYPE for HTML parsing
46+
xmlSlurper.setFeature('http://apache.org/xml/features/disallow-doctype-decl', false)
47+
GPathResult path = xmlSlurper.parse(spotbugsHtml)
48+
xmlSlurper.setFeature('http://apache.org/xml/features/disallow-doctype-decl', true)
49+
50+
int spotbugsErrors = path.body.'**'.find { NodeChild main -> main.@id == 'bodyColumn' }.section[1].table.tr[1].td[1].toInteger()
51+
println "Error Count is ${spotbugsErrors}"
52+
53+
println '*********************************'
54+
println 'Checking Spotbugs Native XML file'
55+
println '*********************************'
56+
57+
path = xmlSlurper.parse(spotbugsXml)
58+
59+
List<NodeChild> allNodes = path.depthFirst().toList()
60+
int spotbugsXmlErrors = allNodes.count { NodeChild node -> node.name() == 'BugInstance' }
61+
println "BugInstance size is ${spotbugsXmlErrors}"
62+
63+
assert spotbugsXmlErrors == spotbugsErrors
64+
65+
spotbugsXmlErrors = allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'URF_UNREAD_FIELD' }
66+
spotbugsXmlErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'UUF_UNUSED_FIELD'}
67+
spotbugsXmlErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'DLS_DEAD_LOCAL_STORE'}
68+
println "BugInstance size from detectors removed is ${spotbugsXmlErrors}"
69+
70+
assert 0 == spotbugsXmlErrors
71+
72+
spotbugsXmlErrors = allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'NM_METHOD_NAMING_CONVENTION'}
73+
spotbugsXmlErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'ICAST_INT_CAST_TO_DOUBLE_PASSED_TO_CEIL'}
74+
spotbugsXmlErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'NM_FIELD_NAMING_CONVENTION'}
75+
println "BugInstance size from detectors removed is ${spotbugsXmlErrors}"
76+
77+
assert 0 != spotbugsXmlErrors
78+
79+
println '******************'
80+
println 'Checking xDoc file'
81+
println '******************'
82+
83+
path = xmlSlurper.parse(spotbugsXdoc)
84+
85+
allNodes = path.depthFirst().toList()
86+
int xdocErrors = allNodes.count { NodeChild node -> node.name() == 'BugInstance' }
87+
println "BugInstance size is ${xdocErrors}"
88+
89+
assert xdocErrors == spotbugsErrors
90+
91+
xdocErrors = allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'URF_UNREAD_FIELD' }
92+
xdocErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'UUF_UNUSED_FIELD'}
93+
xdocErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'DLS_DEAD_LOCAL_STORE'}
94+
println "BugInstance size from detectors removed is ${xdocErrors}"
95+
96+
assert 0 == xdocErrors
97+
98+
xdocErrors = allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'NM_METHOD_NAMING_CONVENTION'}
99+
xdocErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'ICAST_INT_CAST_TO_DOUBLE_PASSED_TO_CEIL'}
100+
xdocErrors += allNodes.count { NodeChild node -> node.name() == 'BugInstance' && node.@type == 'NM_FIELD_NAMING_CONVENTION'}
101+
println "BugInstance size from detectors removed is ${xdocErrors}"
102+
103+
assert 0 != spotbugsXmlErrors

src/main/groovy/org/codehaus/mojo/spotbugs/SpotBugsMojo.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,14 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
340340
@Parameter(property = 'spotbugs.omitVisitors')
341341
String omitVisitors
342342

343+
/**
344+
* Selectively enable/disable detectors. This is a comma-delimited list with "+" or "-" before each detectors name indicated enabling or disabling.
345+
*
346+
* @since 4.9.4.2
347+
*/
348+
@Parameter(property = 'spotbugs.chooseVisitors')
349+
String chooseVisitors
350+
343351
/**
344352
* The plugin list to include in the report. This is a comma-delimited list.
345353
* <p>
@@ -886,6 +894,12 @@ class SpotBugsMojo extends AbstractMavenReport implements SpotBugsPluginsTrait {
886894
args << omitVisitors
887895
}
888896

897+
if (chooseVisitors) {
898+
log.debug(" Adding 'chooseVisitors'")
899+
args << '-chooseVisitors'
900+
args << chooseVisitors
901+
}
902+
889903
if (relaxed) {
890904
log.debug(" Adding 'relaxed'")
891905
args << '-relaxed'

0 commit comments

Comments
 (0)