45
45
*
46
46
* <p>If the type arguments of the parameterized type are only available at runtime, for example
47
47
* when you want to create a {@code List<E>} based on a {@code Class<E>} representing the element
48
- * type, the method {@link #getParameterized(Type , Type...)} can be used.
48
+ * type, the method {@link #getParameterized(Class , Type...)} can be used.
49
49
*
50
50
* @author Bob Lee
51
51
* @author Sven Mawson
@@ -379,27 +379,20 @@ public static <T> TypeToken<T> get(Class<T> type) {
379
379
* <p>If {@code rawType} is a non-generic class and no type arguments are provided, this method
380
380
* simply delegates to {@link #get(Class)} and creates a {@code TypeToken(Class)}.
381
381
*
382
- * @throws IllegalArgumentException If {@code rawType} is not of type {@code Class}, or if the
382
+ * @throws IllegalArgumentException If the
383
383
* type arguments are invalid for the raw type
384
384
*/
385
- public static TypeToken <?> getParameterized (Type rawType , Type ... typeArguments ) {
385
+ public static TypeToken <?> getParameterized (Class <?> rawType , Type ... typeArguments ) {
386
386
Objects .requireNonNull (rawType );
387
387
Objects .requireNonNull (typeArguments );
388
388
389
- // Perform basic validation here because this is the only public API where users
390
- // can create malformed parameterized types
391
- if (!(rawType instanceof Class )) {
392
- // See also https://bugs.openjdk.org/browse/JDK-8250659
393
- throw new IllegalArgumentException ("rawType must be of type Class, but was " + rawType );
394
- }
395
- Class <?> rawClass = (Class <?>) rawType ;
396
- TypeVariable <?>[] typeVariables = rawClass .getTypeParameters ();
389
+ TypeVariable <?>[] typeVariables = rawType .getTypeParameters ();
397
390
398
391
int expectedArgsCount = typeVariables .length ;
399
392
int actualArgsCount = typeArguments .length ;
400
393
if (actualArgsCount != expectedArgsCount ) {
401
394
throw new IllegalArgumentException (
402
- rawClass .getName ()
395
+ rawType .getName ()
403
396
+ " requires "
404
397
+ expectedArgsCount
405
398
+ " type arguments, but got "
@@ -408,14 +401,14 @@ public static TypeToken<?> getParameterized(Type rawType, Type... typeArguments)
408
401
409
402
// For legacy reasons create a TypeToken(Class) if the type is not generic
410
403
if (typeArguments .length == 0 ) {
411
- return get (rawClass );
404
+ return get (( Class <?>) rawType );
412
405
}
413
406
414
407
// Check for this here to avoid misleading exception thrown by ParameterizedTypeImpl
415
408
if ($Gson$Types .requiresOwnerType (rawType )) {
416
409
throw new IllegalArgumentException (
417
410
"Raw type "
418
- + rawClass .getName ()
411
+ + (( Class <?>) rawType ) .getName ()
419
412
+ " is not supported because it requires specifying an owner type" );
420
413
}
421
414
0 commit comments