Skip to content

Commit 00548b3

Browse files
committed
lint w/Spotbugs
1 parent 5aa0010 commit 00548b3

4 files changed

Lines changed: 110 additions & 28 deletions

File tree

build.gradle

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import com.github.spotbugs.snom.SpotBugsTask
2+
13
plugins {
24
id 'war'
35
id "com.diffplug.spotless" version "7.2.1"
46
id 'pmd'
7+
id 'com.github.spotbugs' version '6.5.6'
58
}
69
description = 'Application Registry Manager app'
710
group = 'org.jlab'
@@ -67,4 +70,32 @@ pmd {
6770
// category/java/performance.xml
6871
// category/java/multithreading.xml
6972
// category/java/design.xml
73+
}
74+
spotbugs {
75+
excludeFilter = file("spotbugs-exclude.xml")
76+
showProgress = true
77+
78+
// Allows the build to proceed to custom conditional throw
79+
ignoreFailures = true
80+
}
81+
tasks.spotbugsMain {
82+
reports.create("html") {
83+
required = true
84+
outputLocation = file("$buildDir/reports/spotbugs.html")
85+
stylesheet = "fancy-hist.xsl"
86+
}
87+
reports.create("text") {
88+
required = true
89+
outputLocation = file("$buildDir/reports/spotbugs.text")
90+
}
91+
}
92+
tasks.withType(SpotBugsTask).configureEach {
93+
doLast {
94+
def reportFile = reports.text.outputLocation.asFile.get()
95+
if (reportFile.exists() && reportFile.length() > 0) {
96+
logger.lifecycle("\n[SpotBugs Console Stream]\n" + reportFile.text)
97+
// Manually break the build here so fails AFTER logging to stdout for CI
98+
throw new GradleException("SpotBugs violations found. Check the console log above.")
99+
}
100+
}
70101
}

spotbugs-exclude.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<FindBugsFilter>
3+
<Match>
4+
<!-- @EJB Injected false positive -->
5+
<Bug pattern="SE_BAD_FIELD" />
6+
<Or>
7+
<Class name="org.jlab.sim.presentation.controller.ajax.AddSoftware"/>
8+
<Class name="org.jlab.sim.presentation.controller.ajax.EditSoftware"/>
9+
<Class name="org.jlab.sim.presentation.controller.ajax.EditSoftwareRisk"/>
10+
<Class name="org.jlab.sim.presentation.controller.ajax.RemoveSoftware"/>
11+
<Class name="org.jlab.sim.presentation.controller.Directory"/>
12+
<Class name="org.jlab.sim.presentation.controller.Repositories"/>
13+
<Class name="org.jlab.sim.presentation.controller.Sync"/>
14+
</Or>
15+
</Match>
16+
<Match>
17+
<!-- @Entity objects composed of other entities false positive -->
18+
<Or>
19+
<Bug pattern="EI_EXPOSE_REP" />
20+
<Bug pattern="EI_EXPOSE_REP2" />
21+
</Or>
22+
<Or>
23+
<Class name="org.jlab.sim.persistence.entity.Software"/>
24+
<Class name="org.jlab.sim.persistence.entity.SoftwareTopic"/>
25+
</Or>
26+
</Match>
27+
</FindBugsFilter>

src/main/java/org/jlab/sim/business/service/SyncService.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ private PaginatedResult paginatedGitLabQuery(String url, Repository repository)
156156
throw new UserFriendlyException("Could not parse JSON", e);
157157
}
158158
} else {
159-
throw new UserFriendlyException("Request failed with status code: " + response.statusCode());
159+
throw new UserFriendlyException(
160+
"Request failed with status code: "
161+
+ (response == null ? "NONE" : response.statusCode()));
160162
}
161163

162164
return new PaginatedResult(softwareList, nextUrl);
@@ -294,7 +296,9 @@ private PaginatedResult paginatedGitHubQuery(String url, Repository repository)
294296
throw new UserFriendlyException("Could not parse JSON", e);
295297
}
296298
} else {
297-
throw new UserFriendlyException("Request failed with status code: " + response.statusCode());
299+
throw new UserFriendlyException(
300+
"Request failed with status code: "
301+
+ (response == null ? "NONE" : response.statusCode()));
298302
}
299303

300304
return new PaginatedResult(softwareList, nextUrl);
@@ -317,16 +321,18 @@ private List<Software> fetchGitHub(Repository repository) throws UserFriendlyExc
317321
}
318322

