8000 Issue #16857: api: add new int process(List<Path> files) method to Ro… · checkstyle/checkstyle@60fbecc · GitHub
[go: up one dir, main page]

Skip to content

Commit 60fbecc

Browse files
Vincent PotucekPankraz76
Vincent Potucek
authored andcommitted
Issue #16857: api: add new int process(List<Path> files) method to RootModule - add api
1 parent 56fbb43 commit 60fbecc

File tree

10 files changed

+228
-38
lines changed

10 files changed

+228
-38
lines changed

config/jsoref-spellchecker/whitelist.words

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,7 @@ stylesheet
12771277
subdir
12781278
subelements
12791279
subext
1280+
subpath
12801281
subscope
12811282
sudo
12821283
suitebuilder

src/main/java/com/puppycrawl/tools/checkstyle/Checker.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import java.io.UnsupportedEncodingException;
2727
import java.nio.charset.Charset;
2828
import java.nio.charset.StandardCharsets;
29+
import java.nio.file.Path;
2930
import java.util.ArrayList;
31+
import java.util.Collection;
3032
import java.util.List;
3133
import java.util.Locale;
3234
import java.util.Set;
@@ -208,6 +210,13 @@ public void setBasedir(String basedir) {
208210
this.basedir = basedir;
209211
}
210212

