diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/FileSystem.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/FileSystem.java index e32d77ef6c2..7df9c70034f 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/FileSystem.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/FileSystem.java @@ -168,7 +168,7 @@ public static ArrayList normalize(ArrayList classpaths) { protected boolean annotationsFromClasspath; // should annotation files be read from the classpath (vs. explicit separate path)? private static HashMap JRT_CLASSPATH_CACHE = null; protected Map moduleLocations = new HashMap<>(); - private Consumer nameEnvironmentAnswerListener; // a listener for findType* answers + private Consumer nameEnvironmentAnswerListener; // a listener for findType* answers /** Tasks resulting from --add-reads or --add-exports command line options. */ Map moduleUpdates = new HashMap<>(); @@ -432,7 +432,7 @@ public void cleanup() { private static String convertPathSeparators(String path) { return File.separatorChar == '/' ? path.replace('\\', '/') - : path.replace('/', '\\'); + : path.replace('/', '\\'); } @SuppressWarnings("resource") // don't close classpathEntry.zipFile, which we don't own private NameEnvironmentAnswer findClass(String qualifiedTypeName, char[] typeName, boolean asBinaryOnly, /*NonNull*/char[] moduleName) { @@ -450,7 +450,7 @@ private NameEnvironmentAnswer findClass(String qualifiedTypeName, char[] typeNam } answer.setBinaryType(ExternalAnnotationDecorator.create(answer.getBinaryType(), classpathEntry.getPath(), qualifiedTypeName, zip)); - return notify(answer); + return notify(answer); } catch (IOException e) { // ignore broken entry, keep searching } finally { @@ -464,17 +464,17 @@ private NameEnvironmentAnswer findClass(String qualifiedTypeName, char[] typeNam // globally configured (annotationsFromClasspath), but no .eea found, decorate in order to answer NO_EEA_FILE: answer.setBinaryType(new ExternalAnnotationDecorator(answer.getBinaryType(), null)); } - return notify(answer); + return notify(answer); } private NameEnvironmentAnswer notify(NameEnvironmentAnswer answer) { - if(answer == null) { - return answer; - } - Consumer listener = this.nameEnvironmentAnswerListener; - if(listener != null) { - listener.accept(answer); - } + if(answer == null) { + return answer; + } + Consumer listener = this.nameEnvironmentAnswerListener; + if(listener != null) { + listener.accept(answer); + } return answer; } @@ -773,8 +773,8 @@ public void applyModuleUpdates(IUpdatableModule compilerModule, IUpdatableModule * @return a previously set listener (may be null) */ public Consumer setNameEnvironmentAnswerListener(Consumer nameEnvironmentAnswerListener) { - Consumer existing = this.nameEnvironmentAnswerListener; - this.nameEnvironmentAnswerListener = nameEnvironmentAnswerListener; - return existing; + Consumer existing = this.nameEnvironmentAnswerListener; + this.nameEnvironmentAnswerListener = nameEnvironmentAnswerListener; + return existing; } } diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/Main.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/Main.java index fe61cebfa6a..29f24f63927 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/Main.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/batch/Main.java @@ -1288,19 +1288,19 @@ public void startLoggingSource(CompilationResult compilationResult) { ICompilationUnit compilationUnit = compilationResult.compilationUnit; HashMap parameters = new HashMap<>(); if (compilationUnit != null) { - char[] fileName = compilationUnit.getFileName(); - File f = new File(new String(fileName)); - if (fileName != null) { - parameters.put(Logger.PATH, f.getAbsolutePath()); - } - char[][] packageName = compilationResult.packageName; - if (packageName != null) { - parameters.put( - Logger.PACKAGE, - new String(CharOperation.concatWith(packageName, File.separatorChar))); - } - CompilationUnit unit = (CompilationUnit) compilationUnit; - String destinationPath = unit.destinationPath; + char[] fileName = compilationUnit.getFileName(); + File f = new File(new String(fileName)); + if (fileName != null) { + parameters.put(Logger.PATH, f.getAbsolutePath()); + } + char[][] packageName = compilationResult.packageName; + if (packageName != null) { + parameters.put( + Logger.PACKAGE, + new String(CharOperation.concatWith(packageName, File.separatorChar))); + } + CompilationUnit unit = (CompilationUnit) compilationUnit; + String destinationPath = unit.destinationPath; if (destinationPath == null) { destinationPath = this.main.destinationPath; } @@ -1613,7 +1613,7 @@ protected void addNewEntry(ArrayList paths, String current } } if (rulesOK) { - accessRuleSet = new AccessRuleSet(accessRules, AccessRestriction.COMMAND_LINE, currentClasspathName); + accessRuleSet = new AccessRuleSet(accessRules, AccessRestriction.COMMAND_LINE, currentClasspathName); } else { if (currentClasspathName.length() != 0) { // we go on anyway @@ -3366,7 +3366,7 @@ public String extractDestinationPathFromSourceFile(CompilationResult result) { * Answer the component to which will be handed back compilation results from the compiler */ public ICompilerRequestor getBatchRequestor() { - return new BatchCompilerRequestor(this); + return new BatchCompilerRequestor(this); } /* * Build the set of compilation source units @@ -3499,8 +3499,8 @@ public IProblemFactory getProblemFactory() { * External API */ protected ArrayList handleBootclasspath(ArrayList bootclasspaths, String customEncoding) { - final int bootclasspathsSize; - ArrayList result = new ArrayList<>(DEFAULT_SIZE_CLASSPATH); + final int bootclasspathsSize; + ArrayList result = new ArrayList<>(DEFAULT_SIZE_CLASSPATH); if ((bootclasspaths != null) && ((bootclasspathsSize = bootclasspaths.size()) != 0)) { result = new ArrayList<>(bootclasspathsSize); @@ -3739,7 +3739,7 @@ public void multipleClasspathSections(String jarFilePath) { * External API */ protected ArrayList handleEndorseddirs(ArrayList endorsedDirClasspaths) { - final File javaHome = getJavaHome(); + final File javaHome = getJavaHome(); /* * Feed endorsedDirClasspath according to: * - -endorseddirs first if present; @@ -3802,7 +3802,7 @@ protected ArrayList handleEndorseddirs(ArrayList e * Handle extdirs processing */ protected ArrayList handleExtdirs(ArrayList extdirsClasspaths) { - final File javaHome = getJavaHome(); + final File javaHome = getJavaHome(); /* * Feed extDirClasspath according to: @@ -4043,7 +4043,7 @@ private void handleErrorOrWarningToken(String token, boolean isEnabling, int sev } } this.options.put(CompilerOptions.OPTION_ReportMissingEnumCaseDespiteDefault, - isEnabling ? CompilerOptions.ENABLED : CompilerOptions.DISABLED); + isEnabling ? CompilerOptions.ENABLED : CompilerOptions.DISABLED); return; } else if (token.equals("emptyBlock")) {//$NON-NLS-1$ setSeverity(CompilerOptions.OPTION_ReportUndocumentedEmptyBlock, severity, isEnabling); diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NameEnvironmentAnswerListenerTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NameEnvironmentAnswerListenerTest.java index d08d4acc6fd..eb4cdd9350b 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NameEnvironmentAnswerListenerTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NameEnvironmentAnswerListenerTest.java @@ -37,139 +37,139 @@ public class NameEnvironmentAnswerListenerTest extends AbstractComparableTest { - public NameEnvironmentAnswerListenerTest(String name) { - super(name); - } - - /** - * Extension of ECJ Batch compiler to allow pre-configuration as well as registration of listener. - *

- * This is modeled after real-world use in Bazel ECJ Toolchain. - *

- * @see BlazeEcjMain.java - */ - static class EclipseBatchCompiler extends Main { - - Set answeredFileNames = new LinkedHashSet<>(); - - public EclipseBatchCompiler(PrintWriter errAndOutWriter) { - super(errAndOutWriter, errAndOutWriter, false /* systemExitWhenFinished */, null /* customDefaultOptions */, - null /* compilationProgress */); - - setSeverity(CompilerOptions.OPTION_ReportForbiddenReference, ProblemSeverities.Error, true); - setSeverity(CompilerOptions.OPTION_ReportDiscouragedReference, ProblemSeverities.Error, true); - } - - @Override - public FileSystem getLibraryAccess() { - // we use this to collect information about all used dependencies during - // compilation - FileSystem nameEnvironment = super.getLibraryAccess(); - nameEnvironment.setNameEnvironmentAnswerListener(this::recordNameEnvironmentAnswer); - return nameEnvironment; - } - - protected void recordNameEnvironmentAnswer(NameEnvironmentAnswer answer) { - Assert.assertNotNull("don't call without answer", answer); - - char[] fileName = null; - if(answer.getBinaryType() != null) { - URI uri = answer.getBinaryType().getURI(); - this.answeredFileNames.add(uri.toString()); - return; - } else if(answer.getCompilationUnit() != null) { - fileName = answer.getCompilationUnit().getFileName(); - } else if(answer.getSourceTypes() != null && answer.getSourceTypes().length > 0) { - fileName = answer.getSourceTypes()[0].getFileName(); // the first type is guaranteed to be the requested type - } else if(answer.getResolvedBinding() != null) { - fileName = answer.getResolvedBinding().getFileName(); - } - if (fileName != null) this.answeredFileNames.add(new String(fileName)); - } - } - - public void testNameEnvironmentAnswerListener() throws IOException { - String path = LIB_DIR; - if(!path.endsWith(File.separator)) { - path += File.separator; - } - String libPath = path + "lib.jar"; - Util.createJar( - new String[] { - "p/Color.java", - "package p;\n" + - "public enum Color {\n" + - " R, Y;\n" + - " public static Color getColor() {\n" + - " return R;\n" + - " }\n" + - "}", - }, - libPath, JavaCore.VERSION_17); - - String unusedLibPath = path + "lib_unused.jar"; - Util.createJar( - new String[] { - "p2/Color.java", - "package p2;\n" + - "public enum Color {\n" + - " R, Y;\n" + - " public static Color getColor() {\n" + - " return R;\n" + - " }\n" + - "}", - }, - unusedLibPath, JavaCore.VERSION_17); - - String srcDir = path + "src"; - String[] pathsAndContents = - new String[] { - "s/X.java", - "package s;\n" + - "import p.Color;\n" + - "public class X {\n" + - " public static final Color MY = Color.R;\n" + - "}" - }; - Util.createSourceDir(pathsAndContents, srcDir); - - List classpath = new ArrayList<>(Arrays.asList(getDefaultClassPaths())); - classpath.add(libPath); - classpath.add(unusedLibPath); - - File outputDirectory = new File(Util.getOutputDirectory()); - if (!outputDirectory.isDirectory()) { - outputDirectory.mkdirs(); - } - - List ecjArguments = new ArrayList<>(); - - ecjArguments.add("-classpath"); - ecjArguments.add(classpath.stream() - .map(jar -> jar.equals(unusedLibPath) ? String.format("%s[-**/*]", jar) : jar) - .collect(joining(File.pathSeparator))); - - - ecjArguments.add("-d"); - ecjArguments.add(outputDirectory.getAbsolutePath()); - - ecjArguments.add("--release"); - ecjArguments.add("17"); - - ecjArguments.add(srcDir+ File.separator + "s"+ File.separator + "X.java"); - - EclipseBatchCompiler compiler; - File logFile = new File(outputDirectory, "compile.log"); - try(PrintWriter log = new PrintWriter(new FileOutputStream(logFile))) { - compiler = new EclipseBatchCompiler(log); - boolean compileOK; - compileOK = compiler.compile(ecjArguments.toArray(new String[ecjArguments.size()])); - if(!compileOK) { - String logOutputString = Util.fileContent(logFile.getAbsolutePath()); - Assert.fail("Compile failed, output: '" + logOutputString + "'"); - } - } - Assert.assertTrue("must reference p.Color", compiler.answeredFileNames.stream().anyMatch(s -> s.contains(libPath))); - Assert.assertFalse("must not reference p2.Color", compiler.answeredFileNames.stream().anyMatch(s -> s.contains(unusedLibPath))); - } + public NameEnvironmentAnswerListenerTest(String name) { + super(name); + } + + /** + * Extension of ECJ Batch compiler to allow pre-configuration as well as registration of listener. + *

+ * This is modeled after real-world use in Bazel ECJ Toolchain. + *

+ * @see BlazeEcjMain.java + */ + static class EclipseBatchCompiler extends Main { + + Set answeredFileNames = new LinkedHashSet<>(); + + public EclipseBatchCompiler(PrintWriter errAndOutWriter) { + super(errAndOutWriter, errAndOutWriter, false /* systemExitWhenFinished */, null /* customDefaultOptions */, + null /* compilationProgress */); + + setSeverity(CompilerOptions.OPTION_ReportForbiddenReference, ProblemSeverities.Error, true); + setSeverity(CompilerOptions.OPTION_ReportDiscouragedReference, ProblemSeverities.Error, true); + } + + @Override + public FileSystem getLibraryAccess() { + // we use this to collect information about all used dependencies during + // compilation + FileSystem nameEnvironment = super.getLibraryAccess(); + nameEnvironment.setNameEnvironmentAnswerListener(this::recordNameEnvironmentAnswer); + return nameEnvironment; + } + + protected void recordNameEnvironmentAnswer(NameEnvironmentAnswer answer) { + Assert.assertNotNull("don't call without answer", answer); + + char[] fileName = null; + if(answer.getBinaryType() != null) { + URI uri = answer.getBinaryType().getURI(); + this.answeredFileNames.add(uri.toString()); + return; + } else if(answer.getCompilationUnit() != null) { + fileName = answer.getCompilationUnit().getFileName(); + } else if(answer.getSourceTypes() != null && answer.getSourceTypes().length > 0) { + fileName = answer.getSourceTypes()[0].getFileName(); // the first type is guaranteed to be the requested type + } else if(answer.getResolvedBinding() != null) { + fileName = answer.getResolvedBinding().getFileName(); + } + if (fileName != null) this.answeredFileNames.add(new String(fileName)); + } + } + + public void testNameEnvironmentAnswerListener() throws IOException { + String path = LIB_DIR; + if(!path.endsWith(File.separator)) { + path += File.separator; + } + String libPath = path + "lib.jar"; + Util.createJar( + new String[] { + "p/Color.java", + "package p;\n" + + "public enum Color {\n" + + " R, Y;\n" + + " public static Color getColor() {\n" + + " return R;\n" + + " }\n" + + "}", + }, + libPath, JavaCore.VERSION_17); + + String unusedLibPath = path + "lib_unused.jar"; + Util.createJar( + new String[] { + "p2/Color.java", + "package p2;\n" + + "public enum Color {\n" + + " R, Y;\n" + + " public static Color getColor() {\n" + + " return R;\n" + + " }\n" + + "}", + }, + unusedLibPath, JavaCore.VERSION_17); + + String srcDir = path + "src"; + String[] pathsAndContents = + new String[] { + "s/X.java", + "package s;\n" + + "import p.Color;\n" + + "public class X {\n" + + " public static final Color MY = Color.R;\n" + + "}" + }; + Util.createSourceDir(pathsAndContents, srcDir); + + List classpath = new ArrayList<>(Arrays.asList(getDefaultClassPaths())); + classpath.add(libPath); + classpath.add(unusedLibPath); + + File outputDirectory = new File(Util.getOutputDirectory()); + if (!outputDirectory.isDirectory()) { + outputDirectory.mkdirs(); + } + + List ecjArguments = new ArrayList<>(); + + ecjArguments.add("-classpath"); + ecjArguments.add(classpath.stream() + .map(jar -> jar.equals(unusedLibPath) ? String.format("%s[-**/*]", jar) : jar) + .collect(joining(File.pathSeparator))); + + + ecjArguments.add("-d"); + ecjArguments.add(outputDirectory.getAbsolutePath()); + + ecjArguments.add("--release"); + ecjArguments.add("17"); + + ecjArguments.add(srcDir+ File.separator + "s"+ File.separator + "X.java"); + + EclipseBatchCompiler compiler; + File logFile = new File(outputDirectory, "compile.log"); + try(PrintWriter log = new PrintWriter(new FileOutputStream(logFile))) { + compiler = new EclipseBatchCompiler(log); + boolean compileOK; + compileOK = compiler.compile(ecjArguments.toArray(new String[ecjArguments.size()])); + if(!compileOK) { + String logOutputString = Util.fileContent(logFile.getAbsolutePath()); + Assert.fail("Compile failed, output: '" + logOutputString + "'"); + } + } + Assert.assertTrue("must reference p.Color", compiler.answeredFileNames.stream().anyMatch(s -> s.contains(libPath))); + Assert.assertFalse("must not reference p2.Color", compiler.answeredFileNames.stream().anyMatch(s -> s.contains(unusedLibPath))); + } } diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TestAll.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TestAll.java index 4b373a7d0a4..79b2e5158b9 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TestAll.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TestAll.java @@ -94,7 +94,7 @@ public static Test suite() { standardTests.add(InitializationTests.class); standardTests.add(ResourceLeakTests.class); standardTests.add(PackageBindingTest.class); - standardTests.add(NameEnvironmentAnswerListenerTest.class); + standardTests.add(NameEnvironmentAnswerListenerTest.class); // add all javadoc tests for (int i=0, l=JavadocTest.ALL_CLASSES.size(); i