add TypeKey#getAnnotatedType

This commit is contained in:
dfsek committed 2021-07-21 09:48:21 -07:00
1 parent 6f03cfa600
commit 5c3cd73c03
1 file changed
+17 -1
@@ -7,17 +7,20 @@ import java.util.Objects;
public class TypeKey<T> { public class TypeKey<T> {
final Class<? super T> rawType; final Class<? super T> rawType;
final Type type; final Type type;
final AnnotatedType annotatedType;
final int hashCode; final int hashCode;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected TypeKey() { protected TypeKey() {
this.type = getSuperclassTypeParameter(getClass()); this.type = getSuperclassTypeParameter(getClass());
this.annotatedType = getAnnotatedSuperclassTypeParameter(getClass());
this.rawType = (Class<? super T>) ReflectionUtil.getRawType(type); this.rawType = (Class<? super T>) ReflectionUtil.getRawType(type);
this.hashCode = type.hashCode(); this.hashCode = type.hashCode();
} }
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) {
throw new RuntimeException("Missing type parameter."); throw new RuntimeException("Missing type parameter.");
@@ -26,6 +29,15 @@ public class TypeKey<T> {
return parameterized.getActualTypeArguments()[0]; 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. * Returns the raw (non-generic) type for this type.
*/ */
@@ -40,6 +52,10 @@ public class TypeKey<T> {
return type; return type;
} }
public AnnotatedType getAnnotatedType() {
return annotatedType;
}
@Override @Override
public final int hashCode() { public final int hashCode() {
return this.hashCode; return this.hashCode;