-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Pmd.java
410 lines (376 loc) · 13.3 KB
/
Pmd.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.plugins.quality;
import groovy.lang.Closure;
import groovy.lang.DelegatesTo;
import org.gradle.api.Action;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.quality.internal.PmdAction;
import org.gradle.api.plugins.quality.internal.PmdActionParameters;
import org.gradle.api.plugins.quality.internal.PmdReportsImpl;
import org.gradle.api.provider.Property;
import org.gradle.api.reporting.Reporting;
import org.gradle.api.resources.TextResource;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.LocalState;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.gradle.internal.nativeintegration.console.ConsoleDetector;
import org.gradle.internal.nativeintegration.console.ConsoleMetaData;
import org.gradle.internal.nativeintegration.services.NativeServices;
import org.gradle.util.internal.ClosureBackedAction;
import org.gradle.workers.WorkQueue;
import javax.annotation.Nullable;
import java.io.File;
import java.util.List;
import java.util.stream.Collectors;
/**
* Runs a set of static code analysis rules on Java source code files and generates a report of problems found.
*
* @see PmdPlugin
* @see PmdExtension
*/
@CacheableTask
public abstract class Pmd extends AbstractCodeQualityTask implements Reporting<PmdReports> {
private FileCollection pmdClasspath;
private List<String> ruleSets;
private TargetJdk targetJdk;
private TextResource ruleSetConfig;
private FileCollection ruleSetFiles;
private final PmdReports reports;
private boolean consoleOutput;
private FileCollection classpath;
private final Property<Integer> rulesMinimumPriority;
private final Property<Integer> maxFailures;
private final Property<Boolean> incrementalAnalysis;
private final Property<Integer> threads;
public Pmd() {
super();
ObjectFactory objects = getObjectFactory();
reports = objects.newInstance(PmdReportsImpl.class, this);
this.rulesMinimumPriority = objects.property(Integer.class);
this.incrementalAnalysis = objects.property(Boolean.class);
this.maxFailures = objects.property(Integer.class);
this.threads = objects.property(Integer.class);
}
@TaskAction
public void run() {
validate(rulesMinimumPriority.get());
validateThreads(threads.get());
WorkQueue workQueue = getWorkerExecutor().processIsolation(spec -> configureForkOptions(spec.getForkOptions()));
workQueue.submit(PmdAction.class, this::setupParameters);
}
private void setupParameters(PmdActionParameters parameters) {
parameters.getAntLibraryClasspath().setFrom(getPmdClasspath());
parameters.getPmdClasspath().setFrom(getPmdClasspath());
parameters.getTargetJdk().set(getTargetJdk());
parameters.getRuleSets().set(getRuleSets());
parameters.getRuleSetConfigFiles().from(getRuleSetFiles());
if (getRuleSetConfig() != null) {
parameters.getRuleSetConfigFiles().from(getRuleSetConfig().asFile());
}
parameters.getIgnoreFailures().set(getIgnoreFailures());
parameters.getConsoleOutput().set(isConsoleOutput());
parameters.getStdOutIsAttachedToTerminal().set(stdOutIsAttachedToTerminal());
if (getClasspath() != null) {
parameters.getAuxClasspath().setFrom(getClasspath());
}
parameters.getRulesMinimumPriority().set(getRulesMinimumPriority());
parameters.getMaxFailures().set(getMaxFailures());
parameters.getIncrementalAnalysis().set(getIncrementalAnalysis());
parameters.getIncrementalCacheFile().set(getIncrementalCacheFile());
parameters.getThreads().set(getThreads());
parameters.getSource().setFrom(getSource());
parameters.getEnabledReports().set(getReports().getEnabled().stream().map(report -> {
PmdActionParameters.EnabledReport newReport = getObjectFactory().newInstance(PmdActionParameters.EnabledReport.class);
newReport.getName().set(report.getName());
newReport.getOutputLocation().set(report.getOutputLocation());
return newReport;
}).collect(Collectors.toList()));
}
public boolean stdOutIsAttachedToTerminal() {
try {
ConsoleDetector consoleDetector = NativeServices.getInstance().get(ConsoleDetector.class);
ConsoleMetaData consoleMetaData = consoleDetector.getConsole();
return consoleMetaData != null && consoleMetaData.isStdOut();
} catch (RuntimeException e) {
return false;
}
}
/**
* Configures the reports to be generated by this task.
*/
@Override
@SuppressWarnings("rawtypes")
public PmdReports reports(@DelegatesTo(value = PmdReports.class, strategy = Closure.DELEGATE_FIRST) Closure closure) {
return reports(new ClosureBackedAction<>(closure));
}
/**
* Configures the reports to be generated by this task.
*
* @since 3.0
*/
@Override
public PmdReports reports(Action<? super PmdReports> configureAction) {
configureAction.execute(reports);
return reports;
}
/**
* Validates the value is a valid PMD rules minimum priority (1-5)
*
* @param value rules minimum priority threshold
*/
public static void validate(int value) {
if (value > 5 || value < 1) {
throw new InvalidUserDataException(String.format("Invalid rulesMinimumPriority '%d'. Valid range 1 (highest) to 5 (lowest).", value));
}
}
/**
* Validates the number of threads used by PMD.
*
* @param value the number of threads used by PMD
*/
private static void validateThreads(int value) {
if (value < 0) {
throw new InvalidUserDataException(String.format("Invalid number of threads '%d'. Number should not be negative.", value));
}
}
/**
* {@inheritDoc}
*/
@Override
@PathSensitive(PathSensitivity.RELATIVE)
public FileTree getSource() {
return super.getSource();
}
/**
* The class path containing the PMD library to be used.
*/
@Classpath
public FileCollection getPmdClasspath() {
return pmdClasspath;
}
/**
* The class path containing the PMD library to be used.
*/
public void setPmdClasspath(FileCollection pmdClasspath) {
this.pmdClasspath = pmdClasspath;
}
/**
* The built-in rule sets to be used. See the <a href="https://docs.pmd-code.org/pmd-doc-6.55.0/pmd_rules_java.html">official list</a> of built-in rule sets.
*
* <pre>
* ruleSets = ["basic", "braces"]
* </pre>
*/
@Input
public List<String> getRuleSets() {
return ruleSets;
}
/**
* The built-in rule sets to be used. See the <a href="https://docs.pmd-code.org/pmd-doc-6.55.0/pmd_rules_java.html">official list</a> of built-in rule sets.
*
* <pre>
* ruleSets = ["basic", "braces"]
* </pre>
*/
public void setRuleSets(List<String> ruleSets) {
this.ruleSets = ruleSets;
}
/**
* The target JDK to use with PMD.
*/
@Input
public TargetJdk getTargetJdk() {
return targetJdk;
}
/**
* The target JDK to use with PMD.
*/
public void setTargetJdk(TargetJdk targetJdk) {
this.targetJdk = targetJdk;
}
/**
* The custom rule set to be used (if any). Replaces {@code ruleSetFiles}, except that it does not currently support multiple rule sets.
*
* See the <a href="https://docs.pmd-code.org/pmd-doc-6.55.0/pmd_userdocs_making_rulesets.html">official documentation</a> for how to author a rule set.
*
* <pre>
* ruleSetConfig = resources.text.fromFile(resources.file("config/pmd/myRuleSets.xml"))
* </pre>
*
* @since 2.2
*/
@Nullable
@Optional
@Nested
public TextResource getRuleSetConfig() {
return ruleSetConfig;
}
/**
* The custom rule set to be used (if any). Replaces {@code ruleSetFiles}, except that it does not currently support multiple rule sets.
*
* See the <a href="https://docs.pmd-code.org/pmd-doc-6.55.0/pmd_userdocs_making_rulesets.html">official documentation</a> for how to author a rule set.
*
* <pre>
* ruleSetConfig = resources.text.fromFile(resources.file("config/pmd/myRuleSets.xml"))
* </pre>
*
* @since 2.2
*/
public void setRuleSetConfig(@Nullable TextResource ruleSetConfig) {
this.ruleSetConfig = ruleSetConfig;
}
/**
* The custom rule set files to be used. See the <a href="https://docs.pmd-code.org/pmd-doc-6.55.0/pmd_userdocs_making_rulesets.html">official documentation</a> for how to author a rule set file.
* If you want to only use custom rule sets, you must clear {@code ruleSets}.
*
* <pre>
* ruleSetFiles = files("config/pmd/myRuleSet.xml")
* </pre>
*/
@InputFiles
@PathSensitive(PathSensitivity.NONE)
public FileCollection getRuleSetFiles() {
return ruleSetFiles;
}
/**
* The custom rule set files to be used. See the <a href="https://docs.pmd-code.org/pmd-doc-6.55.0/pmd_userdocs_making_rulesets.html">official documentation</a> for how to author a rule set file.
* This adds to the default rule sets defined by {@link #getRuleSets()}.
*
* <pre>
* ruleSetFiles = files("config/pmd/myRuleSets.xml")
* </pre>
*/
public void setRuleSetFiles(FileCollection ruleSetFiles) {
this.ruleSetFiles = ruleSetFiles;
}
/**
* The reports to be generated by this task.
*/
@Override
@Nested
public final PmdReports getReports() {
return reports;
}
/**
* The maximum number of failures to allow before stopping the build.
*
* Defaults to 0, which will stop the build on any failure. Values 0 and
* above are valid. If <pre>ignoreFailures</pre> is set, this is ignored
* and the build will continue (infinite failures allowed).
*
* @since 6.4
*/
@Input
public Property<Integer> getMaxFailures() {
return maxFailures;
}
/**
* Specifies the rule priority threshold.
*
* @see PmdExtension#getRulesMinimumPriority()
* @since 6.8
*/
@Input
public Property<Integer> getRulesMinimumPriority() {
return rulesMinimumPriority;
}
/**
* Whether or not to write PMD results to {@code System.out}.
*
* @since 2.1
*/
@Input
public boolean isConsoleOutput() {
return consoleOutput;
}
/**
* Whether or not to write PMD results to {@code System.out}.
*
* @since 2.1
*/
public void setConsoleOutput(boolean consoleOutput) {
this.consoleOutput = consoleOutput;
}
/**
* Compile class path for the classes to be analyzed.
*
* The classes on this class path are used during analysis but aren't analyzed themselves.
*
* This is only well supported for PMD 5.2.1 or better.
*
* @since 2.8
*/
@Nullable
@Optional
@Classpath
public FileCollection getClasspath() {
return classpath;
}
/**
* Compile class path for the classes to be analyzed.
*
* The classes on this class path are used during analysis but aren't analyzed themselves.
*
* This is only well supported for PMD 5.2.1 or better.
*
* @since 2.8
*/
public void setClasspath(@Nullable FileCollection classpath) {
this.classpath = classpath;
}
/**
* Controls whether to use incremental analysis or not.
*
* This is only supported for PMD 6.0.0 or better. See <a href="https://docs.pmd-code.org/pmd-doc-6.55.0/pmd_userdocs_incremental_analysis.html"></a> for more details.
*
* @since 5.6
*/
@Internal
public Property<Boolean> getIncrementalAnalysis() {
return incrementalAnalysis;
}
/**
* Path to the incremental cache file, if incremental analysis is used.
*
* @since 5.6
*/
@LocalState
public File getIncrementalCacheFile() {
return new File(getTemporaryDir(), "incremental.cache");
}
/**
* Specifies the number of threads used by PMD.
*
* @see PmdExtension#getThreads()
* @since 7.5
*/
@Input
public Property<Integer> getThreads() {
return threads;
}
}