Skip to content

Commit

Permalink
Fix IAE computing type signature for anonymous + var
Browse files Browse the repository at this point in the history
Unlike what's specified by TypeBinding.signableType(), the return value
is incorrect for anonymous type and that leads to an exception such as

  java.lang.IllegalArgumentException: new I(){}
  	at org.eclipse.jdt.core.Signature.createCharArrayTypeSignature(Signature.java:1109)
  	at org.eclipse.jdt.core.Signature.createTypeSignature(Signature.java:1305)
  	at org.eclipse.jdt.core.dom.VariableBinding.getUnresolvedJavaElement(VariableBinding.java:277)

Some specific code is added here to handle that case.
  • Loading branch information
mickaelistria authored and akurtakov committed Apr 1, 2024
1 parent c339c56 commit 7bf49aa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,37 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.dom;

import junit.framework.Test;

import org.eclipse.jdt.core.dom.*;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;

import java.io.IOException;
import java.util.Hashtable;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.ILocalVariable;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.FileASTRequestor;
import org.eclipse.jdt.core.dom.ForStatement;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.NodeFinder;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;

import junit.framework.Test;

public class ASTConverter10Test extends ConverterTestSetup {

Expand Down Expand Up @@ -274,5 +291,27 @@ public void acceptAST(String sourceFilePath, CompilationUnit cu) {
deleteFile(jarPath);
}
}

public void testAnonymousTypeVarBindingToLocalVariable() throws Exception {
String filePath = "/Converter10/src/X.java";
try {
String contents = """
class X {
void m() {
var variable = new I() {};
}
}
interface I {}
""";
this.workingCopy = getCompilationUnit(filePath);
CompilationUnit dom = (CompilationUnit)buildAST(contents, this.workingCopy);
Name node = (Name)NodeFinder.perform(dom, contents.indexOf("variable"), "variable".length());
IVariableBinding binding = (IVariableBinding)node.resolveBinding();
ILocalVariable model = (ILocalVariable)binding.getJavaElement();
assertEquals("I", Signature.toString(model.getTypeSignature()));
} finally {
deleteFile(filePath);
}
}
// Add new tests here
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
import org.eclipse.jdt.internal.compiler.lookup.RecordComponentBinding;
import org.eclipse.jdt.internal.compiler.lookup.TagBits;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
import org.eclipse.jdt.internal.core.JavaElement;
import org.eclipse.jdt.internal.core.LocalVariable;
Expand Down Expand Up @@ -264,7 +265,13 @@ private JavaElement getUnresolvedJavaElement() {
}
}
int sourceEnd = sourceStart+sourceLength-1;
char[] typeSig = Signature.createTypeSignature(this.binding.type.signableName(), true).toCharArray();
TypeBinding signableType = this.binding.type;
if (signableType.isAnonymousType()) {
signableType = signableType.superInterfaces() != null && signableType.superInterfaces().length == 1 ?
signableType.superInterfaces()[0] :
signableType.superclass();
}
char[] typeSig = Signature.createTypeSignature(signableType.signableName(), true).toCharArray();
JavaElement parent = null;
IMethodBinding declaringMethod = getDeclaringMethod();
if (this.binding instanceof RecordComponentBinding) {
Expand Down

0 comments on commit 7bf49aa

Please sign in to comment.