Skip to content

Commit 33f42e7

Browse files
committed
[MPMD-370] Remove remaining uses of FileReader
1 parent 957ec89 commit 33f42e7

7 files changed

+24
-70
lines changed

src/main/java/org/apache/maven/plugins/pmd/ExcludeDuplicationsFromFile.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
*/
1919
package org.apache.maven.plugins.pmd;
2020

21-
import java.io.FileReader;
2221
import java.io.IOException;
23-
import java.io.LineNumberReader;
22+
import java.nio.file.Files;
23+
import java.nio.file.Paths;
2424
import java.util.ArrayList;
2525
import java.util.HashSet;
2626
import java.util.List;
2727
import java.util.Set;
28+
import java.util.stream.Collectors;
29+
import java.util.stream.Stream;
2830

2931
import net.sourceforge.pmd.cpd.Mark;
3032
import net.sourceforge.pmd.cpd.Match;
@@ -102,13 +104,10 @@ public void loadExcludeFromFailuresData(final String excludeFromFailureFile) thr
102104
return;
103105
}
104106

105-
try (LineNumberReader reader = new LineNumberReader(new FileReader(excludeFromFailureFile))) {
106-
String line;
107-
while ((line = reader.readLine()) != null) {
108-
if (!line.startsWith("#")) {
109-
exclusionList.add(createSetFromExclusionLine(line));
110-
}
111-
}
107+
try (Stream<String> lines = Files.lines(Paths.get(excludeFromFailureFile))) {
108+
exclusionList.addAll(lines.filter(line -> !line.startsWith("#"))
109+
.map(line -> createSetFromExclusionLine(line))
110+
.collect(Collectors.toList()));
112111
} catch (final IOException e) {
113112
throw new MojoExecutionException("Cannot load file " + excludeFromFailureFile, e);
114113
}

src/test/java/org/apache/maven/plugins/pmd/AbstractPmdReportTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ protected File generateReport(AbstractPmdReport mojo, File pluginXmlFile) throws
134134
/**
135135
* Read the contents of the specified file object into a string
136136
*/
137-
protected String readFile(File pmdTestDir, String fileName) throws IOException {
138-
return new String(Files.readAllBytes(pmdTestDir.toPath().resolve(fileName)));
137+
protected String readFile(File file) throws IOException {
138+
return new String(Files.readAllBytes(file.toPath()));
139139
}
140140

141141
/**

src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
import javax.xml.parsers.DocumentBuilder;
2222
import javax.xml.parsers.DocumentBuilderFactory;
2323

24-
import java.io.BufferedReader;
2524
import java.io.File;
26-
import java.io.FileReader;
27-
import java.io.IOException;
2825
import java.util.Locale;
2926

3027
import org.apache.commons.io.FileUtils;
@@ -137,26 +134,6 @@ public void testInvalidFormat() throws Exception {
137134
}
138135
}
139136

140-
/**
141-
* Read the contents of the specified file object into a string
142-
*
143-
* @param file the file to be read
144-
* @return a String object that contains the contents of the file
145-
* @throws java.io.IOException
146-
*/
147-
private String readFile(File file) throws IOException {
148-
String strTmp;
149-
StringBuilder str = new StringBuilder((int) file.length());
150-
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
151-
while ((strTmp = in.readLine()) != null) {
152-
str.append(' ');
153-
str.append(strTmp);
154-
}
155-
}
156-
157-
return str.toString();
158-
}
159-
160137
public void testWriteNonHtml() throws Exception {
161138
generateReport("cpd", "default-configuration/cpd-default-configuration-plugin-config.xml");
162139

src/test/java/org/apache/maven/plugins/pmd/PmdReportTest.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
*/
1919
package org.apache.maven.plugins.pmd;
2020

21-
import java.io.BufferedReader;
2221
import java.io.File;
23-
import java.io.FileReader;
2422
import java.io.IOException;
2523
import java.net.ServerSocket;
2624
import java.net.URL;
@@ -389,26 +387,6 @@ public void testIncludeXmlInSite() throws Exception {
389387
assertTrue(pmdXml.contains("</pmd>"));
390388
}
391389

392-
/**
393-
* Read the contents of the specified file object into a string
394-
*
395-
* @param file the file to be read
396-
* @return a String object that contains the contents of the file
397-
* @throws java.io.IOException
398-
*/
399-
private String readFile(File file) throws IOException {
400-
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
401-
final StringBuilder str = new StringBuilder((int) file.length());
402-
403-
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
404-
str.append(' ');
405-
str.append(line);
406-
str.append('\n');
407-
}
408-
return str.toString();
409-
}
410-
}
411-
412390
/**
413391
* Verify the correct working of the locationTemp method
414392
*

src/test/java/org/apache/maven/plugins/pmd/stubs/CustomConfigurationMavenProjectStub.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
package org.apache.maven.plugins.pmd.stubs;
2020

2121
import java.io.File;
22-
import java.io.FileReader;
22+
import java.io.FileInputStream;
23+
import java.io.InputStream;
2324
import java.util.ArrayList;
2425
import java.util.List;
2526

@@ -43,12 +44,11 @@ public CustomConfigurationMavenProjectStub() {
4344
MavenXpp3Reader pomReader = new MavenXpp3Reader();
4445
Model model = null;
4546

46-
try {
47-
model = pomReader.read(new FileReader(new File(getBasedir()
48-
+ "/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml")));
47+
try (InputStream is = new FileInputStream(new File(getBasedir()
48+
+ "/src/test/resources/unit/custom-configuration/custom-configuration-plugin-config.xml"))) {
49+
model = pomReader.read(is);
4950
setModel(model);
5051
} catch (Exception e) {
51-
5252
}
5353

5454
setGroupId(model.getGroupId());

src/test/java/org/apache/maven/plugins/pmd/stubs/DefaultConfigurationMavenProjectStub.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
package org.apache.maven.plugins.pmd.stubs;
2020

2121
import java.io.File;
22-
import java.io.FileReader;
22+
import java.io.FileInputStream;
23+
import java.io.InputStream;
2324
import java.util.ArrayList;
2425
import java.util.List;
2526

@@ -43,12 +44,11 @@ public DefaultConfigurationMavenProjectStub() {
4344
MavenXpp3Reader pomReader = new MavenXpp3Reader();
4445
Model model = null;
4546

46-
try (FileReader reader = new FileReader(new File(getBasedir()
47+
try (InputStream is = new FileInputStream(new File(getBasedir()
4748
+ "/src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml"))) {
48-
model = pomReader.read(reader);
49+
model = pomReader.read(is);
4950
setModel(model);
5051
} catch (Exception e) {
51-
5252
}
5353

5454
setGroupId(model.getGroupId());

src/test/java/org/apache/maven/plugins/pmd/stubs/InvalidFormatMavenProjectStub.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
package org.apache.maven.plugins.pmd.stubs;
2020

2121
import java.io.File;
22-
import java.io.FileReader;
22+
import java.io.FileInputStream;
23+
import java.io.InputStream;
2324
import java.util.ArrayList;
2425
import java.util.List;
2526

@@ -40,12 +41,11 @@ public InvalidFormatMavenProjectStub() {
4041
MavenXpp3Reader pomReader = new MavenXpp3Reader();
4142
Model model = null;
4243

43-
try {
44-
model = pomReader.read(new FileReader(new File(
45-
getBasedir() + "/src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml")));
44+
try (InputStream is = new FileInputStream(
45+
new File(getBasedir() + "/src/test/resources/unit/invalid-format/invalid-format-plugin-config.xml"))) {
46+
model = pomReader.read(is);
4647
setModel(model);
4748
} catch (Exception e) {
48-
4949
}
5050

5151
setGroupId(model.getGroupId());

0 commit comments

Comments
 (0)