clean up TypeKey

This commit is contained in:
dfsek 2021-12-25 11:01:48 -07:00
parent 7c32626bc0
commit c8d24f1694
2 changed files with 40 additions and 44 deletions

View File

@ -20,6 +20,7 @@ import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Stream;
@ -72,4 +73,41 @@ public final class ReflectionUtil {
public static String typeToString(Type type) {
return type instanceof Class ? ((Class<?>) type).getName() : type.toString();
}
public static boolean equals(Type a, Type b) {
if(a == b) {
return true;
} else if(a instanceof Class) {
return a.equals(b);
} else if(a instanceof ParameterizedType pa) {
if(!(b instanceof ParameterizedType pb)) {
return false;
}
return Objects.equals(pa.getOwnerType(), pb.getOwnerType())
&& pa.getRawType().equals(pb.getRawType())
&& Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments());
} else if(a instanceof GenericArrayType ga) {
if(!(b instanceof GenericArrayType gb)) {
return false;
}
return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
} else if(a instanceof WildcardType wa) {
if(!(b instanceof WildcardType wb)) {
return false;
}
return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds())
&& Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
} else if(a instanceof TypeVariable<?> va) {
if(!(b instanceof TypeVariable<?> vb)) {
return false;
}
return va.getGenericDeclaration() == vb.getGenericDeclaration()
&& va.getName().equals(vb.getName());
} else {
return false;
}
}
}

View File

@ -11,13 +11,8 @@ import com.dfsek.tectonic.util.ClassAnnotatedTypeImpl;
import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.Objects;
public class TypeKey<T> {
@ -46,43 +41,6 @@ public class TypeKey<T> {
return new TypeKey<>(clazz);
}
public static boolean equals(Type a, Type b) {
if(a == b) {
return true;
} else if(a instanceof Class) {
return a.equals(b);
} else if(a instanceof ParameterizedType pa) {
if(!(b instanceof ParameterizedType pb)) {
return false;
}
return Objects.equals(pa.getOwnerType(), pb.getOwnerType())
&& pa.getRawType().equals(pb.getRawType())
&& Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments());
} else if(a instanceof GenericArrayType ga) {
if(!(b instanceof GenericArrayType gb)) {
return false;
}
return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
} else if(a instanceof WildcardType wa) {
if(!(b instanceof WildcardType wb)) {
return false;
}
return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds())
&& Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
} else if(a instanceof TypeVariable<?> va) {
if(!(b instanceof TypeVariable<?> vb)) {
return false;
}
return va.getGenericDeclaration() == vb.getGenericDeclaration()
&& va.getName().equals(vb.getName());
} else {
return false;
}
}
private static Type getSuperclassTypeParameter(Class<?> subclass) {
Type superclass = subclass.getGenericSuperclass();
if(superclass instanceof Class) {
@ -115,7 +73,7 @@ public class TypeKey<T> {
return type;
}
public AnnotatedType getAnnotatedType() {
public final AnnotatedType getAnnotatedType() {
return annotatedType;
}
@ -127,7 +85,7 @@ public class TypeKey<T> {
@Override
public final boolean equals(Object o) {
return o instanceof TypeKey<?>
&& equals(type, ((TypeKey<?>) o).type);
&& ReflectionUtil.equals(type, ((TypeKey<?>) o).type);
}
@Override