Skip to content

Commit

Permalink
Replace File with Path
Browse files Browse the repository at this point in the history
  • Loading branch information
gaul committed Jan 6, 2025
1 parent f120fab commit 41d76bc
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -258,11 +262,12 @@ public void execute() throws MojoExecutionException {
Set<String> ignoreClassNames = new HashSet<String>();
try {
ignoreClassNames.addAll(
SuppressModernizerAnnotationDetector.detect(outputDirectory));
SuppressModernizerAnnotationDetector.detect(
outputDirectory.toPath()));
if (ignoreGeneratedClasses) {
Set<String> ignore =
SuppressGeneratedAnnotationDetector.detect(
outputDirectory);
outputDirectory.toPath());
if (getLog().isDebugEnabled()) {
getLog().debug(
"The following generated classes will be ignored");
Expand All @@ -275,11 +280,11 @@ public void execute() throws MojoExecutionException {
if (includeTestClasses) {
ignoreClassNames.addAll(
SuppressModernizerAnnotationDetector.detect(
testOutputDirectory));
testOutputDirectory.toPath()));
if (ignoreGeneratedClasses) {
Set<String> ignore =
SuppressGeneratedAnnotationDetector.detect(
testOutputDirectory);
testOutputDirectory.toPath());
if (getLog().isDebugEnabled()) {
getLog().debug(
"The following generated test classes " +
Expand All @@ -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);
Expand All @@ -323,6 +328,7 @@ public void execute() throws MojoExecutionException {
}

private static Map<String, Violation> parseViolations(
// TODO: rename
String violationsFilePath) throws MojoExecutionException {
InputStream is;
if (violationsFilePath.startsWith(CLASSPATH_PREFIX)) {
Expand All @@ -333,12 +339,12 @@ private static Map<String, Violation> 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 {
Expand All @@ -357,13 +363,14 @@ private static Map<String, Violation> parseViolations(
}
}

// TODO: rename
private Collection<String> 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);
Expand All @@ -384,25 +391,24 @@ private Collection<String> 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<Path> stream = Files.list(path);
Iterable<Path> 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<ViolationOccurrence> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,10 +38,10 @@ public final class SuppressGeneratedAnnotationDetector {

private SuppressGeneratedAnnotationDetector() { }

public static Set<String> detect(File file) throws IOException {
public static Set<String> detect(Path path) throws IOException {
SuppressGeneratedAnnotationDetector detector =
new SuppressGeneratedAnnotationDetector();
detector.detectInternal(file);
detector.detectInternal(path);
return detector.computeSuppressedClassNames();
}

Expand Down Expand Up @@ -69,20 +70,19 @@ private Set<String> 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<Path> stream = Files.list(path);
Iterable<Path> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,10 +40,10 @@ public final class SuppressModernizerAnnotationDetector {

private SuppressModernizerAnnotationDetector() { }

public static Set<String> detect(File file) throws IOException {
public static Set<String> detect(Path path) throws IOException {
SuppressModernizerAnnotationDetector detector =
new SuppressModernizerAnnotationDetector();
detector.detectInternal(file);
detector.detectInternal(path);
return detector.computeSuppressedClassNames();
}

Expand Down Expand Up @@ -82,20 +83,19 @@ private Set<String> 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<Path> stream = Files.list(path);
Iterable<Path> 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);
Expand Down

0 comments on commit 41d76bc

Please sign in to comment.