From 329c4dac17757a64f198f6e4ae977a4343308a78 Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Thu, 14 Mar 2024 13:37:47 +0100 Subject: [PATCH] ASTParser.createAST looses ability to resolve some bindings (#1942) Having root unit set in the lookupEnvironment (when focusing on a single unit) can be necessary for further binding resolution. Fixes https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1915 --- .../core/tests/dom/ASTConverter15Test.java | 24 +++++++++++++++++++ .../jdt/core/dom/CompilationUnitResolver.java | 4 ++++ 2 files changed, 28 insertions(+) diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java index 84bf1f22ac1..c6f8afd210b 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java @@ -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; @@ -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 */ diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/CompilationUnitResolver.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/CompilationUnitResolver.java index c5a02e69d4d..1988de8c26b 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/CompilationUnitResolver.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/CompilationUnitResolver.java @@ -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) {