-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Checkstyle.java
467 lines (426 loc) · 14.7 KB
/
Checkstyle.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* Copyright 2016 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.Incubating;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.internal.project.IsolatedAntBuilder;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.quality.internal.CheckstyleAction;
import org.gradle.api.plugins.quality.internal.CheckstyleActionParameters;
import org.gradle.api.plugins.quality.internal.CheckstyleReportsImpl;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
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.Console;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
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.SourceTask;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.VerificationTask;
import org.gradle.jvm.toolchain.JavaLauncher;
import org.gradle.jvm.toolchain.JavaToolchainService;
import org.gradle.jvm.toolchain.internal.CurrentJvmToolchainSpec;
import org.gradle.util.internal.ClosureBackedAction;
import org.gradle.workers.WorkQueue;
import org.gradle.workers.WorkerExecutor;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Runs Checkstyle against some source files.
*/
@CacheableTask
public class Checkstyle extends SourceTask implements VerificationTask, Reporting<CheckstyleReports> {
private FileCollection checkstyleClasspath;
private FileCollection classpath;
private TextResource config;
private Map<String, Object> configProperties = new LinkedHashMap<String, Object>();
private final CheckstyleReports reports;
private boolean ignoreFailures;
private int maxErrors;
private int maxWarnings = Integer.MAX_VALUE;
private boolean showViolations = true;
private final DirectoryProperty configDirectory;
private final Property<JavaLauncher> javaLauncher;
private final Property<String> minHeapSize;
private final Property<String> maxHeapSize;
private final Property<Boolean> enableExternalDtdLoad;
public Checkstyle() {
this.configDirectory = getObjectFactory().directoryProperty();
this.reports = getObjectFactory().newInstance(CheckstyleReportsImpl.class, this);
this.minHeapSize = getObjectFactory().property(String.class);
this.maxHeapSize = getObjectFactory().property(String.class);
this.enableExternalDtdLoad = getObjectFactory().property(Boolean.class).convention(false);
// Set default JavaLauncher to current JVM in case
// CheckstylePlugin that sets Java launcher convention is not applied
this.javaLauncher = getCurrentJvmLauncher();
}
private Property<JavaLauncher> getCurrentJvmLauncher() {
Provider<JavaLauncher> currentJvmLauncherProvider = getToolchainService().launcherFor(new CurrentJvmToolchainSpec(getObjectFactory()));
return getObjectFactory().property(JavaLauncher.class).convention(currentJvmLauncherProvider);
}
/**
* The Checkstyle configuration file to use.
*/
@Internal
public File getConfigFile() {
return getConfig() == null ? null : getConfig().asFile();
}
/**
* The Checkstyle configuration file to use.
*/
public void setConfigFile(File configFile) {
setConfig(getProject().getResources().getText().fromFile(configFile));
}
@Inject
protected ObjectFactory getObjectFactory() {
throw new UnsupportedOperationException();
}
@Inject
protected JavaToolchainService getToolchainService() {
throw new UnsupportedOperationException();
}
@Inject
@Deprecated
public IsolatedAntBuilder getAntBuilder() {
throw new UnsupportedOperationException();
}
@Inject
public WorkerExecutor getWorkerExecutor() {
throw new UnsupportedOperationException();
}
/**
* Configures the reports to be generated by this task.
*
* The contained reports can be configured by name and closures. Example:
*
* <pre>
* checkstyleTask {
* reports {
* html {
* destination "build/checkstyle.html"
* }
* }
* }
* </pre>
*
* @param closure The configuration
* @return The reports container
*/
@Override
@SuppressWarnings("rawtypes")
public CheckstyleReports reports(@DelegatesTo(value = CheckstyleReports.class, strategy = Closure.DELEGATE_FIRST) Closure closure) {
return reports(new ClosureBackedAction<>(closure));
}
/**
* Configures the reports to be generated by this task.
*
* The contained reports can be configured by name and closures. Example:
*
* <pre>
* checkstyleTask {
* reports {
* html {
* destination "build/checkstyle.html"
* }
* }
* }
* </pre>
*
* @param configureAction The configuration
* @return The reports container
* @since 3.0
*/
@Override
public CheckstyleReports reports(Action<? super CheckstyleReports> configureAction) {
configureAction.execute(reports);
return reports;
}
/**
* JavaLauncher for toolchain support.
*
* @since 7.5
*/
@Incubating
@Nested
public Property<JavaLauncher> getJavaLauncher() {
return javaLauncher;
}
@TaskAction
public void run() {
runWithProcessIsolation();
}
private void runWithProcessIsolation() {
WorkQueue workQueue = getWorkerExecutor().processIsolation(spec -> {
spec.getForkOptions().setMinHeapSize(minHeapSize.getOrNull());
spec.getForkOptions().setMaxHeapSize(maxHeapSize.getOrNull());
spec.getForkOptions().setExecutable(javaLauncher.get().getExecutablePath().getAsFile().getAbsolutePath());
spec.getForkOptions().getSystemProperties().put("checkstyle.enableExternalDtdLoad", getEnableExternalDtdLoad().get());
spec.getClasspath().from(getCheckstyleClasspath());
});
workQueue.submit(CheckstyleAction.class, this::setupParameters);
}
private void setupParameters(CheckstyleActionParameters parameters) {
parameters.getConfig().set(getConfigFile());
parameters.getMaxErrors().set(getMaxErrors());
parameters.getMaxWarnings().set(getMaxWarnings());
parameters.getIgnoreFailures().set(getIgnoreFailures());
parameters.getConfigDirectory().set(getConfigDirectory());
parameters.getShowViolations().set(isShowViolations());
parameters.getSource().setFrom(getSource());
parameters.getIsHtmlRequired().set(getReports().getHtml().getRequired());
parameters.getIsXmlRequired().set(getReports().getXml().getRequired());
parameters.getXmlOuputLocation().set(getReports().getXml().getOutputLocation());
parameters.getHtmlOuputLocation().set(getReports().getHtml().getOutputLocation());
parameters.getTemporaryDir().set(getTemporaryDir());
parameters.getConfigProperties().set(getConfigProperties());
TextResource stylesheetString = getReports().getHtml().getStylesheet();
if (stylesheetString != null) {
parameters.getStylesheetString().set(stylesheetString.asString());
}
}
/**
* {@inheritDoc}
*
* <p>The sources for this task are relatively relocatable even though it produces output that
* includes absolute paths. This is a compromise made to ensure that results can be reused
* between different builds. The downside is that up-to-date results, or results loaded
* from cache can show different absolute paths than would be produced if the task was
* executed.</p>
*/
@Override
@PathSensitive(PathSensitivity.RELATIVE)
public FileTree getSource() {
return super.getSource();
}
/**
* The class path containing the Checkstyle library to be used.
*/
@Classpath
public FileCollection getCheckstyleClasspath() {
return checkstyleClasspath;
}
/**
* The class path containing the Checkstyle library to be used.
*/
public void setCheckstyleClasspath(FileCollection checkstyleClasspath) {
this.checkstyleClasspath = checkstyleClasspath;
}
/**
* The class path containing the compiled classes for the source files to be analyzed.
*/
@Classpath
public FileCollection getClasspath() {
return classpath;
}
/**
* The class path containing the compiled classes for the source files to be analyzed.
*/
public void setClasspath(FileCollection classpath) {
this.classpath = classpath;
}
/**
* The Checkstyle configuration to use. Replaces the {@code configFile} property.
*
* @since 2.2
*/
@Nested
public TextResource getConfig() {
return config;
}
/**
* The Checkstyle configuration to use. Replaces the {@code configFile} property.
*
* @since 2.2
*/
public void setConfig(TextResource config) {
this.config = config;
}
/**
* The properties available for use in the configuration file. These are substituted into the configuration file.
*/
@Nullable
@Optional
@Input
public Map<String, Object> getConfigProperties() {
return configProperties;
}
/**
* The properties available for use in the configuration file. These are substituted into the configuration file.
*/
public void setConfigProperties(@Nullable Map<String, Object> configProperties) {
this.configProperties = configProperties;
}
/**
* Path to other Checkstyle configuration files.
* <p>
* This path will be exposed as the variable {@code config_loc} in Checkstyle's configuration files.
* </p>
*
* @return path to other Checkstyle configuration files
* @since 6.0
*/
@PathSensitive(PathSensitivity.RELATIVE)
@InputFiles
public DirectoryProperty getConfigDirectory() {
return configDirectory;
}
/**
* The reports to be generated by this task.
*/
@Override
@Nested
public final CheckstyleReports getReports() {
return reports;
}
/**
* Whether or not this task will ignore failures and continue running the build.
*
* @return true if failures should be ignored
*/
@Override
public boolean getIgnoreFailures() {
return ignoreFailures;
}
/**
* Whether this task will ignore failures and continue running the build.
*
* @return true if failures should be ignored
*/
@Internal
public boolean isIgnoreFailures() {
return ignoreFailures;
}
/**
* Whether this task will ignore failures and continue running the build.
*/
@Override
public void setIgnoreFailures(boolean ignoreFailures) {
this.ignoreFailures = ignoreFailures;
}
/**
* The maximum number of errors that are tolerated before breaking the build
* or setting the failure property.
*
* @return the maximum number of errors allowed
* @since 3.4
*/
@Input
public int getMaxErrors() {
return maxErrors;
}
/**
* Set the maximum number of errors that are tolerated before breaking the build.
*
* @param maxErrors number of errors allowed
* @since 3.4
*/
public void setMaxErrors(int maxErrors) {
this.maxErrors = maxErrors;
}
/**
* The maximum number of warnings that are tolerated before breaking the build
* or setting the failure property.
*
* @return the maximum number of warnings allowed
* @since 3.4
*/
@Input
public int getMaxWarnings() {
return maxWarnings;
}
/**
* Set the maximum number of warnings that are tolerated before breaking the build.
*
* @param maxWarnings number of warnings allowed
* @since 3.4
*/
public void setMaxWarnings(int maxWarnings) {
this.maxWarnings = maxWarnings;
}
/**
* Whether rule violations are to be displayed on the console.
*
* @return true if violations should be displayed on console
*/
@Console
public boolean isShowViolations() {
return showViolations;
}
/**
* Whether rule violations are to be displayed on the console.
*/
public void setShowViolations(boolean showViolations) {
this.showViolations = showViolations;
}
/**
* The minimum heap size for the Checkstyle worker process, if any.
* Supports the units megabytes (e.g. "512m") and gigabytes (e.g. "1g").
*
* @return The minimum heap size. Value should be null if the default minimum heap size should be used.
*
* @since 7.5
*/
@Incubating
@Optional
@Input
public Property<String> getMinHeapSize() {
return minHeapSize;
}
/**
* The maximum heap size for the Checkstyle worker process, if any.
* Supports the units megabytes (e.g. "512m") and gigabytes (e.g. "1g").
*
* @return The maximum heap size. Value should be null if the default maximum heap size should be used.
*
* @since 7.5
*/
@Incubating
@Optional
@Input
public Property<String> getMaxHeapSize() {
return maxHeapSize;
}
/**
* Enable the use of external DTD files in configuration files.
* <strong>Disabled by default because this may be unsafe.</strong>
* See <a href="https://checkstyle.org/config_system_properties.html#Enable_External_DTD_load">Checkstyle documentation</a> for more details.
*
* @return property to enable the use of external DTD files
*
* @since 7.6
*/
@Incubating
@Input
public Property<Boolean> getEnableExternalDtdLoad() {
return enableExternalDtdLoad;
}
}