8000 Issue #17000: AvoidOutdatedUsageCheck: Use modern Java collections AP… · checkstyle/checkstyle@18b2218 · GitHub
[go: up one dir, main page]

Skip to content

Commit 18b2218

Browse files
author
Vincent Potucek
committed
Issue #17000: AvoidOutdatedUsageCheck: Use modern Java collections API (toList() instead of collect(Collectors.toList()))
1 parent 55de373 commit 18b2218

File tree

9 files changed

+209
-38
lines changed

9 files changed

+209
-38
lines changed

config/jsoref-spellchecker/whitelist.words

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,7 @@ stylesheet
12801280
subdir
12811281
subelements
12821282
subext
1283+
subpath
12831284
subscope
12841285
sudo
12851286
suitebuilder

config/pitest-suppressions/pitest-common-suppressions.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@
88
<description>Removed assignment to member variable args</description>
99
<lineContent>this.args = null;</lineContent>
1010
</mutation>
11+
<mutation unstable="false">
12+
<sourceFile>Checker.java</sourceFile>
13+
<mutatedClass>com.puppycrawl.tools.checkstyle.Checker</mutatedClass>
14+
<mutatedMethod>process</mutatedMethod>
15+
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator</mutator>
16+
<description>removed call to com/puppycrawl/tools/checkstyle/Checker::process</description>
17+
<lineContent>return process(files.stream()</lineContent>
18+
</mutation>
1119
</suppressedMutations>

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
@@ -330,7 +330,7 @@ private void realExecute(String checkstyleVersion) {
330330
private void processFiles(RootModule rootModule, final SeverityLevelCounter warningCounter,
331331
final String checkstyleVersion) {
332332
final long startTime = System.currentTimeMillis();
333-
final List<File> files = getFilesToCheck();
333+
final List<Path> files = getFilesToCheck();
334334
final long endTime = System.currentTimeMillis();
335335
log("To locate the files took " + (endTime - startTime) + TIME_SUFFIX,
336336
Project.MSG_VERBOSE);
@@ -485,21 +485,20 @@ private AuditListener[] getListeners() {
485485
*
486486
* @return the list of files included via the fileName, filesets and paths.
487487
*/
488-
private List<File> getFilesToCheck() {
489-
final List<File> allFiles = new ArrayList<>();
488+
private List<Path> getFilesToCheck() {
489+
final List<Path> allFiles = new ArrayList<>();
490490
if (fileName != null) {
491491
// oops, we've got an additional one to process, don't
492492
// forget it. No sweat, it's fully resolved via the setter.
493493
log("Adding standalone file for audit", Project.MSG_VERBOSE);
494-
allFiles.add(Path.of(fileName).toFile());
494+
allFiles.add(Path.of(fileName));
495495
}
496496

497-
final List<File> filesFromFileSets = scanFileSets();
497+
final List<Path> filesFromFileSets = scanFileSets();
498498
allFiles.addAll(filesFromFileSets);
499499

500500
final List<Path> filesFromPaths = scanPaths();
501501
allFiles.addAll(filesFromPaths.stream()
502-
.map(Path::toFile)
503502
.collect(Collectors.toUnmodifiableList()));
504503

505504
return allFiles;
@@ -563,7 +562,7 @@ private List<Path> scanPath(org.apache.tools.ant.types.Path path, int pathIndex)
563562
*
564563
* @return the list of files included via the filesets.
565564
*/
566-
protected List<File> scanFileSets() {
565+
protected List<Path> scanFileSets() {
567566
final List<Path> allFiles = new ArrayList<>();
568567

569568
for (int i = 0; i < fileSets.size(); i++) {
@@ -574,7 +573,6 @@ protected List<File> scanFileSets() {
574573
}
575574

576575
return allFiles.stream()
577-
.map(Path::toFile)
578576
.collect(Collectors.toUnmodifiableList());
579577
}
580578

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public interface RootModule extends Configurable {
4242
* @return the total number of audit events with error severity found
4343
* @throws CheckstyleException if error condition within Checkstyle occurs
4444
* @see #destroy()
45+
* @deprecated Use {@link #process(Collection)}
4546
*/
47+
@Deprecated(since = "10.23.1")
4648
int process(List<File> files) throws CheckstyleException;
4749

4850
/**

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/CheckerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package com.puppycrawl.tools.checkstyle;
2121

22+
import static com.google.common.truth.Truth.assertThat;
2223
import static com.google.common.truth.Truth.assertWithMessage;
2324
import static com.puppycrawl.tools.checkstyle.Checker.EXCEPTION_MSG;
2425
import static com.puppycrawl.tools.checkstyle.DefaultLogger.AUDIT_FINISHED_MESSAGE;
@@ -55,8 +56,10 @@
5556
import java.util.UUID;
5657
import java.util.stream.Collectors;
5758

59+
import org.apache.commons.lang3.RandomUtils;
5860
import org.junit.jupiter.api.Test;
5961
import org.junit.jupiter.api.io.TempDir;
62+
import org.mockito.Mockito;
6063

6164
import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions;
6265
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
@@ -166,6 +169,16 @@ public void testDestroyPath() throws Exception {
166169
assertDestroy(checker, auditAdapter, fileSet, filter, fileFilter);
167170
}
168171

172+
@Test
173+
public void testPath() throws Exception {
174+
final Checker checker = Mockito.spy(new Checker());
175+
final File file = new File("");
176+
final int count = RandomUtils.nextInt();
177+
Mockito.when(checker.process(List.of(file))).thenReturn(count);
178+
Mockito.verify(checker).process(List.of(file));
179+
assertThat(checker.process(Set.of(file.toPath()))).isEqualTo(count);
180+
}
181+
169182
private void assertDestroy(Checker checker,
170183
DebugAuditAdapter auditAdapter,
171184
TestFileSetCheck fileSet,

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