Skip to content

Commit

Permalink
Ensure single instances of each binding
Browse files Browse the repository at this point in the history
Fixes #395
  • Loading branch information
mickaelistria committed May 26, 2024
1 parent 7d3d84f commit a8a6518
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 137 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import com.sun.tools.javac.code.Attribute.Compound;

public class JavacAnnotationBinding implements IAnnotationBinding {
public abstract class JavacAnnotationBinding implements IAnnotationBinding {

private final JavacBindingResolver resolver;
private final Compound annotation;
Expand Down Expand Up @@ -99,13 +99,13 @@ public boolean isEqualTo(IBinding binding) {
@Override
public IMemberValuePairBinding[] getAllMemberValuePairs() {
return this.annotation.getElementValues().entrySet().stream()
.map(entry -> this.resolver.canonicalize(new JavacMemberValuePairBinding(entry.getKey(), entry.getValue(), this.resolver)))
.map(entry -> this.resolver.bindings.getMemberValuePairBinding(entry.getKey(), entry.getValue()))
.toArray(IMemberValuePairBinding[]::new);
}

@Override
public ITypeBinding getAnnotationType() {
return this.resolver.canonicalize(new JavacTypeBinding(this.annotation.type, this.resolver));
return this.resolver.bindings.getTypeBinding(this.annotation.type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Symbol.MethodSymbol;

public class JavacMemberValuePairBinding implements IMemberValuePairBinding {
public abstract class JavacMemberValuePairBinding implements IMemberValuePairBinding {

public final JavacMethodBinding method;
public final Attribute value;
private final JavacBindingResolver resolver;

public JavacMemberValuePairBinding(MethodSymbol key, Attribute value, JavacBindingResolver resolver) {
this.method = resolver.canonicalize(new JavacMethodBinding(key.type.asMethodType(), key, resolver));
this.method = resolver.bindings.getMethodBinding(key.type.asMethodType(), key);
this.value = value;
this.resolver = resolver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import com.sun.tools.javac.code.Type.JCNoType;
import com.sun.tools.javac.code.Type.MethodType;

public class JavacMethodBinding implements IMethodBinding {
public abstract class JavacMethodBinding implements IMethodBinding {

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

Expand Down Expand Up @@ -68,7 +68,7 @@ public int hashCode() {

@Override
public IAnnotationBinding[] getAnnotations() {
return methodSymbol.getAnnotationMirrors().stream().map(ann -> this.resolver.canonicalize(new JavacAnnotationBinding(ann, this.resolver, this))).toArray(IAnnotationBinding[]::new);
return methodSymbol.getAnnotationMirrors().stream().map(ann -> this.resolver.bindings.getAnnotationBinding(ann, this)).toArray(IAnnotationBinding[]::new);
}

@Override
Expand Down Expand Up @@ -126,7 +126,7 @@ public boolean isSynthetic() {

@Override
public IJavaElement getJavaElement() {
IJavaElement parent = this.resolver.getBinding(this.methodSymbol.owner, this.methodType).getJavaElement();
IJavaElement parent = this.resolver.bindings.getBinding(this.methodSymbol.owner, this.methodType).getJavaElement();
if (parent instanceof IType type) {
// prefer DOM object (for type parameters)
MethodDeclaration methodDeclaration = (MethodDeclaration)this.resolver.findDeclaringNode(this);
Expand Down Expand Up @@ -171,7 +171,7 @@ static void getKey(StringBuilder builder, MethodSymbol methodSymbol, JavacBindin
if (!methodSymbol.getTypeParameters().isEmpty()) {
builder.append('<');
for (var typeParam : methodSymbol.getTypeParameters()) {
JavacTypeVariableBinding typeVarBinding = new JavacTypeVariableBinding(typeParam);
JavacTypeVariableBinding typeVarBinding = resolver.bindings.getTypeVariableBinding(typeParam);
builder.append(typeVarBinding.getKey());
}
builder.append('>');
Expand Down Expand Up @@ -232,7 +232,7 @@ public ITypeBinding getDeclaringClass() {
Symbol parentSymbol = this.methodSymbol.owner;
do {
if (parentSymbol instanceof ClassSymbol clazz) {
return this.resolver.canonicalize(new JavacTypeBinding(clazz.type, this.resolver));
return this.resolver.bindings.getTypeBinding(clazz.type);
}
parentSymbol = parentSymbol.owner;
} while (parentSymbol != null);
Expand All @@ -245,9 +245,9 @@ public IBinding getDeclaringMember() {
return null;
}
if (this.methodSymbol.owner instanceof MethodSymbol methodSymbol) {
return this.resolver.canonicalize(new JavacMethodBinding(methodSymbol.type.asMethodType(), methodSymbol, resolver));
return this.resolver.bindings.getMethodBinding(methodSymbol.type.asMethodType(), methodSymbol);
} else if (this.methodSymbol.owner instanceof VarSymbol variableSymbol) {
return this.resolver.canonicalize(new JavacVariableBinding(variableSymbol, resolver));
return this.resolver.bindings.getVariableBinding(variableSymbol);
}
throw new IllegalArgumentException("Unexpected owner type: " + this.methodSymbol.owner.getClass().getCanonicalName());
}
Expand All @@ -261,26 +261,26 @@ public Object getDefaultValue() {
public IAnnotationBinding[] getParameterAnnotations(int paramIndex) {
VarSymbol parameter = this.methodSymbol.params.get(paramIndex);
return parameter.getAnnotationMirrors().stream() //
.map(annotation -> this.resolver.canonicalize(new JavacAnnotationBinding(annotation, this.resolver, this))) //
.map(annotation -> this.resolver.bindings.getAnnotationBinding(annotation, this)) //
.toArray(IAnnotationBinding[]::new);
}

@Override
public ITypeBinding[] getParameterTypes() {
return this.methodSymbol.params().stream()
.map(param -> param.type)
.map(type -> this.resolver.canonicalize(new JavacTypeBinding(type, this.resolver)))
.map(this.resolver.bindings::getTypeBinding)
.toArray(ITypeBinding[]::new);
}

@Override
public ITypeBinding getDeclaredReceiverType() {
return this.resolver.canonicalize(new JavacTypeBinding(this.methodSymbol.getReceiverType(), this.resolver));
return this.resolver.bindings.getTypeBinding(this.methodSymbol.getReceiverType());
}

@Override
public ITypeBinding getReturnType() {
return this.resolver.canonicalize(new JavacTypeBinding(this.methodSymbol.getReturnType(), this.resolver));
return this.resolver.bindings.getTypeBinding(this.methodSymbol.getReturnType());
}

@SuppressWarnings("unchecked")
Expand All @@ -298,7 +298,7 @@ public ITypeBinding[] getExceptionTypes() {
@Override
public ITypeBinding[] getTypeParameters() {
return this.methodSymbol.getTypeParameters().stream()
.map(symbol -> this.resolver.canonicalize(new JavacTypeBinding(symbol.type, this.resolver)))
.map(symbol -> this.resolver.bindings.getTypeBinding(symbol.type))
.toArray(ITypeBinding[]::new);
}

Expand All @@ -323,7 +323,7 @@ public ITypeBinding[] getTypeArguments() {
return NO_TYPE_ARGUMENTS;
}
return this.methodType.getTypeArguments().stream()
.map(type -> this.resolver.canonicalize(new JavacTypeBinding(type, this.resolver)))
.map(this.resolver.bindings::getTypeBinding)
.toArray(ITypeBinding[]::new);
}

Expand Down Expand Up @@ -365,7 +365,7 @@ public IVariableBinding[] getSyntheticOuterLocals() {
return new IVariableBinding[0];
}
return this.methodSymbol.capturedLocals.stream() //
.map(capturedLocal -> this.resolver.canonicalize(new JavacVariableBinding(capturedLocal, this.resolver))) //
.map(this.resolver.bindings::getVariableBinding) //
.toArray(IVariableBinding[]::new);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
import com.sun.tools.javac.code.Symbol.PackageSymbol;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.code.Type.ModuleType;
public class JavacModuleBinding implements IModuleBinding {
public abstract class JavacModuleBinding implements IModuleBinding {

private static final ITypeBinding[] NO_TYPE_ARGUMENTS = new ITypeBinding[0];
final JavacBindingResolver resolver;
Expand All @@ -60,7 +59,7 @@ public JavacModuleBinding(final ModuleSymbol moduleSymbol, final ModuleType modu
public IAnnotationBinding[] getAnnotations() {
// TODO - don't see any way to get this?
List<Attribute.Compound> list = moduleSymbol.getRawAttributes();
return list.stream().map((x) -> new JavacAnnotationBinding(x, this.resolver, this)).toArray(JavacAnnotationBinding[]::new);
return list.stream().map(x -> this.resolver.bindings.getAnnotationBinding(x, this)).toArray(JavacAnnotationBinding[]::new);
}

@Override
Expand Down Expand Up @@ -113,20 +112,26 @@ public boolean isOpen() {

@Override
public IModuleBinding[] getRequiredModules() {
RequiresDirective[] arr = this.moduleSymbol.getDirectives().stream().filter((x) -> x.getKind() == DirectiveKind.REQUIRES).map((x) -> (RequiresDirective)x).toArray(RequiresDirective[]::new);
RequiresDirective[] arr = this.moduleSymbol.getDirectives().stream() //
.filter(x -> x.getKind() == DirectiveKind.REQUIRES) //
.map(x -> (RequiresDirective)x) //
.toArray(RequiresDirective[]::new);
IModuleBinding[] arr2 = new IModuleBinding[arr.length];
for( int i = 0; i < arr.length; i++ ) {
arr2[i] = new JavacModuleBinding((ModuleType)arr[i].module.type, this.resolver);
arr2[i] = this.resolver.bindings.getModuleBinding((ModuleType)arr[i].module.type);
}
return arr2;
}

@Override
public IPackageBinding[] getExportedPackages() {
ExportsDirective[] arr = this.moduleSymbol.getDirectives().stream().filter((x) -> x.getKind() == DirectiveKind.EXPORTS).map((x) -> (ExportsDirective)x).toArray(ExportsDirective[]::new);
ExportsDirective[] arr = this.moduleSymbol.getDirectives().stream() //
.filter(x -> x.getKind() == DirectiveKind.EXPORTS) //
.map(x -> (ExportsDirective)x) //
.toArray(ExportsDirective[]::new);
IPackageBinding[] arr2 = new IPackageBinding[arr.length];
for( int i = 0; i < arr.length; i++ ) {
arr2[i] = new JavacPackageBinding((PackageSymbol)arr[i].packge, this.resolver);
arr2[i] = this.resolver.bindings.getPackageBinding(arr[i].packge);
}
return arr2;
}
Expand All @@ -135,10 +140,10 @@ public IPackageBinding[] getExportedPackages() {
public String[] getExportedTo(IPackageBinding packageBinding) {
ExportsDirective[] arr = this.moduleSymbol.getDirectives().stream().filter((x) -> x.getKind() == DirectiveKind.EXPORTS).map((x) -> (ExportsDirective)x).toArray(ExportsDirective[]::new);
for( int i = 0; i < arr.length; i++ ) {
JavacPackageBinding tmp = new JavacPackageBinding((PackageSymbol)arr[i].packge, this.resolver);
JavacPackageBinding tmp = this.resolver.bindings.getPackageBinding(arr[i].packge);
if( tmp.isUnnamed() == packageBinding.isUnnamed() &&
tmp.getName().equals(packageBinding.getName())) {
return arr[i].getTargetModules().stream().map((x) -> x.toString()).toArray(String[]::new);
return arr[i].getTargetModules().stream().map(ModuleSymbol::toString).toArray(String[]::new);
}
}
return new String[0];
Expand All @@ -149,7 +154,7 @@ public IPackageBinding[] getOpenedPackages() {
OpensDirective[] arr = this.moduleSymbol.getDirectives().stream().filter((x) -> x.getKind() == DirectiveKind.OPENS).map((x) -> (OpensDirective)x).toArray(OpensDirective[]::new);
IPackageBinding[] arr2 = new IPackageBinding[arr.length];
for( int i = 0; i < arr.length; i++ ) {
arr2[i] = new JavacPackageBinding((PackageSymbol)arr[i].packge, this.resolver);
arr2[i] = this.resolver.bindings.getPackageBinding(arr[i].packge);
}
return arr2;
}
Expand All @@ -158,7 +163,7 @@ public IPackageBinding[] getOpenedPackages() {
public String[] getOpenedTo(IPackageBinding packageBinding) {
OpensDirective[] arr = this.moduleSymbol.getDirectives().stream().filter((x) -> x.getKind() == DirectiveKind.OPENS).map((x) -> (OpensDirective)x).toArray(OpensDirective[]::new);
for( int i = 0; i < arr.length; i++ ) {
JavacPackageBinding tmp = new JavacPackageBinding((PackageSymbol)arr[i].packge, this.resolver);
JavacPackageBinding tmp = this.resolver.bindings.getPackageBinding(arr[i].packge);
if( tmp.isUnnamed() == packageBinding.isUnnamed() &&
tmp.getName().equals(packageBinding.getName())) {
return arr[i].getTargetModules().stream().map((x) -> x.toString()).toArray(String[]::new);
Expand All @@ -172,7 +177,7 @@ public ITypeBinding[] getUses() {
UsesDirective[] arr = this.moduleSymbol.getDirectives().stream().filter((x) -> x.getKind() == DirectiveKind.USES).map((x) -> (UsesDirective)x).toArray(UsesDirective[]::new);
ITypeBinding[] arr2 = new ITypeBinding[arr.length];
for( int i = 0; i < arr.length; i++ ) {
arr2[i] = new JavacTypeBinding(arr[i].getService().type, this.resolver);
arr2[i] = this.resolver.bindings.getTypeBinding(arr[i].getService().type);
}
return arr2;
}
Expand All @@ -182,7 +187,7 @@ public ITypeBinding[] getServices() {
ProvidesDirective[] arr = this.moduleSymbol.getDirectives().stream().filter((x) -> x.getKind() == DirectiveKind.PROVIDES).map((x) -> (ProvidesDirective)x).toArray(ProvidesDirective[]::new);
ITypeBinding[] arr2 = new ITypeBinding[arr.length];
for( int i = 0; i < arr.length; i++ ) {
arr2[i] = new JavacTypeBinding(arr[i].getService().type, this.resolver);
arr2[i] = this.resolver.bindings.getTypeBinding(arr[i].getService().type);
}
return arr2;
}
Expand All @@ -191,10 +196,10 @@ public ITypeBinding[] getServices() {
public ITypeBinding[] getImplementations(ITypeBinding service) {
ProvidesDirective[] arr = this.moduleSymbol.getDirectives().stream().filter((x) -> x.getKind() == DirectiveKind.PROVIDES).map((x) -> (ProvidesDirective)x).toArray(ProvidesDirective[]::new);
for( int i = 0; i < arr.length; i++ ) {
JavacTypeBinding tmp = new JavacTypeBinding(arr[i].getService().type, this.resolver);
JavacTypeBinding tmp = this.resolver.bindings.getTypeBinding(arr[i].getService().type);
if(service.getKey().equals(tmp.getKey())) {
// we have our match
JavacTypeBinding[] ret = arr[i].getImplementations().stream().map(x -> new JavacTypeBinding((ClassType)x.type, this.resolver)).toArray(JavacTypeBinding[]::new);
JavacTypeBinding[] ret = arr[i].getImplementations().stream().map(x -> this.resolver.bindings.getTypeBinding((ClassType)x.type)).toArray(JavacTypeBinding[]::new);
return ret;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import com.sun.tools.javac.code.Symbol.PackageSymbol;

public class JavacPackageBinding implements IPackageBinding {
public abstract class JavacPackageBinding implements IPackageBinding {

public final PackageSymbol packageSymbol;
final JavacBindingResolver resolver;
Expand All @@ -48,7 +48,7 @@ public int hashCode() {
@Override
public IAnnotationBinding[] getAnnotations() {
return this.packageSymbol.getAnnotationMirrors().stream()
.map(am -> this.resolver.canonicalize(new JavacAnnotationBinding(am, resolver, this)))
.map(am -> this.resolver.bindings.getAnnotationBinding(am, this))
.toArray(IAnnotationBinding[]::new);
}

Expand Down Expand Up @@ -101,7 +101,7 @@ public IJavaElement getJavaElement() {
}

public IModuleBinding getModule() {
return new JavacModuleBinding(this.packageSymbol.modle, this.resolver);
return this.resolver.bindings.getModuleBinding(this.packageSymbol.modle);
}

@Override
Expand Down
Loading

0 comments on commit a8a6518

Please sign in to comment.