From 01781a6417f793d1e769188e1ee5cbb5d714bf60 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Wed, 24 Apr 2024 12:08:52 -0400 Subject: [PATCH] [WIP] use MethodType for method bindings Signed-off-by: David Thompson --- .../jdt/core/dom/JavacBindingResolver.java | 24 +++++++++---------- .../dom/JavacMemberValuePairBinding.java | 3 ++- .../javac/dom/JavacMethodBinding.java | 24 ++++++++++++------- .../internal/javac/dom/JavacTypeBinding.java | 6 ++--- .../javac/dom/JavacVariableBinding.java | 2 +- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java index 7a5624cf74e..ea8ec3ec345 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java @@ -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 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); } @@ -211,15 +211,14 @@ IVariableBinding resolveField(FieldAccess fieldAccess) { IMethodBinding resolveMethod(MethodInvocation method) { resolve(); JCTree javacElement = this.converter.domToJavac.get(method); - final java.util.List 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; } @@ -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; } @@ -241,18 +240,17 @@ IBinding resolveName(Name name) { if (tree == null) { tree = this.converter.domToJavac.get(name.getParent()); } - final java.util.List 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; } diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMemberValuePairBinding.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMemberValuePairBinding.java index ddf029fa576..f92cb5f6be0 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMemberValuePairBinding.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMemberValuePairBinding.java @@ -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; } diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMethodBinding.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMethodBinding.java index 453f6d36f8f..37c5023f4b3 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMethodBinding.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMethodBinding.java @@ -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 typeArguments; - public JavacMethodBinding(MethodSymbol sym, JavacBindingResolver resolver, List 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 @@ -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); } @@ -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); } diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java index 88bb79c71b3..f81c53abf53 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java @@ -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); } @@ -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); @@ -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) { } diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacVariableBinding.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacVariableBinding.java index 39d9697b211..d65bd3e907c 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacVariableBinding.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacVariableBinding.java @@ -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);