add TypeKey#getAnnotatedType

This commit is contained in:
dfsek
2021-07-21 09:48:21 -07:00
parent 6f03cfa600
commit 5c3cd73c03

View File

@@ -7,17 +7,20 @@ import java.util.Objects;
public class TypeKey<T> {
final Class<? super T> rawType;
final Type type;
final AnnotatedType annotatedType;
final int hashCode;
@SuppressWarnings("unchecked")
protected TypeKey() {
this.type = getSuperclassTypeParameter(getClass());
this.annotatedType = getAnnotatedSuperclassTypeParameter(getClass());
this.rawType = (Class<? super T>) ReflectionUtil.getRawType(type);
this.hashCode = type.hashCode();
}
static Type getSuperclassTypeParameter(Class<?> subclass) {
private static Type getSuperclassTypeParameter(Class<?> subclass) {
Type superclass = subclass.getGenericSuperclass();
if(superclass instanceof Class) {
throw new RuntimeException("Missing type parameter.");
@@ -26,6 +29,15 @@ public class TypeKey<T> {
return parameterized.getActualTypeArguments()[0];
}
private static AnnotatedType getAnnotatedSuperclassTypeParameter(Class<?> subclass) {
AnnotatedType superclass = subclass.getAnnotatedSuperclass();
if(superclass.getType() instanceof Class) {
throw new RuntimeException("Missing type parameter.");
}
AnnotatedParameterizedType parameterized = (AnnotatedParameterizedType) superclass;
return parameterized.getAnnotatedActualTypeArguments()[0];
}
/**
* Returns the raw (non-generic) type for this type.
*/
@@ -40,6 +52,10 @@ public class TypeKey<T> {
return type;
}
public AnnotatedType getAnnotatedType() {
return annotatedType;
}
@Override
public final int hashCode() {
return this.hashCode;