mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-22 08:10:40 +00:00
clean up TypeKey
This commit is contained in:
@@ -20,6 +20,7 @@ import java.lang.reflect.Type;
|
|||||||
import java.lang.reflect.TypeVariable;
|
import java.lang.reflect.TypeVariable;
|
||||||
import java.lang.reflect.WildcardType;
|
import java.lang.reflect.WildcardType;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@@ -72,4 +73,41 @@ public final class ReflectionUtil {
|
|||||||
public static String typeToString(Type type) {
|
public static String typeToString(Type type) {
|
||||||
return type instanceof Class ? ((Class<?>) type).getName() : type.toString();
|
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.AnnotatedParameterizedType;
|
||||||
import java.lang.reflect.AnnotatedType;
|
import java.lang.reflect.AnnotatedType;
|
||||||
import java.lang.reflect.GenericArrayType;
|
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.lang.reflect.Type;
|
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> {
|
public class TypeKey<T> {
|
||||||
@@ -46,43 +41,6 @@ public class TypeKey<T> {
|
|||||||
return new TypeKey<>(clazz);
|
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) {
|
private static Type getSuperclassTypeParameter(Class<?> subclass) {
|
||||||
Type superclass = subclass.getGenericSuperclass();
|
Type superclass = subclass.getGenericSuperclass();
|
||||||
if(superclass instanceof Class) {
|
if(superclass instanceof Class) {
|
||||||
@@ -115,7 +73,7 @@ public class TypeKey<T> {
|
|||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AnnotatedType getAnnotatedType() {
|
public final AnnotatedType getAnnotatedType() {
|
||||||
return annotatedType;
|
return annotatedType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +85,7 @@ public class TypeKey<T> {
|
|||||||
@Override
|
@Override
|
||||||
public final boolean equals(Object o) {
|
public final boolean equals(Object o) {
|
||||||
return o instanceof TypeKey<?>
|
return o instanceof TypeKey<?>
|
||||||
&& equals(type, ((TypeKey<?>) o).type);
|
&& ReflectionUtil.equals(type, ((TypeKey<?>) o).type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user