Skip to content

Commit

Permalink
Fix getting bounds for a capture binding bounded by typevar
Browse files Browse the repository at this point in the history
Also fix the binding key and display name for capture bindings

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 committed Aug 13, 2024
1 parent 180cf6f commit 789db72
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
import org.eclipse.jdt.internal.compiler.codegen.ConstantPool;
import org.eclipse.jdt.internal.core.SourceType;
import org.eclipse.jdt.internal.core.util.Util;

import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Printer;
import com.sun.tools.javac.code.Kinds.Kind;
import com.sun.tools.javac.code.Kinds.KindSelector;
import com.sun.tools.javac.code.Symbol;
Expand Down Expand Up @@ -266,7 +268,7 @@ public String getKey(Type t, Name n) {
static void getKey(StringBuilder builder, Type typeToBuild, boolean isLeaf) throws BindingKeyException {
getKey(builder, typeToBuild, typeToBuild.asElement().flatName(), isLeaf, false);
}

static void getKey(StringBuilder builder, Type typeToBuild, boolean isLeaf, boolean includeParameters) throws BindingKeyException {
getKey(builder, typeToBuild, typeToBuild.asElement().flatName(), isLeaf, includeParameters);
}
Expand All @@ -275,6 +277,14 @@ static void getKey(StringBuilder builder, Type typeToBuild, Name n, boolean isLe
if (typeToBuild instanceof Type.JCNoType) {
return;
}
if (typeToBuild instanceof Type.CapturedType capturedType) {
builder.append('!');
getKey(builder, capturedType.wildcard, false, includeParameters);
// taken from Type.CapturedType.toString()
builder.append((capturedType.hashCode() & 0xFFFFFFFFL) % 997);
builder.append(';');
return;
}
if (typeToBuild.hasTag(TypeTag.UNKNOWN)) {
builder.append('*');
return;
Expand Down Expand Up @@ -600,7 +610,7 @@ public String getName() {
}
StringBuilder builder = new StringBuilder(this.typeSymbol.getSimpleName().toString());
ITypeBinding[] types = this.getUncheckedTypeArguments(this.type, this.typeSymbol);

if (types != null && types.length > 0) {
builder.append("<");
for (var typeArgument : types) {
Expand All @@ -625,11 +635,7 @@ public IPackageBinding getPackage() {
public String getQualifiedName() {
return getQualifiedNameImpl(this.type, this.typeSymbol, this.typeSymbol.owner, !this.isDeclaration);
}
private String getQualifiedNameImpl(Type type, TypeSymbol typeSymbol, Symbol owner, boolean includeParameters) {
if (owner instanceof MethodSymbol) {
return "";
}

protected String getQualifiedNameImpl(Type type, TypeSymbol typeSymbol, Symbol owner, boolean includeParameters) {
if (owner instanceof MethodSymbol) {
return "";
}
Expand Down Expand Up @@ -688,7 +694,7 @@ private String getQualifiedNameImpl(Type type, TypeSymbol typeSymbol, Symbol own
res.append(">");
}
}

// remove annotations here
int annotationIndex = -1;
while ((annotationIndex = res.lastIndexOf("@")) >= 0) {
Expand Down Expand Up @@ -804,6 +810,9 @@ public ITypeBinding[] getTypeBounds() {
}
return new ITypeBinding[] { this.resolver.bindings.getTypeBinding(bounds) };
} else if (this.type instanceof WildcardType wildcardType) {
if (wildcardType.bound instanceof Type.TypeVar typeVar) {
return this.resolver.bindings.getTypeVariableBinding(typeVar).getTypeBounds();
}
return new ITypeBinding[] { wildcardType.isUnbound() || wildcardType.isSuperBound() ?
this.resolver.resolveWellKnownType(Object.class.getName()) :
this.resolver.bindings.getTypeBinding(wildcardType.bound) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.jdt.core.dom.JavacBindingResolver;
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;

import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Symbol.TypeVariableSymbol;
import com.sun.tools.javac.code.Type.TypeVar;

Expand All @@ -27,16 +28,30 @@
public abstract class JavacTypeVariableBinding extends JavacTypeBinding {
private final TypeVariableSymbol sym;
private final JavacBindingResolver bindingResolver;
private final TypeVar typeVar;

public JavacTypeVariableBinding(TypeVar type, TypeVariableSymbol sym, JavacBindingResolver bindingResolver) {
super(type, sym, false, bindingResolver);
this.typeVar = type;
this.sym = sym;
this.bindingResolver = bindingResolver;
}

@Override
public String getKey() {
StringBuilder builder = new StringBuilder();
if (this.typeVar instanceof Type.CapturedType capturedType) {
try {
builder.append('!');
JavacTypeBinding.getKey(builder, capturedType.wildcard, false, true);
// taken from Type.CapturedType.toString()
builder.append((capturedType.hashCode() & 0xFFFFFFFFL) % 997);
builder.append(';');
return builder.toString();
} catch (BindingKeyException e) {
return null;
}
}
if (this.sym.owner != null) {
IBinding ownerBinding = this.bindingResolver.bindings.getBinding(this.sym.owner, null);
if (ownerBinding != null) {
Expand All @@ -51,14 +66,17 @@ public String getKey() {

@Override
public String getQualifiedName() {
if (this.typeVar instanceof Type.CapturedType) {
return "";
}
return sym.getSimpleName().toString();
}

/**
* this is the one that's used in method params and such, not the one that's actually used as it's final resting place (RIP)
* @param sym
* @return
* @throws BindingKeyException
* @throws BindingKeyException
*/
static String getTypeVariableKey(TypeVariableSymbol sym) throws BindingKeyException {
StringBuilder builder = new StringBuilder();
Expand All @@ -77,6 +95,14 @@ static String getTypeVariableKey(TypeVariableSymbol sym) throws BindingKeyExcept

@Override
public String toString() {
if (this.typeVar instanceof Type.CapturedType capturedType) {
StringBuilder builder = new StringBuilder();
builder.append("capture#");
builder.append((capturedType.hashCode() & 0xFFFFFFFFL) % 997);
builder.append("-of");
builder.append(getQualifiedNameImpl(capturedType.wildcard, capturedType.wildcard.tsym, capturedType.wildcard.tsym.owner, true));
return builder.toString();
}
return getKey();
}
}

0 comments on commit 789db72

Please sign in to comment.