Skip to content

Commit f6cbd36

Browse files
authored
Replace File with Path at codemodder-plugin-maven (#233)
Remove the usage of File and replace it to Path
1 parent 2be735f commit f6cbd36

17 files changed

+164
-159
lines changed

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/operator/AbstractCommand.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package io.codemodder.plugins.maven.operator;
22

3-
import java.io.File;
43
import java.io.IOException;
54
import java.net.URISyntaxException;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
67
import java.util.List;
78
import javax.xml.stream.XMLStreamException;
89
import org.dom4j.Element;
@@ -63,17 +64,17 @@ public boolean postProcess(ProjectModel c) throws XMLStreamException {
6364
* @param pm The ProjectModel containing information about the project.
6465
* @return A File object representing the local repository path.
6566
*/
66-
protected File getLocalRepositoryPath(ProjectModel pm) {
67-
File localRepositoryPath = null;
67+
protected Path getLocalRepositoryPath(ProjectModel pm) {
68+
Path localRepositoryPath = null;
6869

6970
if (pm.getRepositoryPath() != null) {
7071
localRepositoryPath = pm.getRepositoryPath();
7172
} else if (System.getenv("M2_REPO") != null) {
72-
localRepositoryPath = new File(System.getenv("M2_REPO"));
73+
localRepositoryPath = Paths.get(System.getenv("M2_REPO"));
7374
} else if (System.getProperty("maven.repo.local") != null) {
74-
localRepositoryPath = new File(System.getProperty("maven.repo.local"));
75+
localRepositoryPath = Paths.get(System.getProperty("maven.repo.local"));
7576
} else {
76-
localRepositoryPath = new File(System.getProperty("user.home"), ".m2/repository");
77+
localRepositoryPath = Paths.get(System.getProperty("user.home"), ".m2/repository");
7778
}
7879

7980
return localRepositoryPath;

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/operator/AbstractQueryCommand.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import io.github.pixee.security.BoundedLineReader;
44
import java.io.BufferedReader;
5-
import java.io.File;
6-
import java.io.FileReader;
75
import java.io.IOException;
86
import java.net.URISyntaxException;
7+
import java.nio.file.Files;
98
import java.nio.file.Path;
109
import java.nio.file.Paths;
1110
import java.util.*;
@@ -29,24 +28,21 @@ abstract class AbstractQueryCommand extends AbstractCommand {
2928
*
3029
* @param pomFilePath POM Original File Path
3130
*/
32-
private File getOutputPath(File pomFilePath) {
33-
File basePath = pomFilePath.getParentFile();
31+
private Path getOutputPath(Path pomFilePath) {
32+
Path basePath = pomFilePath.getParent();
3433

3534
String outputBasename = String.format("output-%08X.txt", pomFilePath.hashCode());
3635

37-
File outputPath = new File(basePath, outputBasename);
38-
39-
return outputPath;
36+
return basePath.resolve(outputBasename);
4037
}
4138

4239
/**
4340
* Given a POM URI, returns a File Object
4441
*
4542
* @param d POMDocument
4643
*/
47-
private File getPomFilePath(POMDocument d) throws URISyntaxException {
48-
Path pomPath = Paths.get(d.getPomPath().toURI());
49-
return pomPath.toFile();
44+
private Path getPomFilePath(POMDocument d) throws URISyntaxException {
45+
return Paths.get(d.getPomPath().toURI());
5046
}
5147

5248
/**
@@ -56,7 +52,7 @@ private File getPomFilePath(POMDocument d) throws URISyntaxException {
5652
* @param pomFilePath Input Pom Path
5753
* @param c Project Model
5854
*/
59-
protected abstract void extractDependencyTree(File outputPath, File pomFilePath, ProjectModel c);
55+
protected abstract void extractDependencyTree(Path outputPath, Path pomFilePath, ProjectModel c);
6056

6157
/**
6258
* Internal Holder Variable
@@ -85,11 +81,11 @@ public Collection<Dependency> getResult() {
8581
*/
8682
@Override
8783
public boolean execute(ProjectModel pm) throws URISyntaxException, IOException {
88-
File pomFilePath = getPomFilePath(pm.getPomFile());
89-
File outputPath = getOutputPath(pomFilePath);
84+
Path pomFilePath = getPomFilePath(pm.getPomFile());
85+
Path outputPath = getOutputPath(pomFilePath);
9086

91-
if (outputPath.exists()) {
92-
outputPath.delete();
87+
if (Files.exists(outputPath)) {
88+
Files.delete(outputPath);
9389
}
9490

9591
try {
@@ -118,9 +114,9 @@ public boolean execute(ProjectModel pm) throws URISyntaxException, IOException {
118114
*
119115
* @param outputPath file to read
120116
*/
121-
private Map<String, Dependency> extractDependencies(File outputPath) throws IOException {
117+
private Map<String, Dependency> extractDependencies(Path outputPath) throws IOException {
122118
Map<String, Dependency> dependencyMap = new HashMap<>();
123-
try (BufferedReader reader = new BufferedReader(new FileReader(outputPath))) {
119+
try (BufferedReader reader = Files.newBufferedReader(outputPath)) {
124120
String line;
125121
boolean skipFirstLine = true;
126122
while ((line = BoundedLineReader.readLine(reader, 5_000_000)) != null) {

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/operator/CheckLocalRepositoryDirCommand.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.codemodder.plugins.maven.operator;
22

3-
import java.io.File;
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
46

57
/** Commands for checking and creating the local repository directory if it doesn't exist. */
68
class CheckLocalRepositoryDirCommand {
@@ -29,7 +31,7 @@ public static CheckParentDirCommand getInstance() {
2931
* @throws InvalidContextException Always throws an InvalidContextException.
3032
*/
3133
@Override
32-
protected void extractDependencyTree(File outputPath, File pomFilePath, ProjectModel c) {
34+
protected void extractDependencyTree(Path outputPath, Path pomFilePath, ProjectModel c) {
3335
throw new InvalidContextException();
3436
}
3537

@@ -42,11 +44,11 @@ protected void extractDependencyTree(File outputPath, File pomFilePath, ProjectM
4244
* errors.
4345
*/
4446
@Override
45-
public boolean execute(ProjectModel c) {
46-
File localRepositoryPath = getLocalRepositoryPath(c);
47+
public boolean execute(ProjectModel c) throws IOException {
48+
Path localRepositoryPath = getLocalRepositoryPath(c);
4749

48-
if (!localRepositoryPath.exists()) {
49-
localRepositoryPath.mkdirs();
50+
if (Files.notExists(localRepositoryPath)) {
51+
Files.createDirectories(localRepositoryPath);
5052
}
5153

5254
return false;
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package io.codemodder.plugins.maven.operator;
22

3-
import java.io.File;
43
import java.io.IOException;
4+
import java.nio.file.Path;
55

66
/** Exception class for handling invalid file paths. */
77
class InvalidPathException extends IOException {
8-
private final File parentPath;
98
private final String relativePath;
10-
private final boolean loop;
119

1210
/**
1311
* Constructs an InvalidPathException with the specified details.
@@ -16,29 +14,19 @@ class InvalidPathException extends IOException {
1614
* @param relativePath The relative path that is considered invalid.
1715
* @param loop Indicates whether the path forms a loop.
1816
*/
19-
public InvalidPathException(File parentPath, String relativePath, boolean loop) {
17+
public InvalidPathException(Path parentPath, String relativePath, boolean loop) {
2018
super(
2119
"Invalid Relative Path "
2220
+ relativePath
2321
+ " (from "
24-
+ parentPath.getAbsolutePath()
22+
+ parentPath.toAbsolutePath()
2523
+ ") (loops? "
2624
+ loop
2725
+ ")");
28-
this.parentPath = parentPath;
2926
this.relativePath = relativePath;
30-
this.loop = loop;
31-
}
32-
33-
public File getParentPath() {
34-
return parentPath;
3527
}
3628

3729
public String getRelativePath() {
3830
return relativePath;
3931
}
40-
41-
public boolean isLoop() {
42-
return loop;
43-
}
4432
}

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/operator/POMDocument.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package io.codemodder.plugins.maven.operator;
22

3-
import java.io.File;
43
import java.net.URISyntaxException;
54
import java.net.URL;
65
import java.nio.charset.Charset;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
78
import org.dom4j.Document;
89

910
/**
@@ -24,7 +25,7 @@ public class POMDocument {
2425
private Document pomDocument;
2526

2627
private Document resultPom;
27-
private File file;
28+
private Path path;
2829
private Charset charset;
2930
private String endl;
3031
private String indent;
@@ -54,7 +55,7 @@ public POMDocument(byte[] originalPom, URL pomPath, Document pomDocument)
5455
this.pomPath = pomPath;
5556
this.pomDocument = pomDocument;
5657
this.resultPom = (Document) pomDocument.clone();
57-
this.file = this.pomPath != null ? new File(this.pomPath.toURI()) : null;
58+
this.path = this.pomPath != null ? Paths.get(this.pomPath.toURI()) : null;
5859
this.charset = Charset.defaultCharset();
5960
this.endl = "\n";
6061
this.indent = " ";
@@ -117,12 +118,12 @@ public void setResultPom(Document resultPom) {
117118
this.resultPom = resultPom;
118119
}
119120

120-
public File getFile() {
121-
return file;
121+
public Path getPath() {
122+
return path;
122123
}
123124

124-
public void setFile(File file) {
125-
this.file = file;
125+
public void setPath(Path file) {
126+
this.path = file;
126127
}
127128

128129
public Charset getCharset() {

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/operator/POMDocumentFactory.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package io.codemodder.plugins.maven.operator;
22

33
import java.io.ByteArrayInputStream;
4-
import java.io.File;
54
import java.io.IOException;
65
import java.io.InputStream;
76
import java.net.URISyntaxException;
87
import java.net.URL;
8+
import java.nio.file.Path;
99
import org.apache.commons.io.IOUtils;
1010
import org.dom4j.Document;
1111
import org.dom4j.DocumentException;
@@ -41,15 +41,15 @@ public static POMDocument load(InputStream is)
4141
/**
4242
* Loads a POM document from the provided file.
4343
*
44-
* @param f The file representing the POM document.
44+
* @param filePath The file representing the POM document.
4545
* @return A new instance of {@link POMDocument} representing the loaded POM.
4646
* @throws IOException If an I/O error occurs while reading the file.
4747
* @throws DocumentException If an error occurs while parsing the POM document.
4848
* @throws URISyntaxException If there is an issue with the URI syntax.
4949
*/
50-
public static POMDocument load(File f) throws IOException, DocumentException, URISyntaxException {
51-
52-
URL fileUrl = f.toURI().toURL();
50+
public static POMDocument load(Path filePath)
51+
throws IOException, DocumentException, URISyntaxException {
52+
URL fileUrl = filePath.toUri().toURL();
5353
return load(fileUrl);
5454
}
5555

plugins/codemodder-plugin-maven/src/main/java/io/codemodder/plugins/maven/operator/POMOperator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class POMOperator {
1616
private final POMScanner pomScanner;
1717

1818
public POMOperator(final Path pomFile, final Path projectDir) {
19-
this.pomScanner = new POMScanner(pomFile.toFile(), projectDir.toFile());
19+
this.pomScanner = new POMScanner(pomFile, projectDir);
2020
}
2121

2222
public POMScanner getPomScanner() {
@@ -42,7 +42,7 @@ public ProjectModel addDependency(final DependencyGAV newDependencyGAV)
4242
.withDependency(newDependency)
4343
.withSkipIfNewer(true)
4444
.withUseProperties(true)
45-
.withRepositoryPath(FileUtils.createTempDirectoryWithPermissions().toFile())
45+
.withRepositoryPath(FileUtils.createTempDirectoryWithPermissions())
4646
.build();
4747

4848
return modify(projectModel) ? projectModel : null;
@@ -65,7 +65,7 @@ public Collection<DependencyGAV> getAllFoundDependencies()
6565
pomScanner
6666
.scanFrom()
6767
.withSafeQueryType()
68-
.withRepositoryPath(FileUtils.createTempDirectoryWithPermissions().toFile())
68+
.withRepositoryPath(FileUtils.createTempDirectoryWithPermissions())
6969
.build();
7070

7171
final Collection<Dependency> foundDependencies = queryDependency(originalProjectModel);

0 commit comments

Comments
 (0)