Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephan Herrmann2017-09-17 14:41:41 +0000
committerStephan Herrmann2017-09-17 14:41:41 +0000
commit0201874afdd1b68acce876105bbdf3cc69d05ccc (patch)
tree436888a3d0b3012cc0c556d6337efd2b6c1e9960 /org.eclipse.jdt.core
parentee1456d22bc6413cf0655e6ab4304dc6965f4445 (diff)
downloadeclipse.jdt.core-0201874afdd1b68acce876105bbdf3cc69d05ccc.tar.gz
eclipse.jdt.core-0201874afdd1b68acce876105bbdf3cc69d05ccc.tar.xz
eclipse.jdt.core-0201874afdd1b68acce876105bbdf3cc69d05ccc.zip
Bug 522328: [9] define default set of JDK modules on the module path
Diffstat (limited to 'org.eclipse.jdt.core')
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/ObjectVector.java19
-rw-r--r--org.eclipse.jdt.core/model/org/eclipse/jdt/core/provisional/JavaModelAccess.java17
-rw-r--r--org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathChange.java2
-rw-r--r--org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java90
4 files changed, 109 insertions, 19 deletions
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/ObjectVector.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/ObjectVector.java
index 07dbfba4b3..d103b9aca8 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/ObjectVector.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/ObjectVector.java
@@ -10,7 +10,9 @@
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.util;
-public final class ObjectVector {
+import java.util.Iterator;
+
+public final class ObjectVector implements Iterable<Object> {
static int INITIAL_SIZE = 10;
@@ -132,4 +134,19 @@ public final class ObjectVector {
s += this.elements[i].toString() + "\n"; //$NON-NLS-1$
return s;
}
+
+ @Override
+ public Iterator<Object> iterator() {
+ return new Iterator<Object>() {
+ int i=0;
+ @Override
+ public boolean hasNext() {
+ return this.i < ObjectVector.this.size;
+ }
+ @Override
+ public Object next() {
+ return ObjectVector.this.elementAt(this.i++);
+ }
+ };
+ }
}
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/provisional/JavaModelAccess.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/provisional/JavaModelAccess.java
index 4b685d34e5..fc3478ef1f 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/provisional/JavaModelAccess.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/provisional/JavaModelAccess.java
@@ -15,6 +15,7 @@
package org.eclipse.jdt.core.provisional;
import java.util.Arrays;
+import java.util.List;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
@@ -34,9 +35,10 @@ public class JavaModelAccess {
/**
* In a Java 9 project, a classpath entry can be filtered using a {@link IClasspathAttribute#LIMIT_MODULES} attribute,
- * in which case {@link IJavaProject#findPackageFragmentRoots(IClasspathEntry)} will not contain all roots physically
+ * otherwise a default set of roots is used as defined in JEP 261.
+ * In both cases {@link IJavaProject#findPackageFragmentRoots(IClasspathEntry)} will not contain all roots physically
* present in the container.
- * This provisional API can be used to bypass the filter and get really all roots to which the given entry is resolved.
+ * This provisional API can be used to bypass any filter and get really all roots to which the given entry is resolved.
*
* @param javaProject the Java project to search in
* @param entry a classpath entry of the Java project
@@ -46,7 +48,7 @@ public class JavaModelAccess {
try {
JavaProject internalProject = (JavaProject) javaProject; // cast should be safe since IJavaProject is @noimplement
IClasspathEntry[] resolvedEntries = internalProject.resolveClasspath(new IClasspathEntry[]{ entry });
- return internalProject.computePackageFragmentRoots(resolvedEntries, false /* not exported roots */, false /* ignore limit-modules! */, null /* no reverse map */);
+ return internalProject.computePackageFragmentRoots(resolvedEntries, false /* not exported roots */, false /* don't filter! */, null /* no reverse map */);
} catch (JavaModelException e) {
// according to comment in JavaProject.findPackageFragmentRoots() we assume that this is caused by the project no longer existing
return new IPackageFragmentRoot[] {};
@@ -62,4 +64,13 @@ public class JavaModelAccess {
IModuleReference[] references = ((AbstractModule) module).getRequiredModules();
return Arrays.stream(references).map(ref -> String.valueOf(ref.name())).toArray(String[]::new);
}
+
+ /**
+ * Filter the given set of system roots by the rules for root modules from JEP 261.
+ * @param allSystemRoots all physically available system modules, represented by their package fragment roots
+ * @return the list of names of default root modules
+ */
+ public static List<String> defaultRootModules(Iterable<IPackageFragmentRoot> allSystemRoots) {
+ return JavaProject.defaultRootModules(allSystemRoots);
+ }
}
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathChange.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathChange.java
index 92a2f4ae57..bb887aab20 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathChange.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathChange.java
@@ -347,7 +347,7 @@ public class ClasspathChange {
rootIDs,
null, // inside original project
false, // don't retrieve exported roots
- true, // respect limit-modules
+ true, // filter module roots
null); /*no reverse map*/
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=335986
// When a package fragment's corresponding resource is removed from the project,
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java
index b3caeb6029..9812bcb316 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java
@@ -85,6 +85,7 @@ import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.env.IModule.IModuleReference;
+import org.eclipse.jdt.internal.compiler.env.IModule.IPackageExport;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.compiler.util.JRTUtil;
import org.eclipse.jdt.internal.compiler.util.ObjectVector;
@@ -598,7 +599,9 @@ public class JavaProject
* @param rootIDs HashSet
* @param referringEntry the CP entry (project) referring to this entry, or null if initial project
* @param retrieveExportedRoots boolean
- * @param respectLimitModules if true a limit-modules attribute will be evaluated to filter the resulting roots
+ * @param filterModuleRoots if true, roots corresponding to modules will be filtered if applicable:
+ * if a limit-modules attribute exists, this is used, otherwise system modules will be filtered
+ * according to the rules of root modules per JEP 261.
* @throws JavaModelException
*/
public void computePackageFragmentRoots(
@@ -607,7 +610,7 @@ public class JavaProject
HashSet rootIDs,
IClasspathEntry referringEntry,
boolean retrieveExportedRoots,
- boolean respectLimitModules,
+ boolean filterModuleRoots,
Map rootToResolvedEntries) throws JavaModelException {
String rootID = ((ClasspathEntry)resolvedEntry).rootID();
@@ -658,10 +661,17 @@ public class JavaProject
} else {
imageRoots = info.jrtRoots.get(entryPath);
}
- if (respectLimitModules) {
+ if (filterModuleRoots) {
+ List<String> rootModules = null;
String limitModules = ClasspathEntry.getExtraAttribute(resolvedEntry, IClasspathAttribute.LIMIT_MODULES);
- if (limitModules != null)
- imageRoots = filterLimitedModules(entryPath, imageRoots, limitModules);
+ if (limitModules != null) {
+ rootModules = Arrays.asList(limitModules.split(",")); //$NON-NLS-1$
+ } else if (isUnNamedModule()) {
+ rootModules = defaultRootModules((Iterable) imageRoots);
+ }
+ if (rootModules != null) {
+ imageRoots = filterLimitedModules(entryPath, imageRoots, rootModules);
+ }
}
accumulatedRoots.addAll(imageRoots);
} else if (JavaModel.isJmod((File) target)) {
@@ -694,7 +704,7 @@ public class JavaProject
rootIDs,
rootToResolvedEntries == null ? resolvedEntry : ((ClasspathEntry)resolvedEntry).combineWith((ClasspathEntry) referringEntry), // only combine if need to build the reverse map
retrieveExportedRoots,
- respectLimitModules,
+ filterModuleRoots,
rootToResolvedEntries);
}
break;
@@ -707,8 +717,42 @@ public class JavaProject
}
}
- private ObjectVector filterLimitedModules(IPath jrtPath, ObjectVector imageRoots, String limitModules) {
- Set<String> limitModulesSet = new HashSet<>(Arrays.asList(limitModules.split(","))); //$NON-NLS-1$
+ /** Implements selection of root modules per JEP 261. */
+ public static List<String> defaultRootModules(Iterable<IPackageFragmentRoot> allSystemRoots) {
+ List<String> result = new ArrayList<>();
+ boolean hasJavaDotSE = false;
+ for (IPackageFragmentRoot root : allSystemRoots) {
+ if ("java.se".equals(root.getElementName())) { //$NON-NLS-1$
+ result.add(root.getElementName());
+ hasJavaDotSE = true;
+ break;
+ }
+ }
+ for (IPackageFragmentRoot root : allSystemRoots) {
+ String moduleName = root.getElementName();
+ boolean isJavaDotStart = moduleName.startsWith("java."); //$NON-NLS-1$
+ boolean isPotentialRoot = !isJavaDotStart; // always include non-java.*
+ if (!hasJavaDotSE)
+ isPotentialRoot |= isJavaDotStart; // no java.se => add all java.*
+
+ if (isPotentialRoot && root instanceof JrtPackageFragmentRoot) {
+ JrtPackageFragmentRoot jrtRoot = (JrtPackageFragmentRoot) root;
+ IModule module = jrtRoot.getModule();
+ if (module != null) {
+ for (IPackageExport packageExport : module.exports()) {
+ if (!packageExport.isQualified()) {
+ result.add(moduleName);
+ break;
+ }
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ private ObjectVector filterLimitedModules(IPath jrtPath, ObjectVector imageRoots, List<String> rootModuleNames) {
+ Set<String> limitModulesSet = new HashSet<>(rootModuleNames);
ModuleLookup lookup = new ModuleLookup(jrtPath.toFile());
// collect all module roots:
for (int i = 0; i < imageRoots.size(); i++) {
@@ -834,14 +878,16 @@ public class JavaProject
* Only works with resolved entries
* @param resolvedClasspath IClasspathEntry[]
* @param retrieveExportedRoots boolean
- * @param respectLimitModules if true a limit-modules attribute will be evaluated to filter the resulting roots
+ * @param filterModuleRoots if true, roots corresponding to modules will be filtered if applicable:
+ * if a limit-modules attribute exists, this is used, otherwise system modules will be filtered
+ * according to the rules of root modules per JEP 261.
* @return IPackageFragmentRoot[]
* @throws JavaModelException
*/
public IPackageFragmentRoot[] computePackageFragmentRoots(
IClasspathEntry[] resolvedClasspath,
boolean retrieveExportedRoots,
- boolean respectLimitModules,
+ boolean filterModuleRoots,
Map rootToResolvedEntries) throws JavaModelException {
ObjectVector accumulatedRoots = new ObjectVector();
@@ -851,7 +897,7 @@ public class JavaProject
new HashSet(5), // rootIDs
null, // inside original project
retrieveExportedRoots,
- respectLimitModules,
+ filterModuleRoots,
rootToResolvedEntries);
IPackageFragmentRoot[] rootArray = new IPackageFragmentRoot[accumulatedRoots.size()];
accumulatedRoots.copyInto(rootArray);
@@ -868,7 +914,9 @@ public class JavaProject
* @param rootIDs HashSet
* @param referringEntry project entry referring to this CP or null if initial project
* @param retrieveExportedRoots boolean
- * @param respectLimitModules if true a limit-modules attribute will be evaluated to filter the resulting roots
+ * @param filterModuleRoots if true, roots corresponding to modules will be filtered if applicable:
+ * if a limit-modules attribute exists, this is used, otherwise system modules will be filtered
+ * according to the rules of root modules per JEP 261.
* @throws JavaModelException
*/
public void computePackageFragmentRoots(
@@ -877,7 +925,7 @@ public class JavaProject
HashSet rootIDs,
IClasspathEntry referringEntry,
boolean retrieveExportedRoots,
- boolean respectLimitModules,
+ boolean filterModuleRoots,
Map rootToResolvedEntries) throws JavaModelException {
if (referringEntry == null){
@@ -890,7 +938,7 @@ public class JavaProject
rootIDs,
referringEntry,
retrieveExportedRoots,
- respectLimitModules,
+ filterModuleRoots,
rootToResolvedEntries);
}
}
@@ -3496,6 +3544,20 @@ public class JavaProject
}
info.setModule(module);
}
+
+ private boolean isUnNamedModule() throws JavaModelException {
+ JavaProjectElementInfo info = (JavaProjectElementInfo) getElementInfo();
+ IModuleDescription module = info.getModule();
+ if (module != null)
+ return false;
+ for(IClasspathEntry entry : getRawClasspath()) {
+ String mainModule = ClasspathEntry.getExtraAttribute(entry, IClasspathAttribute.PATCH_MODULE);
+ if (mainModule != null)
+ return false;
+
+ }
+ return true;
+ }
public Manifest getManifest() {
IFile file = getProject().getFile(new Path(TypeConstants.META_INF_MANIFEST_MF));

Back to the top