Skip to content

Commit

Permalink
TypeBinding: What happens without custom hash()/equals()
Browse files Browse the repository at this point in the history
  • Loading branch information
EcljpseB0T committed Dec 6, 2024
1 parent 11bf464 commit 5789caa
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,6 @@ public PackageBinding getPackage() {
return this.leafComponentType.getPackage();
}

@Override
public int hashCode() {
return this.leafComponentType == null ? super.hashCode() : this.leafComponentType.hashCode();
}

/* Answer true if the receiver type can be assigned to the argument type (right)
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,25 +208,6 @@ public String toString() {
return debugName();
}

@Override
public int hashCode() {
int code = this.typeParameter.hashCode() + 17 * this.rank;
if (this.site != null) {
code = 31 * code + this.site.sourceStart();
code = 31 * code + this.site.sourceEnd();
}
return code;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof InferenceVariable))
return false;
InferenceVariable other = (InferenceVariable) obj;
return this.rank == other.rank
&& InferenceContext18.isSameSite(this.site, other.site)
&& TypeBinding.equalsEquals(this.typeParameter, other.typeParameter);
}

@Override
public TypeBinding erasure() {
// lazily initialize field that may be required in super.erasure():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ public TypeBinding clone(TypeBinding outerType) {
return copy;
}

@Override
public int hashCode() {
return this.enclosingType.hashCode();
}
/*
* Overriden for code assist. In this case, the constantPoolName() has not been computed yet.
* Slam the source name so that the signature is syntactically correct.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1204,15 +1204,6 @@ public TypeVariableBinding getTypeVariable(char[] variableName) {
return null;
}

@Override
public int hashCode() {
// ensure ReferenceBindings hash to the same position as UnresolvedReferenceBindings so they can be replaced without rehashing
// ALL ReferenceBindings are unique when created so equals() is the same as ==
return (this.compoundName == null || this.compoundName.length == 0)
? super.hashCode()
: CharOperation.hashCode(this.compoundName[this.compoundName.length - 1]);
}

final int identityHashCode() {
return super.hashCode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1829,4 +1829,14 @@ public boolean isNonDenotable() {
public boolean isSealed() {
return false;
}
@Override
public final int hashCode() {
// just make sure it is not overridden
return super.hashCode();
}
@Override
public final boolean equals(Object obj) {
// just make sure it is not overridden
return super.equals(obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
import org.eclipse.jdt.internal.compiler.util.Util;

/* TypeSystem: An abstraction responsible for keeping track of types that undergo "derivation" of some sort and the derived types produced thus.
Expand Down Expand Up @@ -128,31 +127,6 @@ public void swapUnresolved(UnresolvedReferenceBinding unresolvedType, ReferenceB
}
}
}
@Override
public boolean equals(Object other) {
PTBKey that = (PTBKey) other; // homogeneous container.
return this.type == that.type && this.enclosingType == that.enclosingType && Util.effectivelyEqual(this.arguments, that.arguments); //$IDENTITY-COMPARISON$
}
final int hash(TypeBinding b) {
if(b instanceof WildcardBinding || b instanceof TypeVariableBinding || b.getClass() == ParameterizedTypeBinding.class) {
return System.identityHashCode(b);
}
return b.hashCode();
}
@Override
public int hashCode() {
final int prime=31;
int hashCode = 1 + hash(this.type);
if (this.enclosingType != null && this.enclosingType.getClass() == ParameterizedTypeBinding.class) {
// Note: this works as in swapUnresolved, a null enclosingType is never replaced by a
// ParameterizedTypeBinding (just by a non-generic or RawTypeBinding)
hashCode = hashCode * prime + System.identityHashCode(this.enclosingType);
}
for (int i = 0, length = this.arguments == null ? 0 : this.arguments.length; i < length; i++) {
hashCode = hashCode * prime + hash(this.arguments[i]);
}
return hashCode;
}
}

HashMap<PTBKey, ParameterizedTypeBinding []> hashedParameterizedTypes = new HashMap<>(256);
Expand Down Expand Up @@ -219,12 +193,12 @@ void put (ReferenceBinding genericType, TypeBinding[] typeArguments, ReferenceBi
private int typeid = TypeIds.T_LastWellKnownTypeId;
private TypeBinding [][] types;
protected HashedParameterizedTypes parameterizedTypes; // auxiliary fast lookup table for parameterized types.
private SimpleLookupTable annotationTypes; // cannot store in types, since AnnotationBinding is not a TypeBinding and we don't want types to operate at Binding level.
private HashMap<ReferenceBinding, AnnotationBinding> annotationTypes; // cannot store in types, since AnnotationBinding is not a TypeBinding and we don't want types to operate at Binding level.
LookupEnvironment environment;

public TypeSystem(LookupEnvironment environment) {
this.environment = environment;
this.annotationTypes = new SimpleLookupTable(16);
this.annotationTypes = new HashMap<>();
this.typeid = TypeIds.T_LastWellKnownTypeId;
this.types = new TypeBinding[TypeIds.T_LastWellKnownTypeId * 2][];
this.parameterizedTypes = new HashedParameterizedTypes();
Expand Down Expand Up @@ -560,7 +534,7 @@ alternate code paths. Unless care is exercised, we will end up with duplicate ob
We may return a resolved annotation when requested for unresolved one, but not vice versa.
*/
public final AnnotationBinding getAnnotationType(ReferenceBinding annotationType, boolean requiredResolved) {
AnnotationBinding annotation = (AnnotationBinding) this.annotationTypes.get(annotationType);
AnnotationBinding annotation = this.annotationTypes.get(annotationType);
if (annotation == null) {
if (requiredResolved)
annotation = new AnnotationBinding(annotationType, Binding.NO_ELEMENT_VALUE_PAIRS);
Expand Down Expand Up @@ -589,7 +563,7 @@ public void cleanUp(int typeId) {
}

public void reset() {
this.annotationTypes = new SimpleLookupTable(16);
this.annotationTypes = new HashMap<>();
this.typeid = TypeIds.T_LastWellKnownTypeId;
this.types = new TypeBinding[TypeIds.T_LastWellKnownTypeId * 2][];
this.parameterizedTypes = new HashedParameterizedTypes();
Expand All @@ -613,14 +587,10 @@ public void updateCaches(UnresolvedReferenceBinding unresolvedType, ReferenceBin
}
}
}
if (this.annotationTypes.get(unresolvedType) != null) { // update the key
Object[] keys = this.annotationTypes.keyTable;
for (int i = 0, l = keys.length; i < l; i++) {
if (keys[i] == unresolvedType) {
keys[i] = resolvedType; // hashCode is based on compoundName so this works.
break;
}
}
AnnotationBinding removed = this.annotationTypes.remove(unresolvedType);
if (removed != null) {
// update the key
this.annotationTypes.put(resolvedType, removed);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,6 @@ public char[] genericTypeSignature() {
return this.genericSignature;
}

@Override
public int hashCode() {
return this.genericType.hashCode();
}

@Override
public boolean hasTypeBit(int bit) {
if (this.typeBits == TypeIds.BitUninitialized) {
Expand Down

0 comments on commit 5789caa

Please sign in to comment.