213+
@Override
214+
public int process(Collection<Path> files) throws CheckstyleException {
215+
return process(files.stream()
216+
.map(Path::toFile)
217+
.collect(Collectors.toUnmodifiableList()));
218+
}
219+
211220
@Override
212221
public int process(List<File> files) throws CheckstyleException {
213222
if (cacheFile != null) {

src/main/java/com/puppycrawl/tools/checkstyle/Main.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,12 @@ else if (hasSuppressionLineColumnNumber) {
344344
}
345345

346346
// run Checker
347-
result = runCheckstyle(options, filesToProcess);
347+
result = runCheckstyle(
348+
options,
349+
filesToProcess
350+
.stream()
351+
.map(File::toPath)
352+
.collect(Collectors.toUnmodifiableList()));
348353
}
349354

350355
return result;
@@ -361,7 +366,7 @@ else if (hasSuppressionLineColumnNumber) {
361366
* @throws CheckstyleException
362367
* when properties file could not be loaded
363368
*/
364-
private static int runCheckstyle(CliOptions options, List<File> filesToProcess)
369+
private static int runCheckstyle(CliOptions options, List<Path> filesToProcess)
365370
throws CheckstyleException, IOException {
366371
// setup the properties
367372
final Properties props;

src/main/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTask.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ private void realExecute(String checkstyleVersion) {
328328
private void processFiles(RootModule rootModule, final SeverityLevelCounter warningCounter,
329329
final String checkstyleVersion) {
330330
final long startTime = System.currentTimeMillis();
331-
final List<File> files = getFilesToCheck();
331+
final List<Path> files = getFilesToCheck();
332332
final long endTime = System.currentTimeMillis();
333333
log("To locate the files took " + (endTime - startTime) + TIME_SUFFIX,
334334
Project.MSG_VERBOSE);
@@ -483,21 +483,20 @@ private AuditListener[] getListeners() {
483483
*
484484
* @return the list of files included via the fileName, filesets and paths.
485485
*/
486-
private List<File> getFilesToCheck() {
487-
final List<File> allFiles = new ArrayList<>();
486+
private List<Path> getFilesToCheck() {
487+
final List<Path> allFiles = new ArrayList<>();
488488
if (fileName != null) {
489489
// oops, we've got an additional one to process, don't
490490
// forget it. No sweat, it's fully resolved via the setter.
491491
log("Adding standalone file for audit", Project.MSG_VERBOSE);
492-
allFiles.add(Path.of(fileName).toFile());
492+
allFiles.add(Path.of(fileName));
493493
}
494494

495-
final List<File> filesFromFileSets = scanFileSets();
495+
final List<Path> filesFromFileSets = scanFileSets();
496496
allFiles.addAll(filesFromFileSets);
497497

498498
final List<Path> filesFromPaths = scanPaths();
499499
allFiles.addAll(filesFromPaths.stream()
500-
.map(Path::toFile)
501500
.collect(Collectors.toUnmodifiableList()));
502501

503502
return allFiles;
@@ -561,7 +560,7 @@ private List<Path> scanPath(org.apache.tools.ant.types.Path path, int pathIndex)
561560
*
562561
* @return the list of files included via the filesets.
563562
*/
564-
protected List<File> scanFileSets() {
563+
protected List<Path> scanFileSets() {
565564
final List<Path> allFiles = new ArrayList<>();
566565

567566
for (int i = 0; i < fileSets.size(); i++) {
@@ -572,7 +571,6 @@ protected List<File> scanFileSets() {
572571
}
573572

574573
return allFiles.stream()
575-
.map(Path::toFile)
576574
.collect(Collectors.toUnmodifiableList());
577575
}
578576

src/main/java/com/puppycrawl/tools/checkstyle/api/RootModule.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package com.puppycrawl.tools.checkstyle.api;
2121

2222
import java.io.File;
23+
import java.nio.file.Path;
24+
import java.util.Collection;
2325
import java.util.List;
2426

2527
/**
@@ -40,9 +42,23 @@ public interface RootModule extends Configurable {
4042
* @return the total number of audit events with error severity found
4143
* @throws CheckstyleException if error condition within Checkstyle occurs
4244
* @see #destroy()
45+
* @deprecated Use {@link #process(Collection)}
4346
*/
47+
@Deprecated(since = "10.23.1")
4448
int process(List<File> files) throws CheckstyleException;
4549

50+
/**
51+
* Processes a set of files.
52+
* Once this is done, it is highly recommended to call for
53+
* the destroy method to close and remove the listeners.
54+
*
55+
* @param files the list of files to be audited.
56+
* @return the total number of audit events with error severity found
57+
* @throws CheckstyleException if error condition within Checkstyle occurs
58+
* @see #destroy()
59+
*/
60+
int process(Collection<Path> files) throws CheckstyleException;
61+
4662
/**
4763
* Add the listener that will be used to receive events from the audit.
4864
*

src/test/java/com/puppycrawl/tools/checkstyle/AbstractModuleTestSupport.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,11 @@ protected final void verify(Checker checker,
490490
throws Exception {
491491
stream.flush();
492492
stream.reset();
493-
final List<File> theFiles = new ArrayList<>();
494-
Collections.addAll(theFiles, processedFiles);
495-
final int errs = checker.process(theFiles);
493+
final int errs = checker.process(
494+
Arrays
495+
.stream(processedFiles)
496+
.map(File::toPath)
497+
.collect(Collectors.toUnmodifiableList()));
496498

497499
// process each of the lines
498500
final Map<String, List<String>> actualViolations = getActualViolations(errs);
@@ -540,8 +542,8 @@ protected final void verifyWithLimitedResources(String fileName, String... expec
540542
*/
541543
protected final void execute(Configuration config, String... filenames) throws Exception {
542544
final Checker checker = createChecker(config);
543-
final List<File> files = Arrays.stream(filenames)
544-
.map(File::new)
545+
final List<Path> files = Arrays.stream(filenames)
546+
.map(Path::of)
545547
.collect(Collectors.toUnmodifiableList());
546548
checker.process(files);
547549
checker.destroy();
@@ -555,8 +557,8 @@ protected final void execute(Configuration config, String... filenames) throws E
555557
* @throws Exception if there is a problem during checker configuration
556558
*/
557559
protected static void execute(Checker checker, String... filenames) throws Exception {
558-
final List<File> files = Arrays.stream(filenames)
559-
.map(File::new)
560+
final List<Path> files = Arrays.stream(filenames)
561+
.map(Path::of)
560562
.collect(Collectors.toUnmodifiableList());
561563
checker.process(files);
562564
checker.destroy();
@@ -649,7 +651,7 @@ private List<String> getActualViolationsForFile(Configuration config,
649651
String file) throws Exception {
650652
stream.flush();
651653
stream.reset();
652-
final List<File> files = Collections.singletonList(new File(file));
654+
final List<Path> files = Collections.singletonList(Path.of(file));
653655
final Checker checker = createChecker(config);
654656
final Map<String, List<String>> actualViolations =
655657
getActualViolations(checker.process(files));

src/test/java/com/puppycrawl/tools/checkstyle/internal/testmodules/CheckstyleAntTaskStub.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package com.puppycrawl.tools.checkstyle.internal.testmodules;
2121

22-
import java.io.File;
22+
import java.nio.file.Path;
2323
import java.util.Collections;
2424
import java.util.List;
2525

@@ -28,25 +28,8 @@
2828
public class CheckstyleAntTaskStub extends CheckstyleAntTask {
2929

3030
@Override
31-
protected List<File> scanFileSets() {
32-
return Collections.singletonList(new MockFile());
33-
}
34-
35-
private static final class MockFile extends File {
36-
37-
/** A unique serial version identifier. */
38-
private static final long serialVersionUID = -2903929010510199407L;
39-
40-
private MockFile() {
41-
super("mock");
42-
}
43-
44-
/** This method is overridden to simulate an exception. */
45-
@Override
46-
public long lastModified() {
47-
throw new SecurityException("mock");
48-
}
49-
31+
protected List<Path> scanFileSets() {
32+
return Collections.singletonList(new PathMock());
5033
}
5134

5235
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
///////////////////////////////////////////////////////////////////////////////////////////////
2+
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3+
// Copyright (C) 2001-2025 the original author or authors.
4+
//
5+
// This library is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU Lesser General Public
7+
// License as published by the Free Software Foundation; either
8+
// version 2.1 of the License, or (at your option) any later version.
9+
//
10+
// This library is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
// Lesser General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Lesser General Public
16+
// License along with this library; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
///////////////////////////////////////////////////////////////////////////////////////////////
19+
20+
package com.puppycrawl.tools.checkstyle.internal.testmodules;
21+
22+
import java.io.File;
23+
import java.io.Serializable;
24+
import java.net.URI;
25+
import java.nio.file.FileSystem;
26+
import java.nio.file.LinkOption;
27+
import java.nio.file.Path;
28+
import java.nio.file.WatchEvent;
29+
import java.nio.file.WatchKey;
30+
import java.nio.file.WatchService;
31+
32+
public class PathMock implements Path, Serializable {
33+
34+
/** A unique serial version identifier. */
35+
private static final long serialVersionUID = -7801807253540916684L;
36+
37+
@Override
38+
public File toFile() {
39+
return new FileMock();
40+
}
41+
42+
@Override
43+
public boolean equals(Object obj) {
44+
return false;
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
return 0;
50+
}
51+
52+
@Override
53+
public FileSystem getFileSystem() {
54+
return null;
55+
}
56+
57+
@Override
58+
public boolean isAbsolute() {
59+
return false;
60+
}
61+
62+
@Override
63+
public Path getRoot() {
64+
return null;
65+
}
66+
67+
@Override
68+
public Path getFileName() {
69+
return null;
70+
}
71+
72+
@Override
73+
public Path getParent() {
74+
return null;
75+
}
76+
77+
@Override
78+
public int getNameCount() {
79+
return 0;
80+
}
81+
82+
@Override
83+
public Path getName(int i) {
84+
return null;
85+
}
86+
87+
@Override
88+
public Path subpath(int i, int i1) {
89+
return null;
90+
}
91+
92+
@Override
93+
public boolean startsWith(Path path) {
94+
return false;
95+
}
96+
97+
@Override
98+
public boolean endsWith(Path path) {
99+
return false;
100+
}
101+
102+
@Override
103+
public Path normalize() {
104+
return null;
105+
}
106+
107+
@Override
108+
public Path resolve(Path path) {
109+
return null;
110+
}
111+
112+
@Override
113+
public Path relativize(Path path) {
114+
return null;
115+
}
116+
117+
@Override
118+
public URI toUri() {
119+
return null;
120+
}
121+
122+
@Override
123+
public Path toAbsolutePath() {
124+
return null;
125+
}
126+
127+
@Override
128+
public Path toRealPath(LinkOption... linkOptions) {
129+
return null;
130+
}
131+
132+
@Override
133+
public WatchKey register(WatchService watchService,
134+
WatchEvent.Kind<?>[] kinds,
135+
WatchEvent.Modifier... modifiers) {
136+
return null;
137+
}
138+
139+
@Override
140+
public int compareTo(Path path) {
141+
return 0;
142+
}
143+
144+
private static final class FileMock extends File {
145+
/** A unique serial version identifier. */
146+
private static final long serialVersionUID = -2903929010510199407L;
147+
148+
private FileMock() {
149+
super("mock");
150+
}
151+
152+
@Override
153+
public long lastModified() {
154+
// origin: CheckstyleAntTaskTest#testCheckerException
155+
// trigger: throw new BuildException("Unable to process files: " + files, ex);
156+
throw new SecurityException("mock");
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)
0