From 41d76bcb74f46fb5353cdfb6af52e9a72909b7f5 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 5 Jan 2025 22:34:15 -0800 Subject: [PATCH] Replace File with Path --- .../ModernizerMojo.java | 58 ++++++++++--------- .../SuppressGeneratedAnnotationDetector.java | 28 ++++----- .../SuppressModernizerAnnotationDetector.java | 28 ++++----- 3 files changed, 60 insertions(+), 54 deletions(-) diff --git a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java index 8002c9fc..a50960cf 100644 --- a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java +++ b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java @@ -21,10 +21,11 @@ import static org.gaul.modernizer_maven_plugin.Utils.checkArgument; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -34,6 +35,7 @@ import java.util.Set; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; +import java.util.stream.Stream; import javax.xml.parsers.ParserConfigurationException; @@ -62,6 +64,8 @@ public final class ModernizerMojo extends AbstractMojo { /** The output directory into which to find the source code. */ @Parameter(property = "project.build.sourceDirectory") + // TODO: cannnot convert to Path: + // https://lists.apache.org/thread/b4gz60c5v5oz7khczs4nozywbkfbmhjf private File sourceDirectory; /** The output directory into which to find the test source code. */ @@ -258,11 +262,12 @@ public void execute() throws MojoExecutionException { Set ignoreClassNames = new HashSet(); try { ignoreClassNames.addAll( - SuppressModernizerAnnotationDetector.detect(outputDirectory)); + SuppressModernizerAnnotationDetector.detect( + outputDirectory.toPath())); if (ignoreGeneratedClasses) { Set ignore = SuppressGeneratedAnnotationDetector.detect( - outputDirectory); + outputDirectory.toPath()); if (getLog().isDebugEnabled()) { getLog().debug( "The following generated classes will be ignored"); @@ -275,11 +280,11 @@ public void execute() throws MojoExecutionException { if (includeTestClasses) { ignoreClassNames.addAll( SuppressModernizerAnnotationDetector.detect( - testOutputDirectory)); + testOutputDirectory.toPath())); if (ignoreGeneratedClasses) { Set ignore = SuppressGeneratedAnnotationDetector.detect( - testOutputDirectory); + testOutputDirectory.toPath()); if (getLog().isDebugEnabled()) { getLog().debug( "The following generated test classes " + @@ -301,9 +306,9 @@ public void execute() throws MojoExecutionException { long count; try { - count = recurseFiles(outputDirectory); + count = recurseFiles(outputDirectory.toPath()); if (includeTestClasses) { - count += recurseFiles(testOutputDirectory); + count += recurseFiles(testOutputDirectory.toPath()); } } catch (IOException ioe) { throw new MojoExecutionException("Error reading Java classes", ioe); @@ -323,6 +328,7 @@ public void execute() throws MojoExecutionException { } private static Map parseViolations( + // TODO: rename String violationsFilePath) throws MojoExecutionException { InputStream is; if (violationsFilePath.startsWith(CLASSPATH_PREFIX)) { @@ -333,12 +339,12 @@ private static Map parseViolations( classpath)); is = Modernizer.class.getResourceAsStream(classpath); } else { - File file = new File(violationsFilePath); + Path path = FileSystems.getDefault().getPath(violationsFilePath); try { - is = new FileInputStream(file); - } catch (FileNotFoundException fnfe) { + is = Files.newInputStream(path); + } catch (IOException fnfe) { throw new MojoExecutionException( - "Error opening violation file: " + file, fnfe); + "Error opening violation file: " + path, fnfe); } } try { @@ -357,13 +363,14 @@ private static Map parseViolations( } } + // TODO: rename private Collection readExclusionsFile(String exclusionsFilePath) throws MojoExecutionException { InputStream is = null; try { - File file = new File(exclusionsFilePath); - if (file.exists()) { - is = new FileInputStream(exclusionsFilePath); + Path path = FileSystems.getDefault().getPath(exclusionsFilePath); + if (Files.exists(path)) { + is = Files.newInputStream(path); } else { is = this.getClass().getClassLoader().getResourceAsStream( exclusionsFilePath); @@ -384,25 +391,24 @@ private Collection readExclusionsFile(String exclusionsFilePath) } } - private long recurseFiles(File file) throws IOException { + private long recurseFiles(Path path) throws IOException { long count = 0; - if (!file.exists()) { + if (!Files.exists(path)) { return count; } - if (file.isDirectory()) { - String[] children = file.list(); - if (children != null) { - for (String child : children) { - count += recurseFiles(new File(file, child)); - } + if (Files.isDirectory(path)) { + Stream stream = Files.list(path); + Iterable children = stream::iterator; + for (Path child : children) { + count += recurseFiles(path.resolve(child)); } - } else if (file.getPath().endsWith(".class")) { - InputStream is = new FileInputStream(file); + } else if (path.toString().endsWith(".class")) { + InputStream is = Files.newInputStream(path); try { Collection occurrences = modernizer.check(is); for (ViolationOccurrence occurrence : occurrences) { - String name = file.getPath(); + String name = path.toString(); if (name.startsWith(outputDirectory.getPath())) { name = sourceDirectory.getPath() + name.substring( outputDirectory.getPath().length()); diff --git a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/SuppressGeneratedAnnotationDetector.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/SuppressGeneratedAnnotationDetector.java index 9bdf58ca..8cca0779 100644 --- a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/SuppressGeneratedAnnotationDetector.java +++ b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/SuppressGeneratedAnnotationDetector.java @@ -18,12 +18,13 @@ import static org.gaul.modernizer_maven_plugin.Utils.ASM_API; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.HashSet; import java.util.Set; +import java.util.stream.Stream; import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.ClassReader; @@ -37,10 +38,10 @@ public final class SuppressGeneratedAnnotationDetector { private SuppressGeneratedAnnotationDetector() { } - public static Set detect(File file) throws IOException { + public static Set detect(Path path) throws IOException { SuppressGeneratedAnnotationDetector detector = new SuppressGeneratedAnnotationDetector(); - detector.detectInternal(file); + detector.detectInternal(path); return detector.computeSuppressedClassNames(); } @@ -69,20 +70,19 @@ private Set computeSuppressedClassNames() { return suppressedClassNames; } - private void detectInternal(File file) throws IOException { - if (!file.exists()) { + private void detectInternal(Path path) throws IOException { + if (!Files.exists(path)) { return; - } else if (file.isDirectory()) { - String[] children = file.list(); - if (children != null) { - for (String child : children) { - detectInternal(new File(file, child)); - } + } else if (Files.isDirectory(path)) { + Stream stream = Files.list(path); + Iterable children = stream::iterator; + for (Path child : children) { + detectInternal(path.resolve(child)); } - } else if (file.getPath().endsWith(".class")) { + } else if (path.toString().endsWith(".class")) { InputStream inputStream = null; try { - inputStream = new FileInputStream(file); + inputStream = Files.newInputStream(path); detectInternal(new ClassReader(inputStream)); } finally { Utils.closeQuietly(inputStream); diff --git a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/SuppressModernizerAnnotationDetector.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/SuppressModernizerAnnotationDetector.java index 5e02b588..093db1f1 100644 --- a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/SuppressModernizerAnnotationDetector.java +++ b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/SuppressModernizerAnnotationDetector.java @@ -18,12 +18,13 @@ import static org.gaul.modernizer_maven_plugin.Utils.ASM_API; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.HashSet; import java.util.Set; +import java.util.stream.Stream; import org.gaul.modernizer_maven_annotations.SuppressModernizer; import org.objectweb.asm.AnnotationVisitor; @@ -39,10 +40,10 @@ public final class SuppressModernizerAnnotationDetector { private SuppressModernizerAnnotationDetector() { } - public static Set detect(File file) throws IOException { + public static Set detect(Path path) throws IOException { SuppressModernizerAnnotationDetector detector = new SuppressModernizerAnnotationDetector(); - detector.detectInternal(file); + detector.detectInternal(path); return detector.computeSuppressedClassNames(); } @@ -82,20 +83,19 @@ private Set computeSuppressedClassNames() { return suppressedClassNames; } - private void detectInternal(File file) throws IOException { - if (!file.exists()) { + private void detectInternal(Path path) throws IOException { + if (!Files.exists(path)) { return; - } else if (file.isDirectory()) { - String[] children = file.list(); - if (children != null) { - for (String child : children) { - detectInternal(new File(file, child)); - } + } else if (Files.isDirectory(path)) { + Stream stream = Files.list(path); + Iterable children = stream::iterator; + for (Path child : children) { + detectInternal(path.resolve(child)); } - } else if (file.getPath().endsWith(".class")) { + } else if (path.toString().endsWith(".class")) { InputStream inputStream = null; try { - inputStream = new FileInputStream(file); + inputStream = Files.newInputStream(path); detectInternal(new ClassReader(inputStream)); } finally { Utils.closeQuietly(inputStream);