make sourcepath checks pass for newer builds of javac9 (IDEA-172212)

This commit is contained in:
Eugene Zhuravlev
2017-05-04 15:43:40 +02:00
parent 7ddc0910f5
commit b219c37ac8
2 changed files with 21 additions and 13 deletions
@@ -165,11 +165,14 @@ class JavacFileManager extends ForwardingJavaFileManager<StandardJavaFileManager
if (mySourceTransformers.isEmpty()) {
return originalObjects;
}
final List<JavaFileObject> wrapped = new ArrayList<JavaFileObject>();
List<JavaFileObject> wrapped = null;
for (JavaFileObject fo : originalObjects) {
if (wrapped == null) {
wrapped = new ArrayList<JavaFileObject>(); // lazy init
}
wrapped.add(JavaFileObject.Kind.SOURCE.equals(fo.getKind())? new TransformableJavaFileObject(fo, mySourceTransformers) : fo);
}
return wrapped;
return wrapped != null? wrapped : originalObjects;
}
@Override
@@ -72,8 +72,9 @@ public class JavacMain {
}
final boolean usingJavac = compilingTool instanceof JavacCompilerTool;
final boolean javacBefore9 = isJavacBefore9(compilingTool);
final JavacFileManager fileManager = new JavacFileManager(
new ContextImpl(compiler, diagnosticConsumer, outputSink, canceledStatus, canUseOptimizedFileManager(compilingTool)), JavaSourceTransformer.getTransformers()
new ContextImpl(compiler, diagnosticConsumer, outputSink, canceledStatus, javacBefore9), JavaSourceTransformer.getTransformers()
);
if (!platformClasspath.isEmpty()) {
@@ -140,15 +141,19 @@ public class JavacMain {
}
}
try {
// ensure the source path is set;
// otherwise, if not set, javac attempts to search both classes and sources in classpath;
// so if some classpath jars contain sources, it will attempt to compile them
fileManager.setLocation(StandardLocation.SOURCE_PATH, sourcePath);
}
catch (IOException e) {
fileManager.getContext().reportMessage(Diagnostic.Kind.ERROR, e.getMessage());
return false;
if (javacBefore9 || !sourcePath.isEmpty()) {
try {
// ensure the source path is set;
// otherwise, if not set, javac attempts to search both classes and sources in classpath;
// so if some classpath jars contain sources, it will attempt to compile them
// starting from javac9 it seems that setting empty source path may affect module compilation logic, so starting from javac9
// we avoid forcing empty sourcepath
fileManager.setLocation(StandardLocation.SOURCE_PATH, sourcePath);
}
catch (IOException e) {
fileManager.getContext().reportMessage(Diagnostic.Kind.ERROR, e.getMessage());
return false;
}
}
//noinspection IOResourceOpenedButNotSafelyClosed
@@ -227,7 +232,7 @@ public class JavacMain {
});
}
private static boolean canUseOptimizedFileManager(JavaCompilingTool compilingTool) {
private static boolean isJavacBefore9(JavaCompilingTool compilingTool) {
// since java 9 internal API's used by the optimizedFileManager have changed
return compilingTool instanceof JavacCompilerTool && (JAVA_RUNTIME_VERSION.startsWith("1.8.") || JAVA_RUNTIME_VERSION.startsWith("1.7.") || JAVA_RUNTIME_VERSION.startsWith("1.6."));
}