diff options
| author | Jay Arthanareeswaran | 2021-06-21 07:12:44 +0000 |
|---|---|---|
| committer | Jay Arthanareeswaran | 2021-06-22 08:15:55 +0000 |
| commit | 25d339c3f5d58a95bc58915cac82a2b86d2f215f (patch) | |
| tree | 3514f184562a8792a5443552ba83c4c89c5d0607 | |
| parent | d6ba5f54ae01cbba0db87564433b1a8a0c95ec16 (diff) | |
| download | eclipse.jdt.core-25d339c3f5d58a95bc58915cac82a2b86d2f215f.tar.gz eclipse.jdt.core-25d339c3f5d58a95bc58915cac82a2b86d2f215f.tar.xz eclipse.jdt.core-25d339c3f5d58a95bc58915cac82a2b86d2f215f.zip | |
Bug 574181 - JSR-199 EclipseCompiler: --release 8 with JAVA_HOMEI20210622-1800
pointing to Java 11 JDK generates calls to Java 11 library API
Change-Id: I5a515db8614d2266ca1257a43c1c3d2dbea3318e
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/182280
Tested-by: JDT Bot <jdt-bot@eclipse.org>
2 files changed, 411 insertions, 305 deletions
diff --git a/org.eclipse.jdt.compiler.tool.tests/src/org/eclipse/jdt/compiler/tool/tests/CompilerToolJava9Tests.java b/org.eclipse.jdt.compiler.tool.tests/src/org/eclipse/jdt/compiler/tool/tests/CompilerToolJava9Tests.java index 18131359e8..03f003bad8 100644 --- a/org.eclipse.jdt.compiler.tool.tests/src/org/eclipse/jdt/compiler/tool/tests/CompilerToolJava9Tests.java +++ b/org.eclipse.jdt.compiler.tool.tests/src/org/eclipse/jdt/compiler/tool/tests/CompilerToolJava9Tests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017, 2020 IBM Corporation and others. + * Copyright (c) 2017, 2021 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -32,6 +32,7 @@ import java.util.List; import java.util.Locale; import java.util.ServiceLoader; import java.util.Set; +import java.util.function.Consumer; import javax.lang.model.SourceVersion; import javax.tools.Diagnostic; @@ -66,6 +67,7 @@ public class CompilerToolJava9Tests extends TestCase { private JavaCompiler[] compilers; private String[] compilerNames; private boolean isJREBelow9; + private boolean isJREBelow12; private static String _tmpFolder; private static String _tmpSrcFolderName; private static File _tmpSrcDir; @@ -83,6 +85,7 @@ public class CompilerToolJava9Tests extends TestCase { this.isJREBelow9 = SourceVersion.latest().compareTo(SourceVersion.RELEASE_8) <= 0; if (isJREBelow9) return; + isJREBelow12 = SourceVersion.latest().compareTo(SourceVersion.RELEASE_11) <= 0; this.compilers = new JavaCompiler[2]; this.compilerNames = new String[2]; ServiceLoader<JavaCompiler> javaCompilerLoader = ServiceLoader.load(JavaCompiler.class, EclipseCompiler.class.getClassLoader()); @@ -214,19 +217,8 @@ public class CompilerToolJava9Tests extends TestCase { } } } - public void testOptionRelease1() throws IOException { - if (this.isJREBelow9) return; - JavaCompiler compiler = this.compilers[1]; - String tmpFolder = _tmpFolder; - File inputFile = new File(tmpFolder, "X.java"); - try (Writer writer = new BufferedWriter(new FileWriter(inputFile))) { - writer.write( - "package p;\n" + - "public class X {}"); - } - StandardJavaFileManager manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); - - ForwardingJavaFileManager<StandardJavaFileManager> forwardingJavaFileManager = new ForwardingJavaFileManager<StandardJavaFileManager>(manager) { + public ForwardingJavaFileManager<JavaFileManager> getFileManager(JavaFileManager manager) { + return new ForwardingJavaFileManager<JavaFileManager>(manager) { @Override public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException { @@ -259,301 +251,357 @@ public class CompilerToolJava9Tests extends TestCase { return javaFileForOutput; } }; - // create new list containing input file - List<File> files = new ArrayList<File>(); - files.add(inputFile); - Iterable<? extends JavaFileObject> units = manager.getJavaFileObjectsFromFiles(files); - StringWriter stringWriter = new StringWriter(); - PrintWriter printWriter = new PrintWriter(stringWriter); - - List<String> options = new ArrayList<String>(); - options.add("-d"); - options.add(tmpFolder); - options.add("--release"); - options.add("8"); - ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); - PrintWriter err = new PrintWriter(errBuffer); - CompilerInvocationDiagnosticListener listener = new CompilerInvocationDiagnosticListener(err) { - @Override - public void report(Diagnostic<? extends JavaFileObject> diagnostic) { - JavaFileObject source = diagnostic.getSource(); - assertNotNull("No source", source); - super.report(diagnostic); + } + public void testOptionRelease1() throws IOException { + if (this.isJREBelow9) return; + JavaCompiler compiler = this.compilers[1]; + StandardJavaFileManager standardManager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); + Consumer<JavaFileManager> cons = (manager) -> { + String tmpFolder = _tmpFolder; + File inputFile = new File(tmpFolder, "X.java"); + try (Writer writer = new BufferedWriter(new FileWriter(inputFile))) { + writer.write( + "package p;\n" + + "public class X {}"); + } catch (IOException e1) { + e1.printStackTrace(); } + List<File> files = new ArrayList<File>(); + files.add(inputFile); + Iterable<? extends JavaFileObject> units = standardManager.getJavaFileObjectsFromFiles(files); + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + + List<String> options = new ArrayList<String>(); + options.add("-d"); + options.add(tmpFolder); + options.add("--release"); + options.add("8"); + ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); + PrintWriter err = new PrintWriter(errBuffer); + CompilerInvocationDiagnosticListener listener = new CompilerInvocationDiagnosticListener(err) { + @Override + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { + JavaFileObject source = diagnostic.getSource(); + assertNotNull("No source", source); + super.report(diagnostic); + } + }; + CompilationTask task = compiler.getTask(printWriter, getFileManager(manager), listener, options, null, units); + // check the classpath location + assertTrue("Has no location CLASS_OUPUT", getFileManager(manager).hasLocation(StandardLocation.CLASS_OUTPUT)); + Boolean result = task.call(); + printWriter.flush(); + printWriter.close(); + if (!result.booleanValue()) { + System.err.println("Compilation failed: " + stringWriter.getBuffer().toString()); + assertTrue("Compilation failed ", false); + } + ClassFileReader reader = null; + try { + reader = ClassFileReader.read(new File(tmpFolder, "p/X.class"), true); + } catch (ClassFormatException e) { + assertTrue("Should not happen", false); + } catch (IOException e) { + assertTrue("Should not happen", false); + } + assertNotNull("No reader", reader); + // This needs fix. This test case by design will produce different output every compiler version. + assertEquals("Wrong value", ClassFileConstants.JDK1_8, reader.getVersion()); + // check that the .class file exist for X + assertTrue("delete failed", inputFile.delete()); }; - CompilationTask task = compiler.getTask(printWriter, forwardingJavaFileManager, listener, options, null, units); - // check the classpath location - assertTrue("Has no location CLASS_OUPUT", forwardingJavaFileManager.hasLocation(StandardLocation.CLASS_OUTPUT)); - Boolean result = task.call(); - printWriter.flush(); - printWriter.close(); - if (!result.booleanValue()) { - System.err.println("Compilation failed: " + stringWriter.getBuffer().toString()); - assertTrue("Compilation failed ", false); - } - ClassFileReader reader = null; - try { - reader = ClassFileReader.read(new File(tmpFolder, "p/X.class"), true); - } catch (ClassFormatException e) { - assertTrue("Should not happen", false); - } catch (IOException e) { - assertTrue("Should not happen", false); - } - assertNotNull("No reader", reader); - // This needs fix. This test case by design will produce different output every compiler version. - assertEquals("Wrong value", ClassFileConstants.JDK1_8, reader.getVersion()); - // check that the .class file exist for X - assertTrue("delete failed", inputFile.delete()); + cons.accept(standardManager); + cons.accept(getFileManager(standardManager)); } public void testOptionRelease2() throws IOException { if (this.isJREBelow9) return; JavaCompiler compiler = this.compilers[1]; - String tmpFolder = _tmpFolder; - File inputFile = new File(tmpFolder, "X.java"); - try (Writer writer = new BufferedWriter(new FileWriter(inputFile))){ - writer.write( - "package p;\n" + - "public class X {}"); - } - StandardJavaFileManager manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); - - ForwardingJavaFileManager<StandardJavaFileManager> forwardingJavaFileManager = new ForwardingJavaFileManager<StandardJavaFileManager>(manager) { - @Override - public FileObject getFileForInput(Location location, String packageName, String relativeName) - throws IOException { - if (DEBUG) { - System.out.println("Create file for input : " + packageName + " " + relativeName + " in location " + location); - } - return super.getFileForInput(location, packageName, relativeName); - } - @Override - public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) - throws IOException { - if (DEBUG) { - System.out.println("Create java file for input : " + className + " in location " + location); - } - return super.getJavaFileForInput(location, className, kind); + StandardJavaFileManager standardManager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); + Consumer<JavaFileManager> cons = (manager) -> { + String tmpFolder = _tmpFolder; + File inputFile = new File(tmpFolder, "X.java"); + try (Writer writer = new BufferedWriter(new FileWriter(inputFile))){ + writer.write( + "package p;\n" + + "public class X {}"); + } catch (IOException e1) { + e1.printStackTrace(); } - @Override - public JavaFileObject getJavaFileForOutput(Location location, - String className, - Kind kind, - FileObject sibling) throws IOException { - - if (DEBUG) { - System.out.println("Create .class file for " + className + " in location " + location + " with sibling " + sibling.toUri()); - } - JavaFileObject javaFileForOutput = super.getJavaFileForOutput(location, className, kind, sibling); - if (DEBUG) { - System.out.println(javaFileForOutput.toUri()); + // create new list containing input file + List<File> files = new ArrayList<File>(); + files.add(inputFile); + Iterable<? extends JavaFileObject> units = standardManager.getJavaFileObjectsFromFiles(files); + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + + List<String> options = new ArrayList<String>(); + options.add("-d"); + options.add(tmpFolder); + options.add("--release"); + options.add("8"); + ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); + PrintWriter err = new PrintWriter(errBuffer); + CompilerInvocationDiagnosticListener listener = new CompilerInvocationDiagnosticListener(err) { + @Override + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { + JavaFileObject source = diagnostic.getSource(); + assertNotNull("No source", source); + super.report(diagnostic); } - return javaFileForOutput; - } - }; - // create new list containing input file - List<File> files = new ArrayList<File>(); - files.add(inputFile); - Iterable<? extends JavaFileObject> units = manager.getJavaFileObjectsFromFiles(files); - StringWriter stringWriter = new StringWriter(); - PrintWriter printWriter = new PrintWriter(stringWriter); - - List<String> options = new ArrayList<String>(); - options.add("-d"); - options.add(tmpFolder); - options.add("--release"); - options.add("8"); - ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); - PrintWriter err = new PrintWriter(errBuffer); - CompilerInvocationDiagnosticListener listener = new CompilerInvocationDiagnosticListener(err) { - @Override - public void report(Diagnostic<? extends JavaFileObject> diagnostic) { - JavaFileObject source = diagnostic.getSource(); - assertNotNull("No source", source); - super.report(diagnostic); + }; + CompilationTask task = compiler.getTask(printWriter, getFileManager(manager), listener, options, null, units); + // check the classpath location + assertTrue("Has no location CLASS_OUPUT", getFileManager(manager).hasLocation(StandardLocation.CLASS_OUTPUT)); + Boolean result = task.call(); + printWriter.flush(); + printWriter.close(); + if (!result.booleanValue()) { + System.err.println("Compilation failed: " + stringWriter.getBuffer().toString()); + assertTrue("Compilation failed ", false); + } + ClassFileReader reader = null; + try { + reader = ClassFileReader.read(new File(tmpFolder, "p/X.class"), true); + } catch (ClassFormatException e) { + assertTrue("Should not happen", false); + } catch (IOException e) { + assertTrue("Should not happen", false); } + assertNotNull("No reader", reader); + // This needs fix. This test case by design will produce different output every compiler version. + assertEquals("Wrong value", ClassFileConstants.JDK1_8, reader.getVersion()); + // check that the .class file exist for X + assertTrue("delete failed", inputFile.delete()); }; - CompilationTask task = compiler.getTask(printWriter, forwardingJavaFileManager, listener, options, null, units); - // check the classpath location - assertTrue("Has no location CLASS_OUPUT", forwardingJavaFileManager.hasLocation(StandardLocation.CLASS_OUTPUT)); - Boolean result = task.call(); - printWriter.flush(); - printWriter.close(); - if (!result.booleanValue()) { - System.err.println("Compilation failed: " + stringWriter.getBuffer().toString()); - assertTrue("Compilation failed ", false); - } - ClassFileReader reader = null; - try { - reader = ClassFileReader.read(new File(tmpFolder, "p/X.class"), true); - } catch (ClassFormatException e) { - assertTrue("Should not happen", false); - } catch (IOException e) { - assertTrue("Should not happen", false); - } - assertNotNull("No reader", reader); - // This needs fix. This test case by design will produce different output every compiler version. - assertEquals("Wrong value", ClassFileConstants.JDK1_8, reader.getVersion()); - // check that the .class file exist for X - assertTrue("delete failed", inputFile.delete()); + cons.accept(standardManager); + cons.accept(getFileManager(standardManager)); } public void testOptionRelease3() throws IOException { if (this.isJREBelow9) return; JavaCompiler compiler = this.compilers[1]; - String tmpFolder = _tmpFolder; - File inputFile = new File(tmpFolder, "X.java"); - try (Writer writer = new BufferedWriter(new FileWriter(inputFile))){ - writer.write( - "package p;\n" + - "public class X {}"); - } - StandardJavaFileManager manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); - - ForwardingJavaFileManager<StandardJavaFileManager> forwardingJavaFileManager = new ForwardingJavaFileManager<StandardJavaFileManager>(manager) { - @Override - public FileObject getFileForInput(Location location, String packageName, String relativeName) - throws IOException { - if (DEBUG) { - System.out.println("Create file for input : " + packageName + " " + relativeName + " in location " + location); - } - return super.getFileForInput(location, packageName, relativeName); + StandardJavaFileManager standardManager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); + Consumer<JavaFileManager> cons = (manager) -> { + String tmpFolder = _tmpFolder; + File inputFile = new File(tmpFolder, "X.java"); + try (Writer writer = new BufferedWriter(new FileWriter(inputFile))){ + writer.write( + "package p;\n" + + "public class X {}"); + } catch (IOException e) { + e.printStackTrace(); } - @Override - public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) - throws IOException { - if (DEBUG) { - System.out.println("Create java file for input : " + className + " in location " + location); - } - return super.getJavaFileForInput(location, className, kind); + + + // create new list containing input file + List<File> files = new ArrayList<File>(); + files.add(inputFile); + Iterable<? extends JavaFileObject> units = standardManager.getJavaFileObjectsFromFiles(files); + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + + List<String> options = new ArrayList<String>(); + options.add("-d"); + options.add(tmpFolder); + options.add("--release"); + options.add("8"); + options.add("-source"); + options.add("7"); + ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); + PrintWriter err = new PrintWriter(errBuffer); + CompilerInvocationDiagnosticListener listener = new CompilerInvocationDiagnosticListener(err); + try { + compiler.getTask(printWriter, getFileManager(manager), listener, options, null, units); + fail("compilation didn't fail as expected"); + } catch(IllegalArgumentException iae) { + assertEquals("option -source is not supported when --release is used", iae.getMessage()); } - @Override - public JavaFileObject getJavaFileForOutput(Location location, - String className, - Kind kind, - FileObject sibling) throws IOException { - - if (DEBUG) { - System.out.println("Create .class file for " + className + " in location " + location + " with sibling " + sibling.toUri()); - } - JavaFileObject javaFileForOutput = super.getJavaFileForOutput(location, className, kind, sibling); - if (DEBUG) { - System.out.println(javaFileForOutput.toUri()); - } - return javaFileForOutput; + // Now with standard file manager + try { + compiler.getTask(printWriter, manager, listener, options, null, units); + fail("compilation didn't fail as expected"); + } catch(IllegalArgumentException iae) { + assertEquals("option -source is not supported when --release is used", iae.getMessage()); } }; - // create new list containing input file - List<File> files = new ArrayList<File>(); - files.add(inputFile); - Iterable<? extends JavaFileObject> units = manager.getJavaFileObjectsFromFiles(files); - StringWriter stringWriter = new StringWriter(); - PrintWriter printWriter = new PrintWriter(stringWriter); - - List<String> options = new ArrayList<String>(); - options.add("-d"); - options.add(tmpFolder); - options.add("--release"); - options.add("8"); - options.add("-source"); - options.add("7"); - ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); - PrintWriter err = new PrintWriter(errBuffer); - CompilerInvocationDiagnosticListener listener = new CompilerInvocationDiagnosticListener(err); -// { bug 533830 removed: an option error has no source associated with it, so the below was copied wrongly. -// @Override -// public void report(Diagnostic<? extends JavaFileObject> diagnostic) { -// JavaFileObject source = diagnostic.getSource(); -// assertNotNull("No source", source); -// super.report(diagnostic); -// } -// }; - try { - compiler.getTask(printWriter, forwardingJavaFileManager, listener, options, null, units); - fail("compilation didn't fail as expected"); - } catch(IllegalArgumentException iae) { - assertEquals("option -source is not supported when --release is used", iae.getMessage()); - } + cons.accept(standardManager); + cons.accept(getFileManager(standardManager)); } public void testOptionRelease4() throws IOException { if (this.isJREBelow9) return; JavaCompiler compiler = this.compilers[1]; - String tmpFolder = _tmpFolder; - File inputFile = new File(tmpFolder, "X.java"); - try (Writer writer = new BufferedWriter(new FileWriter(inputFile))) { - writer.write( - "package p;\n" + - "import java.nio.Buffer;\n" + - "import java.nio.ByteBuffer;\n" + - "public class X {\n" + - " public Buffer buf() {\n" + - " ByteBuffer buffer = ByteBuffer.allocate(10);\n" + - " return buffer.flip();\n" + - " }\n" + - "}\n"); - } - StandardJavaFileManager manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); - - // create new list containing input file - List<File> files = new ArrayList<File>(); - files.add(inputFile); - Iterable<? extends JavaFileObject> units = manager.getJavaFileObjectsFromFiles(files); - StringWriter stringWriter = new StringWriter(); - PrintWriter printWriter = new PrintWriter(stringWriter); + StandardJavaFileManager standardManager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); + Consumer<JavaFileManager> cons = (manager) -> { + String tmpFolder = _tmpFolder; + File inputFile = new File(tmpFolder, "X.java"); + try (Writer writer = new BufferedWriter(new FileWriter(inputFile))) { + writer.write( + "package p;\n" + + "import java.nio.Buffer;\n" + + "import java.nio.ByteBuffer;\n" + + "public class X {\n" + + " public Buffer buf() {\n" + + " ByteBuffer buffer = ByteBuffer.allocate(10);\n" + + " return buffer.flip();\n" + + " }\n" + + "}\n"); + } catch (IOException e1) { + e1.printStackTrace(); + } - List<String> options = new ArrayList<String>(); - options.add("-d"); - options.add(tmpFolder); - options.add("--release"); - options.add("8"); - ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); - PrintWriter err = new PrintWriter(errBuffer); - CompilerInvocationDiagnosticListener listener = new CompilerInvocationDiagnosticListener(err) { - @Override - public void report(Diagnostic<? extends JavaFileObject> diagnostic) { - JavaFileObject source = diagnostic.getSource(); - assertNotNull("No source", source); - super.report(diagnostic); + // create new list containing input file + List<File> files = new ArrayList<File>(); + files.add(inputFile); + Iterable<? extends JavaFileObject> units = standardManager.getJavaFileObjectsFromFiles(files); + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + + List<String> options = new ArrayList<String>(); + options.add("-d"); + options.add(tmpFolder); + options.add("--release"); + options.add("8"); + ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); + PrintWriter err = new PrintWriter(errBuffer); + CompilerInvocationDiagnosticListener listener = new CompilerInvocationDiagnosticListener(err) { + @Override + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { + JavaFileObject source = diagnostic.getSource(); + assertNotNull("No source", source); + super.report(diagnostic); + } + }; + + CompilationTask task = compiler.getTask(printWriter, manager, listener, options, null, units); + // check the classpath location + Boolean result = task.call(); + printWriter.flush(); + printWriter.close(); + if (!result.booleanValue()) { + System.err.println("Compilation failed: " + stringWriter.getBuffer().toString()); + assertTrue("Compilation failed ", false); + } + ClassFileReader reader = null; + try { + reader = ClassFileReader.read(new File(tmpFolder, "p/X.class"), false); + } catch (ClassFormatException e) { + assertTrue("Should not happen", false); + } catch (IOException e) { + assertTrue("Should not happen", false); } + assertNotNull("No reader", reader); + // This needs fix. This test case by design will produce different output every compiler version. + assertEquals("Wrong value", ClassFileConstants.JDK1_8, reader.getVersion()); + // check that the correct call was generated: must return a Buffer, not a ByteBuffer + boolean found = false; + int[] offsets = reader.getConstantPoolOffsets(); + for (int i = 0; i < offsets.length; i++) { + int tag = reader.u1At(offsets[i]); + if (tag == ClassFileConstants.MethodRefTag || tag == ClassFileConstants.InterfaceMethodRefTag) { + char[] name = extractName(offsets, reader, i); + char[] className = extractClassName(offsets, reader, i); + String fullName = new String(className) + '.' + new String(name); + char[] typeName = extractType(offsets, reader, i); + if ("java/nio/ByteBuffer.flip".equals(fullName)) { + found = true; + assertEquals(fullName + "()Ljava/nio/Buffer;", fullName + new String(typeName)); + break; + } + } + } + assertTrue("No call to ByteBuffer.flip()", found); + // check that the .class file exist for X + assertTrue("delete failed", inputFile.delete()); }; - CompilationTask task = compiler.getTask(printWriter, manager, listener, options, null, units); - // check the classpath location - Boolean result = task.call(); - printWriter.flush(); - printWriter.close(); - if (!result.booleanValue()) { - System.err.println("Compilation failed: " + stringWriter.getBuffer().toString()); - assertTrue("Compilation failed ", false); - } - ClassFileReader reader = null; - try { - reader = ClassFileReader.read(new File(tmpFolder, "p/X.class"), false); - } catch (ClassFormatException e) { - assertTrue("Should not happen", false); - } catch (IOException e) { - assertTrue("Should not happen", false); - } - assertNotNull("No reader", reader); - // This needs fix. This test case by design will produce different output every compiler version. - assertEquals("Wrong value", ClassFileConstants.JDK1_8, reader.getVersion()); - // check that the correct call was generated: must return a Buffer, not a ByteBuffer - boolean found = false; - int[] offsets = reader.getConstantPoolOffsets(); - for (int i = 0; i < offsets.length; i++) { - int tag = reader.u1At(offsets[i]); - if (tag == ClassFileConstants.MethodRefTag || tag == ClassFileConstants.InterfaceMethodRefTag) { - char[] name = extractName(offsets, reader, i); - char[] className = extractClassName(offsets, reader, i); - String fullName = new String(className) + '.' + new String(name); - char[] typeName = extractType(offsets, reader, i); - if ("java/nio/ByteBuffer.flip".equals(fullName)) { - found = true; - assertEquals(fullName + "()Ljava/nio/Buffer;", fullName + new String(typeName)); - break; - } - } - } - assertTrue("No call to ByteBuffer.flip()", found); - // check that the .class file exist for X - assertTrue("delete failed", inputFile.delete()); + cons.accept(standardManager); + cons.accept(getFileManager(standardManager)); + } + public void testOptionRelease5() throws IOException { + if (this.isJREBelow12) return; + JavaCompiler compiler = this.compilers[1]; + StandardJavaFileManager standardManager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); + Consumer<JavaFileManager> cons = (manager) -> { + String tmpFolder = _tmpFolder; + File inputFile = new File(tmpFolder, "X.java"); + try (Writer writer = new BufferedWriter(new FileWriter(inputFile))){ + writer.write( + "public class X { \n" + + " public Module getModule(String name) {\n" + + " return null;\n" + + " }\n" + + "}"); + } catch (IOException e) { + e.printStackTrace(); + } + // create new list containing input file + List<File> files = new ArrayList<File>(); + files.add(inputFile); + Iterable<? extends JavaFileObject> units = standardManager.getJavaFileObjectsFromFiles(files); + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + List<String> options = new ArrayList<String>(); + options.add("-d"); + options.add(tmpFolder); + options.add("--release"); + options.add("8"); + ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); + PrintWriter err = new PrintWriter(errBuffer); + CompilerInvocationDiagnosticListener listener = new CompilerInvocationDiagnosticListener(err); + CompilationTask task = compiler.getTask(printWriter, getFileManager(manager), listener, options, null, units); + Boolean result = task.call(); + printWriter.flush(); + printWriter.close(); + System.err.println(stringWriter.getBuffer().toString()); + if (result.booleanValue()) { + System.err.println("Compilation did not fail as expected: " + stringWriter.getBuffer().toString()); + assertTrue("Compilation did not fail as expected", false); + } + }; + cons.accept(standardManager); + cons.accept(getFileManager(standardManager)); + } + public void testOptionRelease6() throws IOException { + if (this.isJREBelow12) return; + JavaCompiler compiler = this.compilers[1]; + StandardJavaFileManager standardManager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); + Consumer<JavaFileManager> cons = (manager) -> { + String tmpFolder = _tmpFolder; + File inputFile = new File(tmpFolder, "X.java"); + try (Writer writer = new BufferedWriter(new FileWriter(inputFile))){ + writer.write( + "public class X { \n" + + " public Module getModule(String name) {\n" + + " return null;\n" + + " }\n" + + "}"); + } catch (IOException e) { + e.printStackTrace(); + } + // create new list containing input file + List<File> files = new ArrayList<File>(); + files.add(inputFile); + Iterable<? extends JavaFileObject> units = standardManager.getJavaFileObjectsFromFiles(files); + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + List<String> options = new ArrayList<String>(); + options.add("-d"); + options.add(tmpFolder); + options.add("--release"); + options.add("10"); + ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); + PrintWriter err = new PrintWriter(errBuffer); + CompilerInvocationDiagnosticListener listener = new CompilerInvocationDiagnosticListener(err); + CompilationTask task = compiler.getTask(printWriter, getFileManager(manager), listener, options, null, units); + Boolean result = task.call(); + printWriter.flush(); + printWriter.close(); + if (!result.booleanValue()) { + System.err.println("Compilation failed: " + stringWriter.getBuffer().toString()); + assertTrue("Compilation failed", false); + } + }; + cons.accept(standardManager); + cons.accept(getFileManager(standardManager)); } private char[] extractClassName(int[] constantPoolOffsets, ClassFileReader reader, int index) { // the entry at i has to be a field ref or a method/interface method ref. @@ -681,7 +729,7 @@ public class CompilerToolJava9Tests extends TestCase { System.err.println("Compilation failed unexpectedly: " + stringWriter.getBuffer().toString()); assertTrue("Compilation failed ", false); } - + // Try using the same outut as module-path and compile the new module (mod.test) manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); @@ -718,7 +766,7 @@ public class CompilerToolJava9Tests extends TestCase { System.err.println("Compilation failed unexpectedly: " + stringWriter.getBuffer().toString()); assertTrue("Compilation failed ", false); } - + // Delete the module-info.class from the previously compiled modules // and try compiling the same module mod.test File file = new File(tmpFolder + File.separator + "mod.test" + File.separator + "module-info.class"); 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 9c03411275..00cbf0956e 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 @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.nio.charset.Charset; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -33,6 +34,8 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Optional; +import java.util.stream.Stream; import javax.annotation.processing.Processor; import javax.lang.model.SourceVersion; @@ -71,6 +74,8 @@ import org.eclipse.jdt.internal.compiler.util.Util; public class EclipseCompilerImpl extends Main { private static final CompilationUnit[] NO_UNITS = new CompilationUnit[0]; + private static final String RELEASE_FILE = "release"; //$NON-NLS-1$ + private static final String JAVA_VERSION = "JAVA_VERSION"; //$NON-NLS-1$ private HashMap<CompilationUnit, JavaFileObject> javaFileObjectMap; Iterable<? extends JavaFileObject> compilationUnits; public JavaFileManager fileManager; @@ -229,7 +234,7 @@ public class EclipseCompilerImpl extends Main { DiagnosticListener<? super JavaFileObject> diagListener = EclipseCompilerImpl.this.diagnosticListener; Diagnostic<JavaFileObject> diagnostic = null; if (diagListener != null) { - diagnostic = new Diagnostic<JavaFileObject>() { + diagnostic = new Diagnostic<>() { @Override public String getCode() { return Integer.toString(problemId); @@ -306,7 +311,7 @@ public class EclipseCompilerImpl extends Main { DiagnosticListener<? super JavaFileObject> diagListener = EclipseCompilerImpl.this.diagnosticListener; Diagnostic<JavaFileObject> diagnostic = null; if (diagListener != null) { - diagnostic = new Diagnostic<JavaFileObject>() { + diagnostic = new Diagnostic<>() { @Override public String getCode() { return Integer.toString(problemId); @@ -552,18 +557,10 @@ public class EclipseCompilerImpl extends Main { File javaHome = Util.getJavaHome(); long jdkLevel = Util.getJDKLevel(javaHome); if (jdkLevel >= ClassFileConstants.JDK9) { - Classpath system = null; - if (this.releaseVersion != null && this.complianceLevel < jdkLevel) { - String versionFromJdkLevel = CompilerOptions.versionFromJdkLevel(this.complianceLevel); - if (versionFromJdkLevel.length() >= 3) { - versionFromJdkLevel = versionFromJdkLevel.substring(2); - } - // TODO: Revisit for access rules - system = FileSystem.getOlderSystemRelease(javaHome.getAbsolutePath(), versionFromJdkLevel, null); - } else { - system = FileSystem.getJrtClasspath(javaHome.toString(), null, null, null); - } - Classpath classpath = new ClasspathJsr199(system, this.fileManager, StandardLocation.PLATFORM_CLASS_PATH); + Classpath systemClasspath = getSystemClasspath(fileSystemClasspaths, javaHome, jdkLevel); + Classpath classpath = new ClasspathJsr199(systemClasspath, this.fileManager, StandardLocation.SYSTEM_MODULES); + fileSystemClasspaths.add(classpath); + classpath = new ClasspathJsr199(systemClasspath, this.fileManager, StandardLocation.PLATFORM_CLASS_PATH); fileSystemClasspaths.add(classpath); } else { Classpath classpath = new ClasspathJsr199(this.fileManager, StandardLocation.PLATFORM_CLASS_PATH); @@ -607,6 +604,40 @@ public class EclipseCompilerImpl extends Main { } } } + locationFiles = standardJavaFileManager.getLocation(StandardLocation.PLATFORM_CLASS_PATH); + if (locationFiles != null) { + for (File file : locationFiles) { + String javaVersion = getJavaVersion(file); + long jdkLevel = javaVersion.equals("") ? this.complianceLevel : CompilerOptions.versionToJdkLevel(javaVersion); //$NON-NLS-1$ + Classpath systemClasspath = getSystemClasspath(fileSystemClasspaths, file, jdkLevel); + Classpath classpath = new ClasspathJsr199(systemClasspath, this.fileManager, StandardLocation.PLATFORM_CLASS_PATH); + fileSystemClasspaths.add(classpath); + // Copy over to modules location as well + if (standardJavaFileManager.getLocation(StandardLocation.SYSTEM_MODULES) == null) { + classpath = new ClasspathJsr199(systemClasspath, this.fileManager, StandardLocation.SYSTEM_MODULES); + fileSystemClasspaths.add(classpath); + } + haveClassPaths = true; + break; //unlikely to have more than one path + } + } + locationFiles = standardJavaFileManager.getLocation(StandardLocation.SYSTEM_MODULES); + if (locationFiles != null) { + for (File file : locationFiles) { + String javaVersion = getJavaVersion(file); + long jdkLevel = javaVersion.equals("") ? this.complianceLevel : CompilerOptions.versionToJdkLevel(javaVersion); //$NON-NLS-1$ + Classpath systemClasspath = getSystemClasspath(fileSystemClasspaths, file, jdkLevel); + Classpath classpath = new ClasspathJsr199(systemClasspath, this.fileManager, StandardLocation.SYSTEM_MODULES); + fileSystemClasspaths.add(classpath); + // Copy over to platform location as well + if (standardJavaFileManager.getLocation(StandardLocation.PLATFORM_CLASS_PATH) == null) { + classpath = new ClasspathJsr199(systemClasspath, this.fileManager, StandardLocation.PLATFORM_CLASS_PATH); + fileSystemClasspaths.add(classpath); + } + haveClassPaths = true; + break; //unlikely to have more than one path + } + } if (SourceVersion.latest().compareTo(SourceVersion.RELEASE_8) > 0) { try { Iterable<? extends Path> locationAsPaths = standardJavaFileManager.getLocationAsPaths(StandardLocation.MODULE_SOURCE_PATH); @@ -675,10 +706,6 @@ public class EclipseCompilerImpl extends Main { if (this.fileManager.hasLocation(StandardLocation.UPGRADE_MODULE_PATH)) { classpath = new ClasspathJsr199(this.fileManager, StandardLocation.UPGRADE_MODULE_PATH); } - if (this.fileManager.hasLocation(StandardLocation.SYSTEM_MODULES)) { - classpath = new ClasspathJsr199(this.fileManager, StandardLocation.SYSTEM_MODULES); - fileSystemClasspaths.add(classpath); - } if (this.fileManager.hasLocation(StandardLocation.PATCH_MODULE_PATH)) { classpath = new ClasspathJsr199(this.fileManager, StandardLocation.PATCH_MODULE_PATH); fileSystemClasspaths.add(classpath); @@ -714,6 +741,37 @@ public class EclipseCompilerImpl extends Main { } } } + private String getJavaVersion(File javaHome) { + String version = ""; //$NON-NLS-1$ + if (Files.notExists(Paths.get(javaHome.getAbsolutePath(), RELEASE_FILE))) { + return version; + } + try (Stream<String> lines = Files.lines(Paths.get(javaHome.getAbsolutePath(), RELEASE_FILE), Charset.defaultCharset()).filter(s -> s.contains(JAVA_VERSION))) { + Optional<String> hasVersion = lines.findFirst(); + if (hasVersion.isPresent()) { + String line = hasVersion.get(); + version = line.substring(14, line.length() - 1); // length of JAVA_VERSION + 2 in JAVA_VERSION="9" + } + } + catch (Exception e) { + // return default + } + return version; + } + private Classpath getSystemClasspath(ArrayList<FileSystem.Classpath> fileSystemClasspaths, File jdkHome, long jdkLevel) { + Classpath system; + if (this.releaseVersion != null && this.complianceLevel < jdkLevel) { + String versionFromJdkLevel = CompilerOptions.versionFromJdkLevel(this.complianceLevel); + if (versionFromJdkLevel.length() >= 3) { + versionFromJdkLevel = versionFromJdkLevel.substring(2); + } + // TODO: Revisit for access rules + system = FileSystem.getOlderSystemRelease(jdkHome.getAbsolutePath(), versionFromJdkLevel, null); + } else { + system = FileSystem.getJrtClasspath(jdkHome.toString(), null, null, null); + } + return system; + } protected List<Classpath> getPlatformLocations(ArrayList<FileSystem.Classpath> fileSystemClasspaths, File file) { List<Classpath> platformLibraries = Util.collectPlatformLibraries(file); @@ -726,7 +784,7 @@ public class EclipseCompilerImpl extends Main { Iterator iterator = this.extraProblems.iterator(); iterator.hasNext(); ) { final CategorizedProblem problem = (CategorizedProblem) iterator.next(); if (this.diagnosticListener != null && !isIgnored(problem)) { - Diagnostic<JavaFileObject> diagnostic = new Diagnostic<JavaFileObject>() { + Diagnostic<JavaFileObject> diagnostic = new Diagnostic<>() { @Override public String getCode() { return null; |
