@@ -36,41 +36,41 @@ private Utils() {
36
36
// No instances.
37
37
}
38
38
39
- public static Class <?> getRawType (Type type ) {
39
+ static Class <?> getRawType (Type type ) {
40
+ if (type == null ) throw new NullPointerException ("type == null" );
41
+
40
42
if (type instanceof Class <?>) {
41
43
// Type is a normal class.
42
44
return (Class <?>) type ;
43
-
44
- } else if (type instanceof ParameterizedType ) {
45
+ }
46
+ if (type instanceof ParameterizedType ) {
45
47
ParameterizedType parameterizedType = (ParameterizedType ) type ;
46
48
47
49
// I'm not exactly sure why getRawType() returns Type instead of Class. Neal isn't either but
48
50
// suspects some pathological case related to nested classes exists.
49
51
Type rawType = parameterizedType .getRawType ();
50
52
if (!(rawType instanceof Class )) throw new IllegalArgumentException ();
51
53
return (Class <?>) rawType ;
52
-
53
- } else if (type instanceof GenericArrayType ) {
54
+ }
55
+ if (type instanceof GenericArrayType ) {
54
56
Type componentType = ((GenericArrayType ) type ).getGenericComponentType ();
55
57
return Array .newInstance (getRawType (componentType ), 0 ).getClass ();
56
-
57
- } else if (type instanceof TypeVariable ) {
58
+ }
59
+ if (type instanceof TypeVariable ) {
58
60
// We could use the variable's bounds, but that won't work if there are multiple. Having a raw
59
61
// type that's more general than necessary is okay.
60
62
return Object .class ;
61
-
62
- } else if (type instanceof WildcardType ) {
63
+ }
64
+ if (type instanceof WildcardType ) {
63
65
return getRawType (((WildcardType ) type ).getUpperBounds ()[0 ]);
64
-
65
- } else {
66
- String className = type == null ? "null" : type .getClass ().getName ();
67
- throw new IllegalArgumentException ("Expected a Class, ParameterizedType, or "
68
- + "GenericArrayType, but <" + type + "> is of type " + className );
69
66
}
67
+
68
+ throw new IllegalArgumentException ("Expected a Class, ParameterizedType, or "
69
+ + "GenericArrayType, but <" + type + "> is of type " + type .getClass ().getName ());
70
70
}
71
71
72
72
/** Returns true if {@code a} and {@code b} are equal. */
73
- public static boolean equals (Type a , Type b ) {
73
+ static boolean equals (Type a , Type b ) {
74
74
if (a == b ) {
75
75
return true ; // Also handles (a == null && b == null).
76
76
@@ -162,7 +162,7 @@ static int hashCodeOrZero(Object o) {
162
162
return o != null ? o .hashCode () : 0 ;
163
163
}
164
164
165
- public static String typeToString (Type type ) {
165
+ static String typeToString (Type type ) {
166
166
return type instanceof Class ? ((Class <?>) type ).getName () : type .toString ();
167
167
}
168
168
@@ -173,13 +173,13 @@ public static String typeToString(Type type) {
173
173
*
174
174
* @param supertype a superclass of, or interface implemented by, this.
175
175
*/
176
- public static Type getSupertype (Type context , Class <?> contextRawType , Class <?> supertype ) {
176
+ static Type getSupertype (Type context , Class <?> contextRawType , Class <?> supertype ) {
177
177
if (!supertype .isAssignableFrom (contextRawType )) throw new IllegalArgumentException ();
178
178
return resolve (context , contextRawType ,
179
179
getGenericSupertype (context , contextRawType , supertype ));
180
180
}
181
181
182
- public static Type resolve (Type context , Class <?> contextRawType , Type toResolve ) {
182
+ static Type resolve (Type context , Class <?> contextRawType , Type toResolve ) {
183
183
// This implementation is made a little more complicated in an attempt to avoid object-creation.
184
184
while (true ) {
185
185
if (toResolve instanceof TypeVariable ) {
@@ -318,9 +318,9 @@ static <T> void validateServiceInterface(Class<T> service) {
318
318
319
319
static Type getParameterUpperBound (int index , ParameterizedType type ) {
320
320
Type [] types = type .getActualTypeArguments ();
321
- if (types . length <= index ) {
321
+ if (index < 0 || index >= types . length ) {
322
322
throw new IllegalArgumentException (
323
- "Expected at least " + index + " type argument(s) but got: " + Arrays . toString ( types ) );
323
+ "Index " + index + " not in range [0, " + types . length + ") for " + type );
324
324
}
325
325
Type paramType = types [index ];
326
326
if (paramType instanceof WildcardType ) {
0 commit comments