mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-01 23:47:50 +00:00
clean up TypeKey
This commit is contained in:
parent
7c32626bc0
commit
c8d24f1694
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user