Skip to content

Commit

Permalink
Fix VariableBinding.getJavaElement() type signature (#2037)
Browse files Browse the repository at this point in the history
Make signature format returned by the binding match the usual signature
format for  IJavaElement.

Fixes #2036
  • Loading branch information
mickaelistria authored Mar 8, 2024
1 parent c648d35 commit 1d93115
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.core.tests.util.Util;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.core.LambdaExpression;
Expand Down Expand Up @@ -1423,6 +1429,46 @@ public void test531046h() throws CoreException, IOException {
deleteProject("P");
}
}
// derived from test531046a, verifying we get same value from codeSelect or bindings
public void testJavaElementViaBindings() throws CoreException, IOException {
if (!isJRE9) return;
org.eclipse.jdt.internal.core.CompilationUnit unit = null;
try {
createJava10Project("P", new String[] {"src"});
createFolder("/P/src/p");
createFile("/P/src/p/X.java", "");
waitForAutoBuild();

unit = (org.eclipse.jdt.internal.core.CompilationUnit)getCompilationUnit("/P/src/p/X.java");
unit.becomeWorkingCopy(new ProblemRequestor(), null);
unit.getBuffer().setContents("""
package p;
public class X {
public static void main(java.lang.String[] args) {
var s1 = args[0];
System.out.println(s1);
}
}
""");
CompilationUnit dom = unit.makeConsistent(AST.getJLSLatest(), true, 0, null, null);
VariableDeclarationFragment domVariable = (VariableDeclarationFragment)
((VariableDeclarationStatement)
((TypeDeclaration)dom.types().get(0)).getMethods()[0].getBody().statements().get(0))
.fragments().get(0);
ILocalVariable variable = (ILocalVariable)domVariable.resolveBinding().getJavaElement();
assertEquals("incorrect type", "Ljava.lang.String;", variable.getTypeSignature());

SingleVariableDeclaration parameterVar = (SingleVariableDeclaration)
((TypeDeclaration)dom.types().get(0)).getMethods()[0].parameters().get(0);
variable = (ILocalVariable)parameterVar.resolveBinding().getJavaElement();
assertEquals("incorrect type", "[Ljava.lang.String;", variable.getTypeSignature());
} finally {
if (unit != null) {
unit.discardWorkingCopy();
}
deleteProject("P");
}
}
public void testBug533884a() throws Exception {
if (!isJRE9) return;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.core.util.IModifierConstants;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
Expand Down Expand Up @@ -266,7 +267,7 @@ private JavaElement getUnresolvedJavaElement() {
}
}
int sourceEnd = sourceStart+sourceLength-1;
char[] typeSig = this.binding.type.genericTypeSignature();
char[] typeSig = Signature.createTypeSignature(this.binding.type.signableName(), true).toCharArray();
JavaElement parent = null;
IMethodBinding declaringMethod = getDeclaringMethod();
if (this.binding instanceof RecordComponentBinding) {
Expand Down

0 comments on commit 1d93115

Please sign in to comment.