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,19 @@ 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
383
- * type arguments are invalid for the raw type
382
+ * @throws IllegalArgumentException If the type arguments are invalid for the raw type
384
383
*/
385
- public static TypeToken <?> getParameterized (Type rawType , Type ... typeArguments ) {
384
+ public static TypeToken <?> getParameterized (Class <?> rawType , Type ... typeArguments ) {
386
385
Objects .requireNonNull (rawType );
387
386
Objects .requireNonNull (typeArguments );
388
387
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 ();
388
+ TypeVariable <?>[] typeVariables = rawType .getTypeParameters ();
397
389
398
390
int expectedArgsCount = typeVariables .length ;
399
391
int actualArgsCount = typeArguments .length ;
400
392
if (actualArgsCount != expectedArgsCount ) {
401
393
throw new IllegalArgumentException (
402
- rawClass .getName ()
394
+ rawType .getName ()
403
395
+ " requires "
404
396
+ expectedArgsCount
405
397
+ " type arguments, but got "
@@ -408,14 +400,14 @@ public static TypeToken<?> getParameterized(Type rawType, Type... typeArguments)
408
400
409
401
// For legacy reasons create a TypeToken(Class) if the type is not generic
410
402
if (typeArguments .length == 0 ) {
411
- return get (rawClass );
403
+ return get (( Class <?>) rawType );
412
404
}
413
405
414
406
// Check for this here to avoid misleading exception thrown by ParameterizedTypeImpl
415
407
if ($Gson$Types .requiresOwnerType (rawType )) {
416
408
throw new IllegalArgumentException (
417
409
"Raw type "
418
- + rawClass .getName ()
410
+ + (( Class <?>) rawType ) .getName ()
419
411
+ " is not supported because it requires specifying an owner type" );
420
412
}
421
413
0 commit comments