Returns the type token representing the generic type declaration of cls. For example: TypeToken.getGenericType(Iterable.class) returns Iterable . If cls isn't parameterized and isn't a generic array, the type token of the class is returned.
(Class<T> cls)
| 1085 | */ |
| 1086 | |
| 1087 | @VisibleForTesting |
| 1088 | static <T> TypeToken<? extends T> toGenericType(Class<T> cls) { |
| 1089 | if (cls.isArray()) { |
| 1090 | Type arrayOfGenericType = Types.newArrayType( |
| 1091 | // If we are passed with int[].class, don't turn it to GenericArrayType |
| 1092 | toGenericType(cls.getComponentType()).runtimeType); |
| 1093 | @SuppressWarnings("unchecked") // array is covariant |
| 1094 | TypeToken<? extends T> result = (TypeToken<? extends T>) of(arrayOfGenericType); |
| 1095 | return result; |
| 1096 | } |
| 1097 | TypeVariable<Class<T>>[] typeParams = cls.getTypeParameters(); |
| 1098 | Type ownerType = cls.isMemberClass() && !Modifier.isStatic(cls.getModifiers()) |
| 1099 | ? toGenericType(cls.getEnclosingClass()).runtimeType |
| 1100 | : null; |
| 1101 | if ((typeParams.length > 0) |
| 1102 | || ((ownerType != null) && ownerType != cls.getEnclosingClass())) { |
| 1103 | @SuppressWarnings("unchecked") // Like, it's Iterable<T> for Iterable.class |
| 1104 | TypeToken<? extends T> type = (TypeToken<? extends T>) of(Types.newParameterizedTypeWithOwner(ownerType, cls, typeParams)); |
| 1105 | return type; |
| 1106 | } else { |
| 1107 | return of(cls); |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | private TypeToken<? super T> getSupertypeFromUpperBounds(Class<? super T> supertype, Type[] upperBounds) { |
no test coverage detected