-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathFindbugsConfiguration.java
395 lines (358 loc) · 15 KB
/
FindbugsConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* SonarQube Findbugs Plugin
* Copyright (C) 2012 SonarSource
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.plugins.findbugs;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.thoughtworks.xstream.XStream;
import edu.umd.cs.findbugs.Project;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.CharEncoding;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.PropertyType;
import org.sonar.api.Startable;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile.Type;
import org.sonar.api.batch.rule.ActiveRule;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.config.Configuration;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.SonarException;
import org.sonar.plugins.findbugs.rules.FindbugsRulesDefinition;
import org.sonar.plugins.findbugs.xml.Bug;
import org.sonar.plugins.findbugs.xml.FindBugsFilter;
import org.sonar.plugins.findbugs.xml.Match;
import org.sonar.plugins.java.api.JavaResourceLocator;
import static java.lang.String.format;
@ScannerSide
public class FindbugsConfiguration implements Startable {
private static final Logger LOG = LoggerFactory.getLogger(FindbugsConfiguration.class);
private final FileSystem fileSystem;
private final Configuration config;
private final ActiveRules activeRules;
private final JavaResourceLocator javaResourceLocator;
public FindbugsConfiguration(FileSystem fileSystem, Configuration config, ActiveRules activeRules,
JavaResourceLocator javaResourceLocator) {
this.fileSystem = fileSystem;
this.config = config;
this.activeRules = activeRules;
this.javaResourceLocator = javaResourceLocator;
}
public File getTargetXMLReport() {
return new File(fileSystem.workDir(), "findbugs-result.xml");
}
public Project getFindbugsProject() throws IOException {
Project findbugsProject = new Project();
List<File> classFilesToAnalyze = new ArrayList<>(javaResourceLocator.classFilesToAnalyze());
for (File file : javaResourceLocator.classpath()) {
//Will capture additional classes including precompiled JSP
if(file.isDirectory()) { // will include "/target/classes" and other non-standard folders
classFilesToAnalyze.addAll(scanForAdditionalClasses(file));
}
//Auxiliary dependencies
findbugsProject.addAuxClasspathEntry(file.getCanonicalPath());
}
boolean hasJspFiles = fileSystem.hasFiles(fileSystem.predicates().hasLanguage("jsp"));
boolean hasPrecompiledJsp = false;
for (File classToAnalyze : classFilesToAnalyze) {
String absolutePath = classToAnalyze.getCanonicalPath();
if(hasJspFiles && !hasPrecompiledJsp
&& (absolutePath.endsWith("_jsp.class") || //Jasper
absolutePath.contains("/jsp_servlet/")) //WebLogic
) {
hasPrecompiledJsp = true;
}
if(!"module-info.class".equals(classToAnalyze.getName())) {
findbugsProject.addFile(absolutePath);
}
}
if (classFilesToAnalyze.isEmpty()) {
LOG.warn("Findbugs needs sources to be compiled."
+ " Please build project before executing sonar or check the location of compiled classes to"
+ " make it possible for Findbugs to analyse your (sub)project ({}).", fileSystem.baseDir().getPath());
if (!isAllowUncompiledCode() && hasSourceFiles()) { //This excludes test source files
throw new IllegalStateException(format("One (sub)project contains Java source files that are not compiled (%s).",
fileSystem.baseDir().getPath()));
}
}
if (hasJspFiles && !hasPrecompiledJsp) {
LOG.warn("JSP files were found in the current (sub)project ({}) but FindBugs requires their precompiled form. " +
"For more information on how to configure JSP precompilation : https://github.com/find-sec-bugs/find-sec-bugs/wiki/JSP-precompilation",
fileSystem.baseDir().getPath());
}
copyLibs();
if (annotationsLib != null) {
// Findbugs dependencies are packaged by Maven. They are not available during execution of unit tests.
findbugsProject.addAuxClasspathEntry(annotationsLib.getCanonicalPath());
findbugsProject.addAuxClasspathEntry(jsr305Lib.getCanonicalPath());
}
findbugsProject.setCurrentWorkingDirectory(fileSystem.workDir());
return findbugsProject;
}
private void exportProfile(ActiveRules activeRules, Writer writer) {
try {
FindBugsFilter filter = buildFindbugsFilter(
activeRules.findAll().stream().filter(activeRule ->
{
String repKey = activeRule.ruleKey().repository();
return repKey.contains(FindbugsRulesDefinition.REPOSITORY_KEY) ||
repKey.contains("findsecbugs") ||
repKey.contains("fb-contrib");
})
.collect(Collectors.toList())
);
XStream xstream = FindBugsFilter.createXStream();
writer.append(xstream.toXML(filter));
} catch (IOException e) {
throw new SonarException("Fail to generate the Findbugs profile configuration", e);
}
}
private static FindBugsFilter buildFindbugsFilter(Iterable<ActiveRule> activeRules) {
FindBugsFilter root = new FindBugsFilter();
for (ActiveRule activeRule : activeRules) {
String repoKey = activeRule.ruleKey().repository();
if (repoKey.contains("findsecbugs") || repoKey.contains(FindbugsRulesDefinition.REPOSITORY_KEY) || repoKey.contains("fb-contrib")) {
Match child = new Match();
child.setBug(new Bug(activeRule.internalKey()));
root.addMatch(child);
}
}
return root;
}
/**
* Determine if the project has Java source files. This is used to determine if the project has no compiled classes on
* purpose or because the compilation was omit from the process.
* @return If at least one Java file is present
*/
private boolean hasSourceFiles() {
FilePredicates pred = fileSystem.predicates();
return fileSystem.hasFiles(
pred.and(
pred.hasType(Type.MAIN),
pred.or(FindbugsPlugin.getSupportedLanguagesFilePredicate(pred)),
//package-info.java will not generate any class files.
//See: https://github.com/SonarQubeCommunity/sonar-findbugs/issues/36
pred.not(pred.matchesPathPattern("**/package-info.java")),
pred.not(pred.matchesPathPattern("**/module-info.java")),
pred.not(pred.matchesPathPattern("**/*.jsp"))
)
);
}
@VisibleForTesting
File saveIncludeConfigXml() throws IOException {
StringWriter conf = new StringWriter();
exportProfile(activeRules, conf);
File file = new File(fileSystem.workDir(), "findbugs-include.xml");
FileUtils.write(file, conf.toString(), CharEncoding.UTF_8);
return file;
}
/**
* Scan the given folder for classes. It will catch classes from Java, JSP and more.
*
* @param folder Folder to scan
* @return List<File> of class files
* @throws IOException
*/
public static List<File> scanForAdditionalClasses(File folder) throws IOException {
List<File> allFiles = new ArrayList<File>();
Queue<File> dirs = new LinkedList<File>();
dirs.add(folder);
while (!dirs.isEmpty()) {
File dirPoll = dirs.poll();
if(dirPoll == null) break; //poll() result could be null if the queue is empty.
for (File f : dirPoll.listFiles()) {
if (f.isDirectory()) {
dirs.add(f);
} else if (f.isFile()&& f.getName().endsWith(".class")) {
allFiles.add(f);
}
}
}
return allFiles;
}
@VisibleForTesting
List<File> getExcludesFilters() {
List<File> result = Lists.newArrayList();
PathResolver pathResolver = new PathResolver();
String[] filters = config.getStringArray(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY);
for (String excludesFilterPath : filters) {
excludesFilterPath = StringUtils.trim(excludesFilterPath);
if (StringUtils.isNotBlank(excludesFilterPath)) {
result.add(pathResolver.relativeFile(fileSystem.baseDir(), excludesFilterPath));
}
}
return result;
}
public String getEffort() {
return StringUtils.lowerCase(
config.get(FindbugsConstants.EFFORT_PROPERTY)
.orElse(FindbugsConstants.EFFORT_DEFAULT_VALUE));
}
public String getConfidenceLevel() {
return StringUtils.lowerCase(
config.get(FindbugsConstants.CONFIDENCE_LEVEL_PROPERTY)
.orElse(FindbugsConstants.CONFIDENCE_LEVEL_DEFAULT_VALUE));
}
public long getTimeout() {
return config.getLong(FindbugsConstants.TIMEOUT_PROPERTY).orElse(FindbugsConstants.TIMEOUT_DEFAULT_VALUE);
}
public boolean isAllowUncompiledCode() {
return config.getBoolean(FindbugsConstants.ALLOW_UNCOMPILED_CODE).orElse(FindbugsConstants.ALLOW_UNCOMPILED_CODE_VALUE);
}
private File jsr305Lib;
private File annotationsLib;
private File fbContrib;
private File findSecBugs;
public void copyLibs() {
if (jsr305Lib == null) {
jsr305Lib = copyLib("/jsr305.jar");
}
if (annotationsLib == null) {
annotationsLib = copyLib("/annotations.jar");
}
if (fbContrib == null) {
fbContrib = copyLib("/sb-contrib.jar");
}
if (findSecBugs == null) {
findSecBugs = copyLib("/findsecbugs-plugin.jar");
}
}
@Override
public void start() {
// do nothing
}
/**
* Invoked by PicoContainer to remove temporary files.
*/
@SuppressWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE")
@Override
public void stop() {
if (jsr305Lib != null) {
jsr305Lib.delete();
}
if (annotationsLib != null) {
annotationsLib.delete();
}
if (fbContrib != null) {
fbContrib.delete();
}
if (findSecBugs != null) {
findSecBugs.delete();
}
}
private File copyLib(String name) {
InputStream input = null;
try {
input = getClass().getResourceAsStream(name);
File dir = new File(fileSystem.workDir(), "findbugs");
FileUtils.forceMkdir(dir);
File target = new File(dir, name);
FileUtils.copyInputStreamToFile(input, target);
return target;
} catch (IOException e) {
throw new IllegalStateException("Fail to extract Findbugs dependency", e);
} finally {
IOUtils.closeQuietly(input);
}
}
public File getFbContribJar() {
return fbContrib;
}
public File getFindSecBugsJar() {
return findSecBugs;
}
public static List<PropertyDefinition> getPropertyDefinitions() {
String subCategory = "FindBugs";
return ImmutableList.of(
PropertyDefinition.builder(FindbugsConstants.EFFORT_PROPERTY)
.defaultValue(FindbugsConstants.EFFORT_DEFAULT_VALUE)
.category(CoreProperties.CATEGORY_JAVA)
.subCategory(subCategory)
.name("Effort")
.description("Effort of the bug finders. Valid values are Min, Default and Max. Setting 'Max' increases precision but also increases " +
"memory consumption.")
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE)
.build(),
PropertyDefinition.builder(FindbugsConstants.TIMEOUT_PROPERTY)
.defaultValue(Long.toString(FindbugsConstants.TIMEOUT_DEFAULT_VALUE))
.category(CoreProperties.CATEGORY_JAVA)
.subCategory(subCategory)
.name("Timeout")
.description("Specifies the amount of time, in milliseconds, that FindBugs may run before it is assumed to be hung and is terminated. " +
"The default is 600,000 milliseconds, which is ten minutes.")
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE)
.type(PropertyType.INTEGER)
.build(),
PropertyDefinition.builder(FindbugsConstants.EXCLUDES_FILTERS_PROPERTY)
.category(CoreProperties.CATEGORY_JAVA)
.subCategory(subCategory)
.name("Excludes Filters")
.description("Paths to findbugs filter-files with exclusions.")
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE)
.multiValues(true)
.build(),
PropertyDefinition.builder(FindbugsConstants.CONFIDENCE_LEVEL_PROPERTY)
.defaultValue(FindbugsConstants.CONFIDENCE_LEVEL_DEFAULT_VALUE)
.category(CoreProperties.CATEGORY_JAVA)
.subCategory(subCategory)
.name("Confidence Level")
.description("Specifies the confidence threshold (previously called \"priority\") for reporting issues. If set to \"low\", confidence is not used to filter bugs. " +
"If set to \"medium\" (the default), low confidence issues are suppressed. If set to \"high\", only high confidence bugs are reported. ")
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE)
.build(),
PropertyDefinition.builder(FindbugsConstants.ALLOW_UNCOMPILED_CODE)
.defaultValue(Boolean.toString(FindbugsConstants.ALLOW_UNCOMPILED_CODE_VALUE))
.type(PropertyType.BOOLEAN)
.category(CoreProperties.CATEGORY_JAVA)
.subCategory(subCategory)
.name("Allow Uncompiled Code")
.description("Remove the compiled code requirement for all projects. "+
"It can lead to a false sense of security if the build process skips certain projects.")
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE)
.build(),
PropertyDefinition.builder(FindbugsConstants.REPORT_PATHS)
.category(CoreProperties.CATEGORY_JAVA)
.subCategory(subCategory)
.name("Report Paths")
.description("Relative path to SpotBugs report files intended to be reused. (<code>/target/findbugsXml.xml</code> and <code>/target/spotbugsXml.xml</code> are included by default)")
.onQualifiers(Qualifiers.PROJECT, Qualifiers.MODULE)
.multiValues(true)
.build()
);
}
}