Skip to content

Commit

Permalink
Refactor ResolvedReferenceType#equals
Browse files Browse the repository at this point in the history
Fixes comparing against lazy types
  • Loading branch information
freya022 committed Mar 20, 2024
1 parent f0ca785 commit b3c51cc
Showing 1 changed file with 17 additions and 18 deletions.
Expand Up @@ -20,10 +20,6 @@
*/
package com.github.javaparser.resolution.types;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;
Expand All @@ -37,6 +33,10 @@
import com.github.javaparser.resolution.types.parametrization.ResolvedTypeParametrized;
import com.github.javaparser.utils.Pair;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* A ReferenceType like a class, an interface or an enum. Note that this type can contain also the values
* specified for the type parameters.
Expand Down Expand Up @@ -88,28 +88,27 @@ public ResolvedReferenceType(ResolvedReferenceTypeDeclaration typeDeclaration, L
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || (!isLazyType(o) && getClass() != o.getClass())
|| (isLazyType(o) && !this.equals(asResolvedReferenceType(o))))
if (o == null)
return false;
ResolvedReferenceType that = asResolvedReferenceType(o);

if (o instanceof LazyType) {
final LazyType lazyType = (LazyType) o;

Check warning on line 95 in javaparser-core/src/main/java/com/github/javaparser/resolution/types/ResolvedReferenceType.java

View check run for this annotation

Codecov / codecov/patch

javaparser-core/src/main/java/com/github/javaparser/resolution/types/ResolvedReferenceType.java#L95

Added line #L95 was not covered by tests
if (!lazyType.isReferenceType())
return false;
return this.equals(lazyType.asReferenceType());

Check warning on line 98 in javaparser-core/src/main/java/com/github/javaparser/resolution/types/ResolvedReferenceType.java

View check run for this annotation

Codecov / codecov/patch

javaparser-core/src/main/java/com/github/javaparser/resolution/types/ResolvedReferenceType.java#L97-L98

Added lines #L97 - L98 were not covered by tests
}

if (getClass() != o.getClass())
return false;

Check warning on line 102 in javaparser-core/src/main/java/com/github/javaparser/resolution/types/ResolvedReferenceType.java

View check run for this annotation

Codecov / codecov/patch

javaparser-core/src/main/java/com/github/javaparser/resolution/types/ResolvedReferenceType.java#L102

Added line #L102 was not covered by tests

ResolvedReferenceType that = (ResolvedReferenceType) o;

Check warning on line 104 in javaparser-core/src/main/java/com/github/javaparser/resolution/types/ResolvedReferenceType.java

View check run for this annotation

Codecov / codecov/patch

javaparser-core/src/main/java/com/github/javaparser/resolution/types/ResolvedReferenceType.java#L104

Added line #L104 was not covered by tests
if (!typeDeclaration.equals(that.typeDeclaration))
return false;
if (!typeParametersMap.equals(that.typeParametersMap))
return false;
return true;
}

private boolean isLazyType(Object type) {
return type !=null && type instanceof LazyType;
}

private ResolvedReferenceType asResolvedReferenceType(Object o) {
if (isLazyType(o)) {
return ((LazyType) o).asReferenceType();
}
return ResolvedReferenceType.class.cast(o);
}

@Override
public int hashCode() {
int result = typeDeclaration.hashCode();
Expand Down

0 comments on commit b3c51cc

Please sign in to comment.