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
+ resilience: avoid accepting a sourceType being completed already
+ reduce fix for 544306 to modular projects

Fixes eclipse-jdt#3273
  • Loading branch information
stephan-herrmann committed Nov 30, 2024
1 parent e7f1e3d commit ba07c9d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 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 @@ -335,6 +335,14 @@ public void accept(IBinaryType binaryType, PackageBinding packageBinding, Access
*/
@Override
public void accept(ICompilationUnit sourceUnit, AccessRestriction accessRestriction) {
if (this.lookupEnvironment.unitBeingCompleted != null) {
// resilience for https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3273
// NEVER accept a sourceUnit that is already being completed
char[] fileName1 = this.lookupEnvironment.unitBeingCompleted.compilationResult.fileName;
char[] fileName2 = sourceUnit.getFileName();
if (CharOperation.equals(fileName1, fileName2) && !CharOperation.equals(fileName1, TypeConstants.MODULE_INFO_FILE_NAME))
return;
}
// Switch the current policy and compilation result for this unit to the requested one.
CompilationResult unitResult =
new CompilationResult(sourceUnit, this.totalUnits, this.totalUnits, this.options.maxProblemsPerUnit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1573,17 +1573,23 @@ enum Kind { Good, Bad }
"""
package common.test;
import static common.pack1.C1.*; // this import pulls in classes from the other project
import common.pack1.C1;
public class Client {
String s = CONST1;
C1<?> c1;
}
""");

IJavaElement[] types = { project2.findType("common.test.Client") };
ASTParser astParser = ASTParser.newParser(AST.getJLSLatest());
astParser.setProject(project2);
IBinding[] bindings = astParser.createBindings(types, null);
System.out.println(bindings);
// TODO add useful assertions once we reach here
assertEquals(1, bindings.length);
ITypeBinding type = (ITypeBinding) bindings[0];
IVariableBinding c1 = type.getDeclaredFields()[0];
IModuleBinding module = c1.getType().getModule();
assertNotNull(module);
assertEquals("first", 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, 2020 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 @@ -3741,6 +3741,14 @@ 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, 2021 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 @@ -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())) {
if (!Objects.equals(project, root.getJavaProject()) && project.isModular()) {
IClasspathEntry classpathEntry2 = rootToEntry.apply(root);
if (classpathEntry2 instanceof ClasspathEntry) {
if (!((ClasspathEntry) classpathEntry2).isModular()) {
Expand Down

0 comments on commit ba07c9d

Please sign in to comment.