Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASTParser.createAST fails at handling missing types #1915

Closed
mickaelistria opened this issue Jan 26, 2024 · 1 comment · Fixed by #1942
Closed

ASTParser.createAST fails at handling missing types #1915

mickaelistria opened this issue Jan 26, 2024 · 1 comment · Fixed by #1942

Comments

@mickaelistria
Copy link
Contributor

While working on DOM based operation ( #1863 ), I noticed that a DOM created with ASTParser.createAST is not always complete and can fail at resolving some bindings.

Inspired from ASTConverterTest15.test0238_2

	public void test0238_2() 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((IJavaProject)ResourcesPlugin.getWorkspace().getRoot().getProject("Converter15").getNature(JavaCore.NATURE_ID));
		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);
	}

fails because the method referencing the unresolved type isn't included.

The expectation here would be that this piece of code returns a CompilationUnit DOM and bindings fully equivalent to what CompilationUnit.buildStructure() would process with the same settings (recovery).

CompilationUnit.buildStructure uses a combination of parse/resolve which is similar to what createAST does, however, it looks like createAST doesn't properly set the resolution context/environment before computing the bindings.

@mickaelistria
Copy link
Contributor Author

The "leaf" problem seems to be that environment.problemReporter.referenceContext is null invoking TypeBinding.resolveMethods() when the compilation unit is built using ASTParser.createAST

mickaelistria added a commit to mickaelistria/eclipse.jdt.core that referenced this issue Jan 31, 2024
Keeping the lookupEnvironment intact as it can be necessary for
further binding resolution.
The former "paranoia" seems not to be worried about as the referenced
element in the environment is the unit, that does exist and is the
root of the scope/environment. So this shouldn't consume more memory,
and should cause a risk of memory leak.

Fixes eclipse-jdt#1915
mickaelistria added a commit to mickaelistria/eclipse.jdt.core that referenced this issue Jan 31, 2024
Keeping the lookupEnvironment intact as it can be necessary for
further binding resolution.
The former "paranoia" seems not to be worried about as the referenced
element in the environment is the unit, that does exist and is the
root of the scope/environment. So this shouldn't consume more memory,
and should cause a risk of memory leak.

Fixes eclipse-jdt#1915
mickaelistria added a commit to mickaelistria/eclipse.jdt.core that referenced this issue Feb 1, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
datho7561 pushed a commit to datho7561/eclipse.jdt.core that referenced this issue Feb 8, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
datho7561 pushed a commit to datho7561/eclipse.jdt.core that referenced this issue Feb 8, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to mickaelistria/eclipse.jdt.core that referenced this issue Feb 12, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to mickaelistria/eclipse.jdt.core that referenced this issue Feb 16, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to mickaelistria/eclipse.jdt.core that referenced this issue Feb 23, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to mickaelistria/eclipse.jdt.core that referenced this issue Mar 4, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to eclipse-jdtls/eclipse-jdt-core-incubator that referenced this issue Mar 5, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to mickaelistria/eclipse.jdt.core that referenced this issue Mar 7, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to eclipse-jdtls/eclipse-jdt-core-incubator that referenced this issue Mar 8, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to mickaelistria/eclipse.jdt.core that referenced this issue Mar 8, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to eclipse-jdtls/eclipse-jdt-core-incubator that referenced this issue Mar 8, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to mickaelistria/eclipse.jdt.core that referenced this issue Mar 8, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to eclipse-jdtls/eclipse-jdt-core-incubator that referenced this issue Mar 8, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to eclipse-jdtls/eclipse-jdt-core-incubator that referenced this issue Mar 12, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
mickaelistria added a commit to eclipse-jdtls/eclipse-jdt-core-incubator that referenced this issue Mar 13, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
stephan-herrmann pushed a commit that referenced this issue Mar 14, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for further binding resolution.

Fixes #1915
mickaelistria added a commit to eclipse-jdtls/eclipse-jdt-core-incubator that referenced this issue Apr 1, 2024
Having root unit set in the lookupEnvironment (when focusing on a single
unit) can be necessary for
further binding resolution.

Fixes eclipse-jdt#1915
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant