Skip to content

Commit

Permalink
[WIP] use MethodType for method bindings
Browse files Browse the repository at this point in the history
Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Apr 24, 2024
1 parent f7ce1d7 commit 01781a6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ ITypeBinding resolveType(EnumDeclaration enumDecl) {
/**
* TODO: we should avoid using this if possible
*/
public IBinding getBinding(final Symbol owner, final java.util.List<TypeSymbol> typeArguments) {
public IBinding getBinding(final Symbol owner, final com.sun.tools.javac.code.Type type) {
if (owner instanceof final PackageSymbol other) {
return new JavacPackageBinding(other, this);
} else if (owner instanceof final TypeSymbol other) {
return new JavacTypeBinding(other.type, this);
} else if (owner instanceof TypeSymbol) {
return new JavacTypeBinding(type, this);
} else if (owner instanceof final MethodSymbol other) {
return new JavacMethodBinding(other, this, typeArguments);
return new JavacMethodBinding(type.asMethodType(), other, this);
} else if (owner instanceof final VarSymbol other) {
return new JavacVariableBinding(other, this);
}
Expand All @@ -211,15 +211,14 @@ IVariableBinding resolveField(FieldAccess fieldAccess) {
IMethodBinding resolveMethod(MethodInvocation method) {
resolve();
JCTree javacElement = this.converter.domToJavac.get(method);
final java.util.List<TypeSymbol> typeArguments = getTypeArguments(method);
if (javacElement instanceof JCMethodInvocation javacMethodInvocation) {
javacElement = javacMethodInvocation.getMethodSelect();
}
if (javacElement instanceof JCIdent ident && ident.sym instanceof MethodSymbol methodSymbol) {
return new JavacMethodBinding(methodSymbol, this, typeArguments);
return new JavacMethodBinding(ident.type.asMethodType(), methodSymbol, this);
}
if (javacElement instanceof JCFieldAccess fieldAccess && fieldAccess.sym instanceof MethodSymbol methodSymbol) {
return new JavacMethodBinding(methodSymbol, this, typeArguments);
return new JavacMethodBinding(fieldAccess.type.asMethodType(), methodSymbol, this);
}
return null;
}
Expand All @@ -229,7 +228,7 @@ IMethodBinding resolveMethod(MethodDeclaration method) {
resolve();
JCTree javacElement = this.converter.domToJavac.get(method);
if (javacElement instanceof JCMethodDecl methodDecl) {
return new JavacMethodBinding(methodDecl.sym, this, null);
return new JavacMethodBinding(methodDecl.type.asMethodType(), methodDecl.sym, this);
}
return null;
}
Expand All @@ -241,18 +240,17 @@ IBinding resolveName(Name name) {
if (tree == null) {
tree = this.converter.domToJavac.get(name.getParent());
}
final java.util.List<TypeSymbol> typeArguments = getTypeArguments(name);
if (tree instanceof JCIdent ident && ident.sym != null) {
return getBinding(ident.sym, typeArguments);
return getBinding(ident.sym, ident.type);
}
if (tree instanceof JCFieldAccess fieldAccess && fieldAccess.sym != null) {
return getBinding(fieldAccess.sym, typeArguments);
return getBinding(fieldAccess.sym, fieldAccess.type);
}
if (tree instanceof JCClassDecl classDecl && classDecl.sym != null) {
return getBinding(classDecl.sym, typeArguments);
return getBinding(classDecl.sym, classDecl.type);
}
if (tree instanceof JCVariableDecl variableDecl && variableDecl.sym != null) {
return getBinding(variableDecl.sym, typeArguments);
return getBinding(variableDecl.sym, variableDecl.type);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class JavacMemberValuePairBinding implements IMemberValuePairBinding {
private final JavacBindingResolver resolver;

public JavacMemberValuePairBinding(MethodSymbol key, Attribute value, JavacBindingResolver resolver) {
this.method = new JavacMethodBinding(key, resolver, null);
// FIXME
this.method = new JavacMethodBinding(key.type.asMethodType(), key, resolver);
this.value = value;
this.resolver = resolver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.code.Type.JCNoType;
import com.sun.tools.javac.code.Type.MethodType;

public class JavacMethodBinding implements IMethodBinding {

private static final ITypeBinding[] NO_TYPE_ARGUMENTS = new ITypeBinding[0];

public final MethodSymbol methodSymbol;
private final MethodType methodType;
final JavacBindingResolver resolver;
private final List<TypeSymbol> typeArguments;

public JavacMethodBinding(MethodSymbol sym, JavacBindingResolver resolver, List<TypeSymbol> typeArguments) {
this.methodSymbol = sym;
public JavacMethodBinding(MethodType methodType, MethodSymbol methodSymbol, JavacBindingResolver resolver) {
this.methodType = methodType;
this.methodSymbol = methodSymbol;
this.resolver = resolver;
this.typeArguments = typeArguments;
}

@Override
Expand Down Expand Up @@ -219,7 +222,7 @@ public IBinding getDeclaringMember() {
return null;
}
if (this.methodSymbol.owner instanceof MethodSymbol methodSymbol) {
return new JavacMethodBinding(methodSymbol, resolver, null);
return new JavacMethodBinding(methodSymbol.type.asMethodType(), methodSymbol, resolver);
} else if (this.methodSymbol.owner instanceof VarSymbol variableSymbol) {
return new JavacVariableBinding(variableSymbol, resolver);
}
Expand Down Expand Up @@ -283,18 +286,21 @@ public boolean isAnnotationMember() {

@Override
public boolean isGenericMethod() {
return this.typeArguments == null && !this.methodSymbol.getTypeParameters().isEmpty();
return this.methodType.getTypeArguments().isEmpty() && !this.methodSymbol.getTypeParameters().isEmpty();
}

@Override
public boolean isParameterizedMethod() {
return this.typeArguments != null;
return !this.methodType.getTypeArguments().isEmpty();
}

@Override
public ITypeBinding[] getTypeArguments() {
return this.typeArguments.stream()
.map(symbol -> new JavacTypeBinding(symbol.type, this.resolver))
if (this.methodType.getTypeArguments().isEmpty()) {
return NO_TYPE_ARGUMENTS;
}
return this.methodType.getTypeArguments().stream()
.map(type -> new JavacTypeBinding(type, this.resolver))
.toArray(ITypeBinding[]::new);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public IMethodBinding[] getDeclaredMethods() {
return StreamSupport.stream(this.typeSymbol.members().getSymbols().spliterator(), false)
.filter(MethodSymbol.class::isInstance)
.map(MethodSymbol.class::cast)
.map(sym -> new JavacMethodBinding(sym, this.resolver, null))
.map(sym -> new JavacMethodBinding(sym.type.asMethodType(), sym, this.resolver))
.toArray(IMethodBinding[]::new);
}

Expand Down Expand Up @@ -304,7 +304,7 @@ public IMethodBinding getDeclaringMethod() {
Symbol parentSymbol = this.typeSymbol.owner;
do {
if (parentSymbol instanceof final MethodSymbol method) {
return new JavacMethodBinding(method, this.resolver, null);
return new JavacMethodBinding(method.type.asMethodType(), method, this.resolver);
}
parentSymbol = parentSymbol.owner;
} while (parentSymbol != null);
Expand Down Expand Up @@ -339,7 +339,7 @@ public IMethodBinding getFunctionalInterfaceMethod() {
try {
Symbol symbol = types.findDescriptorSymbol(this.typeSymbol);
if (symbol instanceof MethodSymbol methodSymbol) {
return new JavacMethodBinding(methodSymbol, resolver, null);
return new JavacMethodBinding(methodSymbol.type.asMethodType(), methodSymbol, resolver);
}
} catch (FunctionDescriptorLookupError ignore) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public IMethodBinding getDeclaringMethod() {
Symbol parentSymbol = this.variableSymbol.owner;
do {
if (parentSymbol instanceof MethodSymbol method) {
return new JavacMethodBinding(method, this.resolver, null);
return new JavacMethodBinding(method.type.asMethodType(), method, this.resolver);
}
parentSymbol = parentSymbol.owner;
} while (parentSymbol != null);
Expand Down

0 comments on commit 01781a6

Please sign in to comment.