319323
private SoftwareType getFromTopicList(List<String> topicList) {
320-
SoftwareType type = SoftwareType.APP;
324+
SoftwareType type;
321325

322326
if (topicList.contains("app")) {
323-
// break out of if/else
327+
type = SoftwareType.APP;
324328
} else if (topicList.contains("lib")) {
325329
type = SoftwareType.LIB;
326330
} else if (topicList.contains("script")) {
327331
type = SoftwareType.SCRIPT;
328332
} else if (topicList.contains("plugin")) {
329333
type = SoftwareType.PLUGIN;
334+
} else {
335+
throw new IllegalArgumentException("Unknown type");
330336
}
331337

332338
return type;
@@ -351,10 +357,18 @@ private List<Software> fetchCSUE(Repository repository) throws UserFriendlyExcep
351357

352358
final int NUM_COLUMNS = 5;
353359
if (cells.size() == NUM_COLUMNS) {
354-
Element a = cells.first().select("a").first();
355-
String name = a.text();
356-
Attributes attributes = a.attributes();
357-
String homeUrl = BASE_URL + attributes.get("href");
360+
String name = "";
361+
String homeUrl = "";
362+
Element first = cells.first();
363+
if (first != null) {
364+
Element a = first.select("a").first();
365+
if (a != null) {
366+
name = a.text();
367+
Attributes attributes = a.attributes();
368+
homeUrl = BASE_URL + attributes.get("href");
369+
}
370+
}
371+
358372
String maintainerUsernameCsv = cells.get(3).text();
359373
String description = cells.get(4).text();
360374

@@ -410,10 +424,17 @@ private List<Software> fetchCertifiedType(Repository repository, String param, S
410424

411425
final int NUM_COLUMNS = 2;
412426
if (cells.size() == NUM_COLUMNS) {
413-
Element a = cells.first().select("a").first();
414-
String name = a.text();
415-
Attributes attributes = a.attributes();
416-
String homeUrl = BASE_URL + attributes.get("href");
427+
String name = "";
428+
String homeUrl = "";
429+
Element first = cells.first();
430+
if (first != null) {
431+
Element a = first.select("a").first();
432+
if (a != null) {
433+
name = a.text();
434+
Attributes attributes = a.attributes();
435+
homeUrl = BASE_URL + attributes.get("href");
436+
}
437+
}
417438
String maintainerUsernameCsv = null;
418439
String description = cells.get(1).text();
419440

@@ -480,7 +501,9 @@ private List<Software> fetchLLAPP(Repository repository) throws UserFriendlyExce
480501
}
481502
}
482503
} else {
483-
throw new UserFriendlyException("Request failed with status code: " + response.statusCode());
504+
throw new UserFriendlyException(
505+
"Request failed with status code: "
506+
+ (response == null ? "NONE" : response.statusCode()));
484507
}
485508

486509
return softwareList;

src/main/java/org/jlab/sim/presentation/controller/Directory.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private String createSelectionMessage(
138138
Include includeArchived) {
139139
DecimalFormat formatter = new DecimalFormat("###,###");
140140

141-
String selectionMessage = "All Software ";
141+
StringBuilder selectionMessage = new StringBuilder("All Software ");
142142

143143
List<String> filters = new ArrayList<>();
144144

@@ -167,29 +167,30 @@ private String createSelectionMessage(
167167
}
168168

169169
if (!filters.isEmpty()) {
170-
selectionMessage = filters.get(0);
170+
selectionMessage = new StringBuilder(filters.get(0));
171171

172172
for (int i = 1; i < filters.size(); i++) {
173173
String filter = filters.get(i);
174-
selectionMessage += " and " + filter;
174+
selectionMessage.append(" and ").append(filter);
175175
}
176176
}
177177

178178
if (paginator.getTotalRecords() < paginator.getMaxPerPage() && paginator.getOffset() == 0) {
179-
selectionMessage =
180-
selectionMessage + " {" + formatter.format(paginator.getTotalRecords()) + "}";
179+
selectionMessage
180+
.append(" {")
181+
.append(formatter.format(paginator.getTotalRecords()))
182+
.append("}");
181183
} else {
182-
selectionMessage =
183-
selectionMessage
184-
+ " {"
185-
+ formatter.format(paginator.getStartNumber())
186-
+ " - "
187-
+ formatter.format(paginator.getEndNumber())
188-
+ " of "
189-
+ formatter.format(paginator.getTotalRecords())
190-
+ "}";
184+
selectionMessage
185+
.append(" {")
186+
.append(formatter.format(paginator.getStartNumber()))
187+
.append(" - ")
188+
.append(formatter.format(paginator.getEndNumber()))
189+
.append(" of ")
190+
.append(formatter.format(paginator.getTotalRecords()))
191+
.append("}");
191192
}
192193

193-
return selectionMessage;
194+
return selectionMessage.toString();
194195
}
195196
}

0 commit comments

Comments
 (0)