Bug 574449: compiler should not create parent dirs for non-file URIs.
Change-Id: I05c28e0d95a843e711f8b4a284e94ddf188aed11
Signed-off-by: Dennis Hendriks <dh_tue@hotmail.com>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/185634
Tested-by: JDT Bot <jdt-bot@eclipse.org>
Reviewed-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/org.eclipse.jdt.compiler.tool.tests/src/org/eclipse/jdt/compiler/tool/tests/InMemoryCompilationTest.java b/org.eclipse.jdt.compiler.tool.tests/src/org/eclipse/jdt/compiler/tool/tests/InMemoryCompilationTest.java
index 82ad5fe..3d699a4 100644
--- a/org.eclipse.jdt.compiler.tool.tests/src/org/eclipse/jdt/compiler/tool/tests/InMemoryCompilationTest.java
+++ b/org.eclipse.jdt.compiler.tool.tests/src/org/eclipse/jdt/compiler/tool/tests/InMemoryCompilationTest.java
@@ -16,6 +16,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -86,6 +87,14 @@
assertEquals("Hello world", actual);
}
+ @Test
+ public void testBug574449()
+ throws ReflectiveOperationException, IllegalArgumentException, SecurityException {
+ testInMemoryCompilationStaticMethod();
+ File file = new File("/my/pkg/");
+ assertEquals(false, file.isDirectory());
+ }
+
private Class<?> compile(String absClassName, String sourceCode) throws ClassNotFoundException {
InMemoryJavaSourceFileObject sourceFileObject = new InMemoryJavaSourceFileObject(absClassName, sourceCode);
List<InMemoryJavaSourceFileObject> sources = Arrays.asList(sourceFileObject);
diff --git a/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/EclipseCompilerImpl.java b/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/EclipseCompilerImpl.java
index 3e2c238..6af082f 100644
--- a/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/EclipseCompilerImpl.java
+++ b/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/EclipseCompilerImpl.java
@@ -13,6 +13,7 @@
* IBM Corporation - fix for 342936
* Kenneth Olson - Contribution for bug 188796 - [jsr199] Using JSR199 to extend ECJ
* Dennis Hendriks - Contribution for bug 188796 - [jsr199] Using JSR199 to extend ECJ
+ * Dennis Hendriks - fix for bug 574449.
* Frits Jalvingh - fix for bug 533830.
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.tool;
@@ -22,6 +23,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
+import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -451,13 +453,16 @@
currentFolder.mkdirs();
}
} else {
- // create the subfolfers is necessary
+ // create the subfolders if necessary
// need a way to retrieve the folders to create
- String path = javaFileForOutput.toUri().getPath();
- int index = path.lastIndexOf('/');
- if (index != -1) {
- File file = new File(path.substring(0, index));
- file.mkdirs();
+ URI uri = javaFileForOutput.toUri();
+ if (uri.getScheme() == null || uri.getScheme().equals("file")) { //$NON-NLS-1$
+ String path = uri.getPath();
+ int index = path.lastIndexOf('/');
+ if (index != -1) {
+ File file = new File(path.substring(0, index));
+ file.mkdirs();
+ }
}
}
}