Skip to content

Commit

Permalink
SONARPY-2332 review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-serre-sonarsource committed Nov 22, 2024
1 parent 399db7e commit c50637d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@

public class RuntimeType implements InferredType {

// Name of the type returned by the type() function
private static final String TYPE_CLASS_NAME = "type";

private ClassSymbol typeClass;
private String builtinFullyQualifiedName;
private Set<String> typeClassSuperClassesFQN = null;
Expand All @@ -55,25 +52,21 @@ public boolean isIdentityComparableWith(InferredType other) {
if (other instanceof UnionType) {
return other.isIdentityComparableWith(this);
}
if (isComparingTypeWithMetaclass(other)) {
return true;
}
return this.equals(other);
return isComparingTypeWithMetaclass(other) || this.equals(other);
}

private boolean isComparingTypeWithMetaclass(InferredType other) {
if (other instanceof RuntimeType otherRuntimeType) {
boolean hasOtherMetaClass = hasMetaclassInHierarchy(otherRuntimeType);
boolean hasThisMetaClass = hasMetaclassInHierarchy(this);
return (TYPE_CLASS_NAME.equals(getTypeClass().name()) && hasOtherMetaClass)
|| (hasThisMetaClass && TYPE_CLASS_NAME.equals(otherRuntimeType.getTypeClass().name()));
boolean hasOtherMetaClass = hasMetaclassInHierarchy(otherRuntimeType.getTypeClass());
boolean hasThisMetaClass = hasMetaclassInHierarchy(getTypeClass());
return (InferredTypes.TYPE.equals(this) && hasOtherMetaClass)
|| (hasThisMetaClass && InferredTypes.TYPE.equals(otherRuntimeType));
}
return false;
}

private static boolean hasMetaclassInHierarchy(RuntimeType runtimeType) {
return runtimeType.getTypeClass().hasMetaClass()
|| runtimeType.getTypeClass().superClasses().stream().filter(ClassSymbol.class::isInstance).anyMatch(c -> ((ClassSymbol) c).hasMetaClass());
private static boolean hasMetaclassInHierarchy(ClassSymbol classSymbol) {
return classSymbol.hasMetaClass() || classSymbol.superClasses().stream().filter(ClassSymbol.class::isInstance).anyMatch(c -> hasMetaclassInHierarchy((ClassSymbol) c));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ void isIdentityComparableWith() {

@Test
void isIdentityComparableWithMetaclass() {
RuntimeType typeType = new RuntimeType(new ClassSymbolImpl("type", "type"));

ClassSymbolImpl metaclassSymbol = new ClassSymbolImpl("Meta", "Meta");
metaclassSymbol.setHasMetaClass();
RuntimeType metaClassType = new RuntimeType(metaclassSymbol);
Expand All @@ -78,16 +76,16 @@ void isIdentityComparableWithMetaclass() {

UnknownClassType unknownClassType = new UnknownClassType(metaclassSymbol);

assertThat(typeType.isIdentityComparableWith(typeType)).isTrue();
assertThat(typeType.isIdentityComparableWith(metaClassType)).isTrue();
assertThat(typeType.isIdentityComparableWith(superMetaClassType)).isTrue();
assertThat(typeType.isIdentityComparableWith(unknownClassType)).isFalse();
assertThat(InferredTypes.TYPE.isIdentityComparableWith(InferredTypes.TYPE)).isTrue();
assertThat(InferredTypes.TYPE.isIdentityComparableWith(metaClassType)).isTrue();
assertThat(InferredTypes.TYPE.isIdentityComparableWith(superMetaClassType)).isTrue();
assertThat(InferredTypes.TYPE.isIdentityComparableWith(unknownClassType)).isFalse();

assertThat(metaClassType.isIdentityComparableWith(typeType)).isTrue();
assertThat(metaClassType.isIdentityComparableWith(InferredTypes.TYPE)).isTrue();
assertThat(metaClassType.isIdentityComparableWith(metaClassType)).isTrue();
assertThat(metaClassType.isIdentityComparableWith(superMetaClassType)).isFalse();

assertThat(superMetaClassType.isIdentityComparableWith(typeType)).isTrue();
assertThat(superMetaClassType.isIdentityComparableWith(InferredTypes.TYPE)).isTrue();
assertThat(superMetaClassType.isIdentityComparableWith(metaClassType)).isFalse();
assertThat(superMetaClassType.isIdentityComparableWith(superMetaClassType)).isTrue();
}
Expand Down

0 comments on commit c50637d

Please sign in to comment.