eclipse-jdt / eclipse-jdt/eclipse.jdt.core

ASTNode->IVariableBinding->IJavaElement fails when dereferencing enum component in compact constructor

Open
#2,177 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

Inspired from JavaSearchBugs15Tests
```
public void testBug558812_012_ASTNodeBindingJavaElement() throws CoreException {
JAVA_PROJECT.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.DISABLED);
this.workingCopies = new ICompilationUnit[1];
this.workingCopies[0] = getWorkingCopy("/JavaSearchBugs/src/X.java", """
public record Point(int comp_) {
public Point {
comp_=11;
}
}
""");

String str = this.workingCopies[0].getSource();
String selection = "comp_";
int start = str.lastIndexOf(selection);
int length = selection.length();
ASTParser parser = ASTParser.newParser(AST.getJLSLatest());
parser.setWorkingCopyOwner(this.wcOwner);
parser.setResolveBindings(true);
parser.setSource(this.workingCopies[0]);
parser.setStatementsRecovery(true);
parser.setBindingsRecovery(true);
CompilationUnit dom = (CompilationUnit)parser.createAST(null);
Name node = (Name)NodeFinder.perform(dom, start, length);
IVariableBinding binding = (IVariableBinding)node.resolveBinding();
assertNotNull(binding.getJavaElement());
}
```
currently fails to return an IJavaElement.
The expectation would be to return an ILocalVariable mapping to the Enum definition. Note that the binding itself seems incorrect as we could expect it returns true for `isRecordComponent()`, while it doesn't.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.