Skip to content

Commit

Permalink
Fixes test0063, test0064, test0065, and more - getQualifiedName expec… (
Browse files Browse the repository at this point in the history
#663)

* Fixes test0063, test0064, test0065, and more - getQualifiedName expects no generics for type declarations

Signed-off-by: Rob Stryker <stryker@redhat.com>

Cleanup debugging text

Signed-off-by: Rob Stryker <stryker@redhat.com>

Bad rebase

Signed-off-by: Rob Stryker <stryker@redhat.com>

* Fix some regressions

Signed-off-by: Rob Stryker <stryker@redhat.com>

* Problems determing when a type is or is not generic, and regressions

Signed-off-by: Rob Stryker <stryker@redhat.com>

---------

Signed-off-by: Rob Stryker <stryker@redhat.com>
Co-authored-by: Rob Stryker <stryker@redhat.com>
  • Loading branch information
2 people authored and datho7561 committed Dec 13, 2024
1 parent d6eda05 commit dcf19f4
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Attribute.Compound;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
import com.sun.tools.javac.code.Symbol.PackageSymbol;
Expand Down Expand Up @@ -98,61 +97,109 @@ public class JavacBindingResolver extends BindingResolver {
private JavacConverter converter;
boolean isRecoveringBindings = false;

public static class BindingKeyException extends Exception {
private static final long serialVersionUID = -4468681148041117634L;
public BindingKeyException(Throwable t) {
super(t);
}
public BindingKeyException(String message, Throwable cause) {
super(message, cause);
}
}

public class Bindings {
private Map<String, JavacAnnotationBinding> annotationBindings = new HashMap<>();
public JavacAnnotationBinding getAnnotationBinding(Compound ann, IBinding recipient) {
JavacAnnotationBinding newInstance = new JavacAnnotationBinding(ann, JavacBindingResolver.this, recipient) { };
annotationBindings.putIfAbsent(newInstance.getKey(), newInstance);
return annotationBindings.get(newInstance.getKey());
String k = newInstance.getKey();
if( k != null ) {
annotationBindings.putIfAbsent(k, newInstance);
return annotationBindings.get(k);
}
return null;
}
//
private Map<String, JavacMemberValuePairBinding> memberValuePairBindings = new HashMap<>();
public JavacMemberValuePairBinding getMemberValuePairBinding(MethodSymbol key, Attribute value) {
JavacMemberValuePairBinding newInstance = new JavacMemberValuePairBinding(key, value, JavacBindingResolver.this) { };
memberValuePairBindings.putIfAbsent(newInstance.getKey(), newInstance);
return memberValuePairBindings.get(newInstance.getKey());
String k = newInstance.getKey();
if( k != null ) {
memberValuePairBindings.putIfAbsent(k, newInstance);
return memberValuePairBindings.get(k);
}
return null;
}
//
private Map<String, JavacMethodBinding> methodBindings = new HashMap<>();
public JavacMethodBinding getMethodBinding(MethodType methodType, MethodSymbol methodSymbol) {
JavacMethodBinding newInstance = new JavacMethodBinding(methodType, methodSymbol, JavacBindingResolver.this) { };
methodBindings.putIfAbsent(newInstance.getKey(), newInstance);
return methodBindings.get(newInstance.getKey());
String k = newInstance.getKey();
if( k != null ) {
methodBindings.putIfAbsent(k, newInstance);
return methodBindings.get(k);
}
return null;
}
public JavacMethodBinding getErrorMethodBinding(MethodType methodType, Symbol originatingSymbol) {
JavacMethodBinding newInstance = new JavacErrorMethodBinding(originatingSymbol, methodType, JavacBindingResolver.this) { };
methodBindings.putIfAbsent(newInstance.getKey(), newInstance);
return methodBindings.get(newInstance.getKey());
String k = newInstance.getKey();
if( k != null ) {
methodBindings.putIfAbsent(k, newInstance);
return methodBindings.get(k);
}
return null;
}
//
private Map<String, JavacModuleBinding> moduleBindings = new HashMap<>();
public JavacModuleBinding getModuleBinding(ModuleType moduleType) {
JavacModuleBinding newInstance = new JavacModuleBinding(moduleType, JavacBindingResolver.this) { };
moduleBindings.putIfAbsent(newInstance.getKey(), newInstance);
return moduleBindings.get(newInstance.getKey());
String k = newInstance.getKey();
if( k != null ) {
moduleBindings.putIfAbsent(k, newInstance);
return moduleBindings.get(k);
}
return null;
}
public JavacModuleBinding getModuleBinding(ModuleSymbol moduleSymbol) {
JavacModuleBinding newInstance = new JavacModuleBinding(moduleSymbol, JavacBindingResolver.this) { };
moduleBindings.putIfAbsent(newInstance.getKey(), newInstance);
return moduleBindings.get(newInstance.getKey());
String k = newInstance.getKey();
if( k != null ) {
moduleBindings.putIfAbsent(k, newInstance);
return moduleBindings.get(k);
}
return null;
}
public JavacModuleBinding getModuleBinding(JCModuleDecl moduleDecl) {
JavacModuleBinding newInstance = new JavacModuleBinding(moduleDecl, JavacBindingResolver.this) { };
// Overwrite existing
moduleBindings.put(newInstance.getKey(), newInstance);
return moduleBindings.get(newInstance.getKey());
String k = newInstance.getKey();
if( k != null ) {
moduleBindings.put(k, newInstance);
return moduleBindings.get(k);
}
return null;
}

//
private Map<String, JavacPackageBinding> packageBindings = new HashMap<>();
public JavacPackageBinding getPackageBinding(PackageSymbol packageSymbol) {
JavacPackageBinding newInstance = new JavacPackageBinding(packageSymbol, JavacBindingResolver.this) { };
packageBindings.putIfAbsent(newInstance.getKey(), newInstance);
return packageBindings.get(newInstance.getKey());
String k = newInstance.getKey();
if( k != null ) {
packageBindings.putIfAbsent(k, newInstance);
return packageBindings.get(k);
}
return null;
}
//
private Map<String, JavacTypeBinding> typeBinding = new HashMap<>();
public JavacTypeBinding getTypeBinding(JCTree tree, com.sun.tools.javac.code.Type type) {
return getTypeBinding(type, tree instanceof JCClassDecl);
}
public JavacTypeBinding getTypeBinding(com.sun.tools.javac.code.Type type) {
return getTypeBinding(type, false);
}
public JavacTypeBinding getTypeBinding(com.sun.tools.javac.code.Type type, boolean isDeclaration) {
if (type instanceof com.sun.tools.javac.code.Type.TypeVar typeVar) {
return getTypeVariableBinding(typeVar);
}
Expand All @@ -164,36 +211,52 @@ public JavacTypeBinding getTypeBinding(com.sun.tools.javac.code.Type type) {
&& !(errorType.getOriginalType() instanceof com.sun.tools.javac.code.Type.MethodType)
&& !(errorType.getOriginalType() instanceof com.sun.tools.javac.code.Type.ForAll)
&& !(errorType.getOriginalType() instanceof com.sun.tools.javac.code.Type.ErrorType)) {
JavacTypeBinding newInstance = new JavacTypeBinding(errorType.getOriginalType(), type.tsym, JavacBindingResolver.this) { };
JavacTypeBinding newInstance = new JavacTypeBinding(errorType.getOriginalType(), type.tsym, isDeclaration, JavacBindingResolver.this) { };
typeBinding.putIfAbsent(newInstance.getKey(), newInstance);
JavacTypeBinding jcb = typeBinding.get(newInstance.getKey());
jcb.setRecovered(true);
return jcb;
}
JavacTypeBinding newInstance = new JavacTypeBinding(type, type.tsym, JavacBindingResolver.this) { };
typeBinding.putIfAbsent(newInstance.getKey(), newInstance);
return typeBinding.get(newInstance.getKey());
JavacTypeBinding newInstance = new JavacTypeBinding(type, type.tsym, isDeclaration, JavacBindingResolver.this) { };
String k = newInstance.getKey();
if( k != null ) {
typeBinding.putIfAbsent(k, newInstance);
return typeBinding.get(k);
}
return null;
}
//
private Map<String, JavacTypeVariableBinding> typeVariableBindings = new HashMap<>();
public JavacTypeVariableBinding getTypeVariableBinding(TypeVar typeVar) {
JavacTypeVariableBinding newInstance = new JavacTypeVariableBinding(typeVar, (TypeVariableSymbol)typeVar.tsym, JavacBindingResolver.this) { };
typeVariableBindings.putIfAbsent(newInstance.getKey(), newInstance);
return typeVariableBindings.get(newInstance.getKey());
String k = newInstance.getKey();
if( k != null ) {
typeVariableBindings.putIfAbsent(k, newInstance);
return typeVariableBindings.get(k);
}
return null;
}
//
private Map<String, JavacVariableBinding> variableBindings = new HashMap<>();
public JavacVariableBinding getVariableBinding(VarSymbol varSymbol) {
JavacVariableBinding newInstance = new JavacVariableBinding(varSymbol, JavacBindingResolver.this) { };
variableBindings.putIfAbsent(newInstance.getKey(), newInstance);
return variableBindings.get(newInstance.getKey());
String k = newInstance.getKey();
if( k != null ) {
variableBindings.putIfAbsent(k, newInstance);
return variableBindings.get(k);
}
return null;
}
//
private Map<String, JavacLambdaBinding> lambdaBindings = new HashMap<>();
public JavacLambdaBinding getLambdaBinding(JavacMethodBinding javacMethodBinding) {
JavacLambdaBinding newInstance = new JavacLambdaBinding(javacMethodBinding);
lambdaBindings.putIfAbsent(newInstance.getKey(), newInstance);
return lambdaBindings.get(newInstance.getKey());
String k = newInstance.getKey();
if( k != null ) {
lambdaBindings.putIfAbsent(k, newInstance);
return lambdaBindings.get(k);
}
return null;
}

public IBinding getBinding(final Symbol owner, final com.sun.tools.javac.code.Type type) {
Expand Down Expand Up @@ -445,7 +508,7 @@ ITypeBinding resolveType(TypeDeclaration type) {
resolve();
JCTree javacNode = this.converter.domToJavac.get(type);
if (javacNode instanceof JCClassDecl jcClassDecl && jcClassDecl.type != null) {
return this.bindings.getTypeBinding(jcClassDecl.type);
return this.bindings.getTypeBinding(jcClassDecl.type, true);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public boolean isEqualTo(IBinding binding) {
public IMemberValuePairBinding[] getAllMemberValuePairs() {
return this.annotation.getElementValues().entrySet().stream()
.map(entry -> this.resolver.bindings.getMemberValuePairBinding(entry.getKey(), entry.getValue()))
.filter(Objects::nonNull)
.toArray(IMemberValuePairBinding[]::new);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.JavacBindingResolver;
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;

import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
Expand All @@ -35,6 +36,13 @@ public JavacErrorMethodBinding(Symbol originatingSymbol, MethodType methodType,

@Override
public String getKey() {
try {
return getKeyImpl();
} catch(BindingKeyException bke) {
return null;
}
}
private String getKeyImpl() throws BindingKeyException {
StringBuilder builder = new StringBuilder();
if (this.originatingSymbol instanceof TypeSymbol typeSymbol) {
JavacTypeBinding.getKey(builder, resolver.getTypes().erasure(typeSymbol.type), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.JavacBindingResolver;
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
Expand Down Expand Up @@ -254,20 +255,24 @@ private String resolveTypeName(com.sun.tools.javac.code.Type type, boolean binar

@Override
public String getKey() {
StringBuilder builder = new StringBuilder();
getKey(builder, this.methodSymbol, this.methodType, this.resolver);
return builder.toString();
try {
StringBuilder builder = new StringBuilder();
getKey(builder, this.methodSymbol, this.methodType, this.resolver);
return builder.toString();
} catch(BindingKeyException bke) {
return null;
}
}

static void getKey(StringBuilder builder, MethodSymbol methodSymbol, MethodType methodType, JavacBindingResolver resolver) {
static void getKey(StringBuilder builder, MethodSymbol methodSymbol, MethodType methodType, JavacBindingResolver resolver) throws BindingKeyException {
Symbol ownerSymbol = methodSymbol.owner;
while (ownerSymbol != null && !(ownerSymbol instanceof TypeSymbol)) {
ownerSymbol = ownerSymbol.owner;
}
if (ownerSymbol instanceof TypeSymbol ownerTypeSymbol) {
JavacTypeBinding.getKey(builder, resolver.getTypes().erasure(ownerTypeSymbol.type), false);
} else {
throw new IllegalArgumentException("Method has no owning class");
throw new BindingKeyException(new IllegalArgumentException("Method has no owning class"));
}
builder.append('.');
if (!methodSymbol.isConstructor()) {
Expand Down
Loading

0 comments on commit dcf19f4

Please sign in to comment.