Skip to content

Commit

Permalink
ASTParser.createAST looses ability to resolve some bindings (#1942)
Browse files Browse the repository at this point in the history
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for further binding resolution.

Fixes #1915
  • Loading branch information
mickaelistria authored Mar 14, 2024
1 parent 5664513 commit 329c4da
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.BindingKey;
import org.eclipse.jdt.core.IAnnotation;
Expand Down Expand Up @@ -7891,6 +7892,29 @@ public void test0238_2() throws JavaModelException {
assertEquals("wrong size", 2, methodBindings.length);
}

/*
* https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1915
*/
public void test0238_ASTParser() throws JavaModelException, CoreException {
this.workingCopy = getWorkingCopy("/Converter15/src/test0238/X.java",
"""
package test0238;
public class X extends A {
}
""", true /*resolve*/);
ASTParser parser = ASTParser.newParser(AST.getJLSLatest());
parser.setBindingsRecovery(true);
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
parser.setSource(this.workingCopy);
parser.setProject(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject("Converter15")));
CompilationUnit unit = (CompilationUnit) parser.createAST(null);
TypeDeclaration typeDeclaration = (TypeDeclaration) unit.types().get(0);
ITypeBinding superTypeBinding = typeDeclaration.resolveBinding().getSuperclass();
IMethodBinding[] methodBindings = superTypeBinding.getDeclaredMethods();
assertEquals("wrong size", 2, methodBindings.length);
}

/*
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=173338
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,10 @@ public static CompilationUnitDeclaration resolve(
CancelableNameEnvironment cancelableNameEnvironment = (CancelableNameEnvironment) environment;
cancelableNameEnvironment.printTimeSpent();
}
if (unit != null && unit.scope != null && unit.scope.environment != null
&& unit.scope.environment.unitBeingCompleted == null) {
unit.scope.environment.unitBeingCompleted = unit;
}
return unit;
} finally {
if (environment != null) {
Expand Down

0 comments on commit 329c4da

Please sign in to comment.