Skip to content

Commit

Permalink
java.lang.StackOverflowError during "Requesting Java AST from selection"
Browse files Browse the repository at this point in the history
change of strategy:
+ better hiding of modules seen via the classpath

Fixes eclipse-jdt#3273
  • Loading branch information
stephan-herrmann committed Nov 30, 2024
1 parent ba07c9d commit 3cadc59
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2017 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -61,7 +61,11 @@ default boolean ignoreOptionalProblems() {
* @return the binding representing the module.
*/
default ModuleBinding module(LookupEnvironment environment) {
return environment.getModule(getModuleName());
if (environment.nameEnvironment instanceof IModuleAwareNameEnvironment modEnv) {
char[] moduleName = modEnv.isOnModulePath(this) ? getModuleName() : ModuleBinding.UNNAMED;
return environment.getModule(moduleName);
}
return null;
}
/**
* Returns the name of the module to which this compilation unit is associated.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2017 IBM Corporation and others.
* Copyright (c) 2016, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -132,6 +132,16 @@ default char[][] getUniqueModulesDeclaringPackage(char[][] packageName, char[] m
IModule getModule(char[] moduleName);
char[][] getAllAutomaticModules();

/**
* If this environment focusses on one IJavaProject answer if the given
* compilation unit is found on the module path, rather than the class path.
* @param unit compilation unit to test
* @return true iff unit is on the module path.
*/
default boolean isOnModulePath(ICompilationUnit unit) {
return true; // TODO: should more / all implementors implement this method?
}

/**
* Ask the name environment to perform any updates (add-exports or add-reads) to the given module.
* @param module the compiler representation of the module to updates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,7 @@ public class Client {
IVariableBinding c1 = type.getDeclaredFields()[0];
IModuleBinding module = c1.getType().getModule();
assertNotNull(module);
assertEquals("first", module.getName());
assertEquals("", module.getName());
} finally {
deleteProject("P0");
deleteProject("P1");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
* Copyright (c) 2000, 2020 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -3741,14 +3741,6 @@ public IModuleDescription getModuleDescription() throws JavaModelException {
return null;
}

public boolean isModular() {
try {
return getModuleDescription() != null;
} catch (JavaModelException e) {
return false;
}
}

@Override
public IModuleDescription getOwnModuleDescription() throws JavaModelException {
JavaProjectElementInfo info = (JavaProjectElementInfo) getElementInfo();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
* Copyright (c) 2000, 2021 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -904,7 +904,7 @@ static IModuleDescription getModuleDescription(JavaProject project, IPackageFrag
IModuleDescription module = cache.get(root);
if (module != null)
return module != NO_MODULE ? module : null;
if (!Objects.equals(project, root.getJavaProject()) && project.isModular()) {
if (!Objects.equals(project, root.getJavaProject())) {
IClasspathEntry classpathEntry2 = rootToEntry.apply(root);
if (classpathEntry2 instanceof ClasspathEntry) {
if (!((ClasspathEntry) classpathEntry2).isModular()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
Expand Down Expand Up @@ -301,6 +302,22 @@ public void findModules(char[] prefix, ISearchRequestor requestor, IJavaProject
this.nameLookup.seekModule(prefix, true, new SearchableEnvironmentRequestor(requestor));
}

@Override
public boolean isOnModulePath(ICompilationUnit unit) {
if (unit instanceof CompilationUnit cUnit) {
IPackageFragmentRoot root = cUnit.originalFromClone().getPackageFragmentRoot();
if (Objects.equals(root.getJavaProject(), this.project))
return true; // current project: modular if it contains module-info :)
IClasspathEntry entry = this.nameLookup.rootToResolvedEntries.get(root);
if (entry instanceof ClasspathEntry cpEntry)
return cpEntry.isModular();
return true; // out-of-band resolution / transitive dependency?
} else if (unit instanceof BasicCompilationUnit bUnit) {
return bUnit.moduleName != null;
}
return false;
}

/**
* Find the packages that start with the given prefix.
* A valid prefix is a qualified name separated by periods
Expand Down

0 comments on commit 3cadc59

Please sign in to comment.