Skip to content

Commit

Permalink
[performance][test] avoid File.getCanonicalPath()
Browse files Browse the repository at this point in the history
File.getCanonicalPath() is a IO Operation that is costly especially on
windows OS.
Instead use a NON-IO .toPath().normalize().toAbsolutePath().toString()

https://bugs.eclipse.org/bugs/show_bug.cgi?id=571614
  • Loading branch information
jukzi committed Dec 16, 2024
1 parent b5c15f7 commit aa2310a
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void test_github844() throws IOException {
+ " last round: false\n"
+ "Discovered processor service org.eclipse.jdt.apt.tests.external.annotations.batch.BatchGenProcessor\n"
+ " supporting [org.eclipse.jdt.apt.tests.external.annotations.batch.BatchGen]\n"
+ " in jar:" + Path.of(_extJar.getCanonicalPath()).toUri().toURL().toString() +"!/\n"
+ " in jar:" + Path.of(_extJar.toPath().normalize().toAbsolutePath().toString()).toUri().toURL().toString() +"!/\n"
+ "Processor org.eclipse.jdt.apt.tests.external.annotations.batch.BatchGenProcessor matches [org.eclipse.jdt.apt.tests.external.annotations.batch.BatchGen] and returns true\n"
+ "Round 2:\n"
+ " input files: {p1.gen.Class1,p1.gen.Class2,p1.Class0}\n"
Expand Down Expand Up @@ -178,7 +178,7 @@ public class Works2 {
},
"Discovered processor service org.eclipse.jdt.apt.tests.external.annotations.batch.BatchGenProcessor\n" +
" supporting [org.eclipse.jdt.apt.tests.external.annotations.batch.BatchGen]\n" +
" in jar:" + Path.of(_extJar.getCanonicalPath()).toUri().toURL().toString() + "!/\n" +
" in jar:" + Path.of(_extJar.toPath().normalize().toAbsolutePath().toString()).toUri().toURL().toString() + "!/\n" +
"Processor org.eclipse.jdt.apt.tests.external.annotations.batch.BatchGenProcessor matches [org.eclipse.jdt.apt.tests.external.annotations.batch.BatchGen] and returns true\n" +
"Processor org.eclipse.jdt.apt.tests.external.annotations.batch.BatchGenProcessor matches [] and returns false\n",
"" /* expectedErrOutputString */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ public static String setupProcessorJar(String processorJar, String tmpDir) throw
File destinationDir = new File(tmpDir);
File destinationFile = new File(destinationDir, processorJar);
copyResource(libFile, destinationFile);
return destinationFile.getCanonicalPath();
return destinationFile.toPath().normalize().toAbsolutePath().toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ public void testFileManager2() {


public void testCompilerUnusedVariable() throws Exception {
String tmpFolder = new File(System.getProperty("java.io.tmpdir")).getCanonicalPath();
String tmpFolder = new File(System.getProperty("java.io.tmpdir")).toPath().normalize().toAbsolutePath().toString();
File inputFile = new File(tmpFolder, "NoWarn.java");
BufferedWriter writer = null;
try {
Expand Down Expand Up @@ -1208,7 +1208,7 @@ public void testCompilerUnusedVariable() throws Exception {
assertEquals("Expected no warnings to be generated.", "", stringWriter.toString());
}
public void testCompilerUnusedVariable2() throws Exception {
String tmpFolder = new File(System.getProperty("java.io.tmpdir")).getCanonicalPath();
String tmpFolder = new File(System.getProperty("java.io.tmpdir")).toPath().normalize().toAbsolutePath().toString();
File inputFile = new File(tmpFolder, "NoWarn.java");
BufferedWriter writer = null;
try {
Expand Down Expand Up @@ -1264,7 +1264,7 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
}

private void suppressTest(String fileName, String source, String expectedDiagnostics, String expectedOutput) throws Exception {
String tmpFolder = new File(System.getProperty("java.io.tmpdir")).getCanonicalPath();
String tmpFolder = new File(System.getProperty("java.io.tmpdir")).toPath().normalize().toAbsolutePath().toString();
File inputFile = new File(tmpFolder, fileName);
BufferedWriter writer = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ private File copyFiles(String path, boolean reimportIfExists) throws IOException
try {
Files.copy(from.toPath(), to.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
ILog.get().error("Failed to copy the file - " + from.getCanonicalPath(), e); //$NON-NLS-1$
ILog.get().error("Failed to copy the file - " + from.toPath().normalize().toAbsolutePath().toString(), e); //$NON-NLS-1$
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void traverse (File f) throws IOException {
}
} else {
if (f.getName().endsWith(".java")) {
System.out.println(f.getCanonicalPath());
System.out.println(f.toPath().normalize().toAbsolutePath().toString());
char [] contents = new char[(int) f.length()];
FileInputStream fs = new FileInputStream(f);
InputStreamReader isr = null;
Expand All @@ -222,7 +222,7 @@ void traverse (File f) throws IOException {
if (isr != null) isr.close();
}
isr.read(contents);
checkParse(contents, null, f.getCanonicalPath(), null);
checkParse(contents, null, f.toPath().normalize().toAbsolutePath().toString(), null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ static class JavacCompiler {
JavacCompiler(String rootDirectoryPath, String rawVersion) throws IOException, InterruptedException {
this.rootDirectoryPath = rootDirectoryPath;
this.javacPathName = new File(rootDirectoryPath + File.separator
+ "bin" + File.separator + JAVAC_NAME).getCanonicalPath();
+ "bin" + File.separator + JAVAC_NAME).toPath().normalize().toAbsolutePath().toString();
// WORK don't need JAVAC_NAME any more; suppress this as we work towards code cleanup
if (rawVersion == null) {
rawVersion = getVersion(this.javacPathName);
Expand Down Expand Up @@ -655,7 +655,7 @@ public static JavaRuntime fromCurrentVM() throws IOException, InterruptedExcepti
private JavaRuntime(String rootDirectoryPath, String version, String rawVersion, int minor) throws IOException, InterruptedException {
this.rootDirectoryPath = rootDirectoryPath;
this.javaPathName = new File(this.rootDirectoryPath + File.separator
+ "bin" + File.separator + JAVA_NAME).getCanonicalPath();
+ "bin" + File.separator + JAVA_NAME).toPath().normalize().toAbsolutePath().toString();
this.version = version;
this.rawVersion = rawVersion;
this.minor = minor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void test3() throws IOException {
}

try {
final String canonicalPath = file.getCanonicalPath();
final String filePath = file.toPath().normalize().toAbsolutePath().toString();
final CompilationUnit[] units = new CompilationUnit[1];

FileASTRequestor requestor = new FileASTRequestor() {
Expand All @@ -226,15 +226,15 @@ public void acceptBinding(String bindingKey, IBinding binding) {
}
}
public void acceptAST(String sourceFilePath, CompilationUnit ast) {
if (canonicalPath.equals(sourceFilePath)) {
if (filePath.equals(sourceFilePath)) {
units[0] = ast;
}
}
};

parser.setEnvironment(null, new String[] { rootDir.getCanonicalPath() }, null, true);
parser.setEnvironment(null, new String[] { rootDir.toPath().normalize().toAbsolutePath().toString() }, null, true);

parser.createASTs(new String[] {canonicalPath}, null, new String[] {key}, requestor, null);
parser.createASTs(new String[] {filePath}, null, new String[] {key}, requestor, null);

assertNotNull("No binding", bindings[0]);
assertEquals("Wrong type of binding", IBinding.TYPE, bindings[0].getKind());
Expand Down Expand Up @@ -327,7 +327,7 @@ public void test6() throws IOException {
}

try {
final String canonicalPath = file.getCanonicalPath();
final String filePath = file.toPath().normalize().toAbsolutePath().toString();
final CompilationUnit[] units = new CompilationUnit[1];

FileASTRequestor requestor = new FileASTRequestor() {
Expand All @@ -341,15 +341,15 @@ public void acceptBinding(String bindingKey, IBinding binding) {
}
}
public void acceptAST(String sourceFilePath, CompilationUnit ast) {
if (canonicalPath.equals(sourceFilePath)) {
if (filePath.equals(sourceFilePath)) {
units[0] = ast;
}
}
};

parser.setEnvironment(null, new String[] { rootDir.getCanonicalPath() }, null, true);
parser.setEnvironment(null, new String[] { rootDir.toPath().normalize().toAbsolutePath().toString() }, null, true);

parser.createASTs(new String[] {canonicalPath}, null, new String[] {key}, requestor, null);
parser.createASTs(new String[] {filePath}, null, new String[] {key}, requestor, null);

assertNotNull("No binding", bindings[0]);
assertEquals("Wrong type of binding", IBinding.TYPE, bindings[0].getKind());
Expand Down Expand Up @@ -426,7 +426,7 @@ public void testBug415066_001() throws IOException {
}

try {
final String canonicalPath = fileY.getCanonicalPath();
final String fileYPath = fileY.toPath().normalize().toAbsolutePath().toString();
final CompilationUnit[] units = new CompilationUnit[1];

FileASTRequestor requestor = new FileASTRequestor() {
Expand All @@ -440,15 +440,15 @@ public void acceptBinding(String bindingKey, IBinding binding) {
}
}
public void acceptAST(String sourceFilePath, CompilationUnit ast) {
if (canonicalPath.equals(sourceFilePath)) {
if (fileYPath.equals(sourceFilePath)) {
units[0] = ast;
}
}
};

parser.setEnvironment(null, new String[] { rootDir.getCanonicalPath() }, null, true);
parser.setEnvironment(null, new String[] { rootDir.toPath().normalize().toAbsolutePath().toString() }, null, true);
org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.MAX_AT_ONCE = 0;
parser.createASTs(new String[] {canonicalPath}, null, new String[] {key}, requestor, null);
parser.createASTs(new String[] {fileYPath}, null, new String[] {key}, requestor, null);
assertNotNull("No ast", units[0]);
assertEquals("No problem", 0, units[0].getProblems().length);
} finally {
Expand Down Expand Up @@ -515,7 +515,7 @@ public void testBug415066_002() throws IOException {
}

try {
final String canonicalPath = fileY.getCanonicalPath();
final String fileYPath = fileY.toPath().normalize().toAbsolutePath().toString();
final CompilationUnit[] units = new CompilationUnit[1];

FileASTRequestor requestor = new FileASTRequestor() {
Expand All @@ -529,14 +529,14 @@ public void acceptBinding(String bindingKey, IBinding binding) {
}
}
public void acceptAST(String sourceFilePath, CompilationUnit ast) {
if (canonicalPath.equals(sourceFilePath)) {
if (fileYPath.equals(sourceFilePath)) {
units[0] = ast;
}
}
};

parser.setEnvironment(null, new String[] { rootDir.getCanonicalPath() }, null, true);
parser.createASTs(new String[] {canonicalPath}, null, new String[] {key}, requestor, null);
parser.setEnvironment(null, new String[] { rootDir.toPath().normalize().toAbsolutePath().toString() }, null, true);
parser.createASTs(new String[] {fileYPath}, null, new String[] {key}, requestor, null);
assertNotNull("No ast", units[0]);
IProblem[] problems = units[0].getProblems();
assertEquals("No problem", 1, problems.length);
Expand Down Expand Up @@ -596,7 +596,7 @@ public void test7() throws IOException {
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setCompilerOptions(JavaCore.getOptions());
parser.createASTs(
new String[] { file.getCanonicalPath(), fileY.getCanonicalPath() },
new String[] { file.toPath().normalize().toAbsolutePath().toString(), fileY.toPath().normalize().toAbsolutePath().toString() },
null,
new String[] {},
new FileASTRequestor() {},
Expand Down Expand Up @@ -770,18 +770,9 @@ public void acceptAST(String sourceFilePath, CompilationUnit ast) {
parser.setBindingsRecovery(true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setEnvironment(new String[0], new String[] { rootDir.getAbsolutePath() }, null, true);
String[] files = null;
try {
files = new String[] {file.getCanonicalPath(), fileY.getCanonicalPath()};
parser.createASTs(files,
null,
new String[0],
astRequestor,
null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] files = new String[] { file.toPath().normalize().toAbsolutePath().toString(),
fileY.toPath().normalize().toAbsolutePath().toString() };
parser.createASTs(files, null, new String[0], astRequestor, null);
} finally {
file.delete();
fileY.delete();
Expand Down Expand Up @@ -1576,18 +1567,9 @@ public void acceptAST(String sourceFilePath, CompilationUnit ast) {
parser.setBindingsRecovery(true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setEnvironment(new String[0], new String[] { rootDir.getAbsolutePath() }, null, true);
String[] files = null;
try {
files = new String[] {file.getCanonicalPath(), fileY.getCanonicalPath()};
parser.createASTs(files,
null,
new String[0],
astRequestor,
null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] files = new String[] { file.toPath().normalize().toAbsolutePath().toString(),
fileY.toPath().normalize().toAbsolutePath().toString() };
parser.createASTs(files, null, new String[0], astRequestor, null);
} finally {
file.delete();
fileY.delete();
Expand Down Expand Up @@ -1652,12 +1634,12 @@ public void testBug482254() throws IOException {
File packageDir = new File(rootDir, "p");
packageDir.mkdir();
File fileY = new File(packageDir, "Y.java");
String canonicalPath = fileY.getCanonicalPath();
String fileYPath = fileY.toPath().normalize().toAbsolutePath().toString();

packageDir = new File(rootDir, "p");
packageDir.mkdir();
fileY = new File(packageDir, "Z.java");
String canonicalPath2 = fileY.getCanonicalPath();
String fileZPath = fileY.toPath().normalize().toAbsolutePath().toString();

contents =
"enum X {\n" +
Expand Down Expand Up @@ -1687,7 +1669,7 @@ public void testBug482254() throws IOException {
parser.setResolveBindings(true);
parser.setCompilerOptions(JavaCore.getOptions());
parser.createASTs(
new String[] { file.getCanonicalPath(), canonicalPath, canonicalPath2, file2.getCanonicalPath() },
new String[] { file.toPath().normalize().toAbsolutePath().toString(), fileYPath, fileZPath, file2.toPath().normalize().toAbsolutePath().toString() },
null,
new String[] {},
new FileASTRequestor() {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,7 @@ public class Util {

// Computed test run directory name based on current time
File dateDir = new File(dir, "run."+System.currentTimeMillis());
String pathDir = null;
try {
pathDir = dateDir.getCanonicalPath();
} catch (IOException e) {
pathDir = dateDir.getAbsolutePath();
}
OUTPUT_DIRECTORY = pathDir;
OUTPUT_DIRECTORY = dateDir.toPath().normalize().toAbsolutePath().toString();
}

public static void appendProblem(StringBuilder problems, IProblem problem, char[] source, int problemCount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2669,17 +2669,13 @@ protected String getExternalJCLSourcePathString(String compliance, boolean useFu
*/
protected String getExternalPath() {
if (EXTERNAL_JAR_DIR_PATH == null) {
try {
String path = getWorkspaceRoot().getLocation().toFile().getParentFile().getCanonicalPath();
if (path.charAt(path.length()-1) != File.separatorChar) {
path += File.separatorChar;
}
EXTERNAL_JAR_DIR_PATH = path;
System.out.println("EXTERNAL_JAR_DIR_PATH=" + EXTERNAL_JAR_DIR_PATH);
System.out.println("EXTERNAL_JAR_DIR_PATH writable? " + Files.isWritable(Paths.get(EXTERNAL_JAR_DIR_PATH)));
} catch (IOException e) {
throw new IllegalStateException(e);
String path = getWorkspaceRoot().getLocation().toFile().getParentFile().toPath().normalize().toAbsolutePath().toString();
if (path.charAt(path.length()-1) != File.separatorChar) {
path += File.separatorChar;
}
EXTERNAL_JAR_DIR_PATH = path;
System.out.println("EXTERNAL_JAR_DIR_PATH=" + EXTERNAL_JAR_DIR_PATH);
System.out.println("EXTERNAL_JAR_DIR_PATH writable? " + Files.isWritable(Paths.get(EXTERNAL_JAR_DIR_PATH)));
}
return EXTERNAL_JAR_DIR_PATH;
}
Expand All @@ -2688,15 +2684,12 @@ protected String getExternalPath() {
* This path ends with a File.separatorChar.
*/
protected String getWorkspacePath() {
if (WORKSPACE_DIR_PATH == null)
try {
String path = getWorkspaceRoot().getLocation().toFile().getCanonicalPath();
if (path.charAt(path.length()-1) != File.separatorChar)
path += File.separatorChar;
WORKSPACE_DIR_PATH = path;
} catch (IOException e) {
e.printStackTrace();
}
if (WORKSPACE_DIR_PATH == null) {
String path = getWorkspaceRoot().getLocation().toFile().toPath().normalize().toAbsolutePath().toString();
if (path.charAt(path.length()-1) != File.separatorChar)
path += File.separatorChar;
WORKSPACE_DIR_PATH = path;
}
return WORKSPACE_DIR_PATH;
}
protected IFile getFile(String path) {
Expand Down Expand Up @@ -3410,7 +3403,7 @@ protected IJavaProject setUpJavaProject(final String projectName, String complia
protected IJavaProject setUpJavaProject(final String projectName, String compliance, boolean useFullJCL) throws CoreException, IOException {
// copy files in project from source workspace to target workspace
String sourceWorkspacePath = getSourceWorkspacePath();
String targetWorkspacePath = getWorkspaceRoot().getLocation().toFile().getCanonicalPath();
String targetWorkspacePath = getWorkspaceRoot().getLocation().toFile().toPath().normalize().toAbsolutePath().toString();
copyDirectory(new File(sourceWorkspacePath, projectName), new File(targetWorkspacePath, projectName));

// ensure variables are set
Expand Down
Loading

0 comments on commit aa2310a

Please sign in to comment.