diff options
8 files changed, 154 insertions, 39 deletions
diff --git a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar Binary files differindex de6b974816..bf811a9fe5 100644 --- a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar +++ b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/mod_locations/modules/mod.a/module-info.java b/org.eclipse.jdt.compiler.apt.tests/resources/mod_locations/modules/mod.a/module-info.java index 77d0566384..3f1ea9f110 100644 --- a/org.eclipse.jdt.compiler.apt.tests/resources/mod_locations/modules/mod.a/module-info.java +++ b/org.eclipse.jdt.compiler.apt.tests/resources/mod_locations/modules/mod.a/module-info.java @@ -1,5 +1,5 @@ @java.lang.Deprecated() -module mod.a { +open module mod.a { exports abc.internal; requires transitive java.compiler; diff --git a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java index dfce8c7dad..7903d88f26 100644 --- a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java +++ b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/BatchTestUtils.java @@ -130,6 +130,7 @@ public class BatchTestUtils { PrintWriter printWriter = new PrintWriter(stringWriter); List<String> copyOptions = new ArrayList<>(); + copyOptions.addAll(options); copyOptions.add("-processor"); copyOptions.add(processor); copyOptions.add("-A" + processor); @@ -149,6 +150,7 @@ public class BatchTestUtils { } List<String> classes = new ArrayList<>(); try { + manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); System.clearProperty(processor); copyOptions = new ArrayList<>(); copyOptions.addAll(options); diff --git a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/Factory.java b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/Factory.java index db40f2d7ca..1290abfc95 100644 --- a/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/Factory.java +++ b/org.eclipse.jdt.compiler.apt/src/org/eclipse/jdt/internal/compiler/apt/model/Factory.java @@ -392,6 +392,9 @@ public class Factory { if (binding instanceof SplitPackageBinding && binding.enclosingModule != null) { binding = ((SplitPackageBinding) binding).getIncarnation(binding.enclosingModule); } + if (binding == null) { + return null; + } return new PackageElementImpl(_env, binding); } 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 0fadc35aff..9eb7775919 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 @@ -161,7 +161,7 @@ public class CompilerToolJava9Tests extends TestCase { } } // Incomplete tests - fails both with Javac and ECJ - public void _testGetLocationForModule2() throws IOException { + public void testGetLocationForModule2() throws IOException { if (this.isJREBelow9) return; for(int i = 0; i < 2; i++) { String cName = this.compilerNames[i]; @@ -170,9 +170,9 @@ public class CompilerToolJava9Tests extends TestCase { continue; StandardJavaFileManager manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); Path path = Paths.get(modules_directory + File.separator + "source" + File.separator + "SimpleModules"); - manager.setLocationFromPaths(StandardLocation.MODULE_PATH, Arrays.asList(path)); + manager.setLocationFromPaths(StandardLocation.MODULE_SOURCE_PATH, Arrays.asList(path)); try { - JavaFileManager.Location location = manager.getLocationForModule(StandardLocation.MODULE_PATH, "module.two"); + JavaFileManager.Location location = manager.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH, "module.two"); assertNotNull(cName + ":module path location should not be null", location); } catch (UnsupportedOperationException ex) { fail(cName + ":Should support getLocationForModule()"); @@ -180,7 +180,7 @@ public class CompilerToolJava9Tests extends TestCase { } } // Incomplete tests - fails both with Javac and ECJ - public void _testGetLocationForModule3() throws IOException { + public void testGetLocationForModule3() throws IOException { if (this.isJREBelow9) return; for(int i = 0; i < 2; i++) { String cName = this.compilerNames[i]; @@ -189,9 +189,9 @@ public class CompilerToolJava9Tests extends TestCase { continue; StandardJavaFileManager manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); Path path = Paths.get(modules_directory + File.separator + "source" + File.separator + "SimpleModules"); - manager.setLocationFromPaths(StandardLocation.MODULE_PATH, Arrays.asList(path)); + manager.setLocationFromPaths(StandardLocation.MODULE_SOURCE_PATH, Arrays.asList(path)); try { - JavaFileManager.Location location = manager.getLocationForModule(StandardLocation.MODULE_PATH, "module.one"); + JavaFileManager.Location location = manager.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH, "module.one"); assertNotNull(cName + ":module path location should not be null", location); } catch (UnsupportedOperationException ex) { fail(cName + ":Should support getLocationForModule()"); @@ -498,6 +498,74 @@ public class CompilerToolJava9Tests extends TestCase { assertEquals("option -source is not supported when --release is used", iae.getMessage()); } } + public void testClassOutputLocationForModule_1() throws IOException { + if (this.isJREBelow9) return; + for(int i = 0; i < 2; i++) { + String cName = this.compilerNames[i]; + JavaCompiler compiler = this.compilers[i]; + if (!(compiler instanceof EclipseCompiler)) + continue; + StandardJavaFileManager manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); + Path path = Paths.get(modules_directory + File.separator + "source" + File.separator + "SimpleModules"); + manager.setLocationFromPaths(StandardLocation.MODULE_SOURCE_PATH, Arrays.asList(path)); + manager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(_tmpBinDir)); + try { + JavaFileManager.Location location = manager.getLocationForModule(StandardLocation.CLASS_OUTPUT, "module.two"); + assertNotNull(cName + ":module path location should not be null", location); + assertTrue("should be output location", location.isOutputLocation()); + } catch (UnsupportedOperationException ex) { + fail(cName + ":Should support getLocationForModule()"); + } + } + } + public void testClassOutputLocationForModule_2() throws IOException { + if (this.isJREBelow9) return; + for(int i = 0; i < 2; i++) { + String cName = this.compilerNames[i]; + JavaCompiler compiler = this.compilers[i]; + if (!(compiler instanceof EclipseCompiler)) + continue; + StandardJavaFileManager manager = compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset()); + Path path = Paths.get(modules_directory + File.separator + "source" + File.separator + "SimpleModules" + File.separator + "module.one"); + manager.setLocationForModule(StandardLocation.MODULE_SOURCE_PATH, "module.one", Arrays.asList(path)); + path = Paths.get(modules_directory + File.separator + "source" + File.separator + "SimpleModules" + File.separator + "module.two"); + manager.setLocationForModule(StandardLocation.MODULE_SOURCE_PATH, "module.two", Arrays.asList(path)); + manager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(_tmpBinDir)); + try { + JavaFileManager.Location location = manager.getLocationForModule(StandardLocation.CLASS_OUTPUT, "module.one"); + assertTrue("should be output location", location.isOutputLocation()); + assertNotNull(cName + ":module path location should not be null", location); + Iterable<? extends Path> locationAsPaths = manager.getLocationAsPaths(location); + int count = 0; + boolean found = false; + for (Path path2 : locationAsPaths) { + if (path2.endsWith("module.one")) { + found = true; + } + count++; + } + assertEquals("incorrect no of output locations", 1, count); + assertTrue("output location for module.two not found", found); + location = manager.getLocationForModule(StandardLocation.CLASS_OUTPUT, "module.two"); + assertTrue("should be output location", location.isOutputLocation()); + assertNotNull(cName + ":module path location should not be null", location); + locationAsPaths = manager.getLocationAsPaths(location); + count = 0; + found = false; + for (Path path2 : locationAsPaths) { + if (path2.endsWith("module.two")) { + found = true; + } + count++; + } + assertEquals("incorrect no of output locations", 1, count); + assertTrue("output location for module.two not found", found); + + } catch (UnsupportedOperationException ex) { + fail(cName + ":Should support getLocationForModule()"); + } + } + } public void testGetJavaFileObjects() { if (this.isJREBelow9) return; } 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 fddda9c0d3..c547b22ec8 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 @@ -582,21 +582,23 @@ public class EclipseCompilerImpl extends Main { try { Iterable<? extends Path> locationAsPaths = standardJavaFileManager.getLocationAsPaths(StandardLocation.MODULE_SOURCE_PATH); if (locationAsPaths != null) { + StringBuilder builder = new StringBuilder(); for (Path path : locationAsPaths) { - ArrayList<Classpath> modulepaths = handleModuleSourcepath(path.toFile().getCanonicalPath()); - for (Classpath classpath : modulepaths) { - Collection<String> moduleNames = classpath.getModuleNames(null); - for (String modName : moduleNames) { - Path p = Paths.get(classpath.getPath()); - standardJavaFileManager.setLocationForModule(StandardLocation.MODULE_SOURCE_PATH, modName, - Collections.singletonList(p)); - p = Paths.get(classpath.getDestinationPath()); - standardJavaFileManager.setLocationForModule(StandardLocation.CLASS_OUTPUT, modName, - Collections.singletonList(p)); - } + // Append all of them + builder.append(path.toFile().getCanonicalPath()); + builder.append(File.pathSeparator); + } + ArrayList<Classpath> modulepaths = handleModuleSourcepath(builder.toString()); + for (Classpath classpath : modulepaths) { + Collection<String> moduleNames = classpath.getModuleNames(null); + for (String modName : moduleNames) { + Path p = Paths.get(classpath.getPath()); + standardJavaFileManager.setLocationForModule(StandardLocation.MODULE_SOURCE_PATH, modName, + Collections.singletonList(p)); + p = Paths.get(classpath.getDestinationPath()); } - fileSystemClasspaths.addAll(modulepaths); } + fileSystemClasspaths.addAll(modulepaths); } } catch (Exception e) { // TODO: Revisit when JRE 9 no longer throws IllegalStateException for getLocation. diff --git a/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/EclipseFileManager.java b/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/EclipseFileManager.java index 32f35b3098..4aa0201692 100644 --- a/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/EclipseFileManager.java +++ b/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/EclipseFileManager.java @@ -1346,7 +1346,7 @@ public class EclipseFileManager implements StandardJavaFileManager { @Override public void setLocationFromPaths(Location location, Collection<? extends Path> paths) throws IOException { setLocation(location, getFiles(paths)); - if (location == StandardLocation.MODULE_PATH) { + if (location == StandardLocation.MODULE_PATH || location == StandardLocation.MODULE_SOURCE_PATH) { // FIXME: same for module source path? Map<String, String> options = new HashMap<>(); // FIXME: Find a way to get the options from the EclipseCompiler and pass it to the parser. @@ -1367,7 +1367,7 @@ public class EclipseFileManager implements StandardJavaFileManager { Collection<String> moduleNames = cp.getModuleNames(null); for (String string : moduleNames) { Path p = Paths.get(cp.getPath()); - setLocationForModule(StandardLocation.MODULE_PATH, string, Collections.singletonList(p)); + setLocationForModule(location, string, Collections.singletonList(p)); } } } @@ -1406,7 +1406,13 @@ public class EclipseFileManager implements StandardJavaFileManager { @Override public Location getLocationForModule(Location location, String moduleName) throws IOException { validateModuleLocation(location, moduleName); - return this.locationHandler.getLocation(location, moduleName); + Location result = this.locationHandler.getLocation(location, moduleName); + if (result == null && location == StandardLocation.CLASS_OUTPUT) { + LocationWrapper wrapper = this.locationHandler.getLocation(StandardLocation.MODULE_SOURCE_PATH, moduleName); + deriveOutputLocationForModules(moduleName, wrapper.paths); + result = getLocationForModule(location, moduleName); + } + return result; } @Override @@ -1464,24 +1470,34 @@ public class EclipseFileManager implements StandardJavaFileManager { } return null; } - + private void deriveOutputLocationForModules(String moduleName, Collection<? extends Path> paths) { + LocationWrapper wrapper = this.locationHandler.getLocation(StandardLocation.CLASS_OUTPUT, moduleName); + if (wrapper == null) { + // First get from our internally known location for legacy/unnamed location + wrapper = this.locationHandler.getLocation(StandardLocation.CLASS_OUTPUT, ""); //$NON-NLS-1$ + if (wrapper == null) { + wrapper = this.locationHandler.getLocation(StandardLocation.CLASS_OUTPUT); + } + if (wrapper != null) { + Iterator<? extends Path> iterator = wrapper.paths.iterator(); + if (iterator.hasNext()) { + try { + // Per module output location is always a singleton list + Path path = iterator.next().resolve(moduleName); + this.locationHandler.setLocation(StandardLocation.CLASS_OUTPUT, moduleName, Collections.singletonList(path)); + } catch(Exception e) { + e.printStackTrace(); + } + } + } + } + } @Override public void setLocationForModule(Location location, String moduleName, Collection<? extends Path> paths) throws IOException { validateModuleLocation(location, moduleName); this.locationHandler.setLocation(location, moduleName, paths); if (location == StandardLocation.MODULE_SOURCE_PATH) { - LocationWrapper wrapper = this.locationHandler.getLocation(StandardLocation.CLASS_OUTPUT, moduleName); - if (wrapper == null) { - wrapper = this.locationHandler.getLocation(StandardLocation.CLASS_OUTPUT, ""); //$NON-NLS-1$ - if (wrapper != null) { - Iterator<? extends Path> iterator = wrapper.paths.iterator(); - if (iterator.hasNext()) { - // Per module output location is always a singleton list - Path path = iterator.next().resolve(moduleName); - this.locationHandler.setLocation(StandardLocation.CLASS_OUTPUT, moduleName, Collections.singletonList(path)); - } - } - } + deriveOutputLocationForModules(moduleName, paths); } } } diff --git a/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/ModuleLocationHandler.java b/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/ModuleLocationHandler.java index 41ab9d93ee..351a399b65 100644 --- a/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/ModuleLocationHandler.java +++ b/org.eclipse.jdt.compiler.tool/src/org/eclipse/jdt/internal/compiler/tool/ModuleLocationHandler.java @@ -42,6 +42,10 @@ public class ModuleLocationHandler { SystemLocationContainer systemLocationWrapper = new SystemLocationContainer(StandardLocation.SYSTEM_MODULES, jrt); this.containers.put(loc, systemLocationWrapper); } + public void newOutputLocation(Location loc) { + OutputLocationContainer outputWrapper = new OutputLocationContainer(loc); + this.containers.put(loc, outputWrapper); + } public LocationWrapper getLocation(Location loc, String moduleName) { if (loc instanceof LocationWrapper) { @@ -64,11 +68,16 @@ public class ModuleLocationHandler { public LocationContainer getLocation(Location location) { return this.containers.get(location); } + private LocationContainer createNewContainer(Location loc) { + LocationContainer container = (loc == StandardLocation.CLASS_OUTPUT) ? + new OutputLocationContainer(loc) : new LocationContainer(loc); + this.containers.put(loc, container); + return container; + } public void setLocation(Location location, Iterable<? extends Path> paths) { LocationContainer container = this.containers.get(location); if (container == null) { - container = new LocationContainer(location); - this.containers.put(location, container); + container = createNewContainer(location); } container.setPaths(paths); } @@ -78,8 +87,7 @@ public class ModuleLocationHandler { if (container != null) { wrapper = container.get(moduleName); } else { - container = new LocationContainer(location); - this.containers.put(location, container); + container = createNewContainer(location); } if (wrapper == null) { // module name can't be null @@ -173,6 +181,22 @@ public class ModuleLocationHandler { this(loc, new JrtFileSystem(cp.file)); } } + class OutputLocationContainer extends LocationContainer { + + OutputLocationContainer(Location loc) { + super(loc); + } + + @Override + void put(String moduleName, LocationWrapper impl) { + this.locationNames.put(moduleName, impl); + } + + @Override + void put(Path path, LocationWrapper impl) { + this.locationPaths.put(path, impl); + } + } class LocationWrapper implements Location { |