@@ -129,9 +129,10 @@ private static Type resolveGenericArrayType(GenericArrayType genericArrayType, T
129
129
private static ParameterizedType resolveParameterizedType (ParameterizedType parameterizedType , Type srcType ,
130
130
Class <?> declaringClass ) {
131
131
Class <?> rawType = (Class <?>) parameterizedType .getRawType ();
132
+ Type ownerType = parameterizedType .getOwnerType ();
132
133
Type [] typeArgs = parameterizedType .getActualTypeArguments ();
133
134
Type [] args = resolveTypes (typeArgs , srcType , declaringClass );
134
- return new ParameterizedTypeImpl (rawType , null , args );
135
+ return new ParameterizedTypeImpl (rawType , ownerType , args );
135
136
}
136
137
137
138
private static Type resolveWildcardType (WildcardType wildcardType , Type srcType , Class <?> declaringClass ) {
@@ -158,7 +159,7 @@ private static Type resolveTypeVar(TypeVariable<?> typeVar, Type srcType, Class<
158
159
}
159
160
} else {
160
161
throw new IllegalArgumentException (
161
- "The 2nd arg must be Class or ParameterizedType, but was: " + srcType .getClass ());
162
+ "The srcType( 2nd arg) must be Class or ParameterizedType, but was: " + srcType .getClass ());
162
163
}
163
164
164
165
if (clazz == declaringClass ) {
@@ -198,7 +199,7 @@ private static Type scanSuperTypes(TypeVariable<?> typeVar, Type srcType, Class<
198
199
for (int i = 0 ; i < parentTypeVars .length ; i ++) {
199
200
if (typeVar .equals (parentTypeVars [i ])) {
200
201
Type actualType = parentAsType .getActualTypeArguments ()[i ];
201
- return actualType instanceof TypeVariable <?> ? Object . class : actualType ;
202
+ return actualType instanceof TypeVariable <?> ? (( TypeVariable <?>) actualType ). getBounds ()[ 0 ] : actualType ;
202
203
}
203
204
}
204
205
}
@@ -230,7 +231,40 @@ private static ParameterizedType translateParentTypeVars(ParameterizedType srcTy
230
231
newParentArgs [i ] = parentTypeArgs [i ];
231
232
}
232
233
}
233
- return noChange ? parentType : new ParameterizedTypeImpl ((Class <?>) parentType .getRawType (), null , newParentArgs );
234
+ if (noChange && !(parentType instanceof ParameterizedTypeImpl )) noChange = false ;
235
+ return noChange
236
+ ? parentType
237
+ : new ParameterizedTypeImpl ((Class <?>) parentType .getRawType (), parentType .getOwnerType (), newParentArgs );
238
+ }
239
+
240
+ private static Type canonicalize (Type type ) {
241
+ if (type instanceof ParameterizedType ) {
242
+ ParameterizedType p = (ParameterizedType ) type ;
243
+ return new ParameterizedTypeImpl ((Class <?>) p .getRawType (), p .getOwnerType (), p .getActualTypeArguments ());
244
+ }
245
+ else if (type instanceof GenericArrayType ) {
246
+ GenericArrayType g = (GenericArrayType ) type ;
247
+ return new GenericArrayTypeImpl (g .getGenericComponentType ());
248
+ }
249
+ else if (type instanceof WildcardType ) {
250
+ WildcardType w = (WildcardType ) type ;
251
+ return new WildcardTypeImpl (w .getLowerBounds (), w .getUpperBounds ());
252
+ }
253
+ else {
254
+ return type ;
255
+ }
256
+ }
257
+
258
+ private static Type [] canonicalizeTypes (Type [] types ) {
259
+ if (types == null || types .length == 0 ) {
260
+ return new Type [0 ];
261
+ }
262
+ int length = types .length ;
263
+ Type [] canonicalizedTypes = new Type [length ];
264
+ for (int i = 0 ; i < length ; i ++) {
265
+ canonicalizedTypes [i ] = canonicalize (types [i ]);
266
+ }
267
+ return canonicalizedTypes ;
234
268
}
235
269
236
270
private TypeParameterResolver () {
@@ -244,11 +278,11 @@ static class ParameterizedTypeImpl implements ParameterizedType {
244
278
245
279
private final Type [] actualTypeArguments ;
246
280
247
- public ParameterizedTypeImpl (Class <?> rawType , Type ownerType , Type [] actualTypeArguments ) {
281
+ ParameterizedTypeImpl (Class <?> rawType , Type ownerType , Type [] actualTypeArguments ) {
248
282
super ();
249
- this .rawType = rawType ;
250
- this .ownerType = ownerType ;
251
- this .actualTypeArguments = actualTypeArguments ;
283
+ this .rawType = ( Class <?>) canonicalize ( rawType ) ;
284
+ this .ownerType = canonicalize ( ownerType ) ;
285
+ this .actualTypeArguments = canonicalizeTypes ( actualTypeArguments ) ;
252
286
}
253
287
254
288
@ Override
@@ -283,7 +317,21 @@ public boolean equals(Object obj) {
283
317
284
318
@ Override
285
319
public String toString () {
286
- StringBuilder s = new StringBuilder ().append (rawType .getName ()).append ("<" );
320
+ StringBuilder s = new StringBuilder ();
321
+ if (ownerType != null ) {
322
+ s .append (ownerType .getTypeName ()).append ("$" );
323
+ if (ownerType instanceof ParameterizedTypeImpl ) {
324
+ // remove prefixes that do not contain generic information
325
+ s .append (
326
+ rawType .getName ().replace (((ParameterizedTypeImpl ) ownerType ).rawType .getName () + "$" , "" ));
327
+ }
328
+ else {
329
+ s .append (rawType .getSimpleName ());
330
+ }
331
+ } else {
332
+ s .append (rawType .getName ());
333
+ }
334
+ s .append ("<" );
287
335
for (int i = 0 ; i < actualTypeArguments .length ; i ++) {
288
336
if (i > 0 ) {
289
337
s .append (", " );
@@ -301,8 +349,8 @@ static class WildcardTypeImpl implements WildcardType {
301
349
302
350
WildcardTypeImpl (Type [] lowerBounds , Type [] upperBounds ) {
303
351
super ();
304
- this .lowerBounds = lowerBounds ;
305
- this .upperBounds = upperBounds ;
352
+ this .lowerBounds = canonicalizeTypes ( lowerBounds ) ;
353
+ this .upperBounds = canonicalizeTypes ( upperBounds ) ;
306
354
}
307
355
308
356
@ Override
@@ -353,7 +401,7 @@ static class GenericArrayTypeImpl implements GenericArrayType {
353
401
354
402
GenericArrayTypeImpl (Type genericComponentType ) {
355
403
super ();
356
- this .genericComponentType = genericComponentType ;
404
+ this .genericComponentType = canonicalize ( genericComponentType ) ;
357
405
}
358
406
359
407
@ Override
@@ -380,7 +428,7 @@ public boolean equals(Object obj) {
380
428
381
429
@ Override
382
430
public String toString () {
383
- return new StringBuilder ().append (genericComponentType .toString ()).append ("[]" ).toString ();
431
+ return new StringBuilder ().append (genericComponentType .getTypeName ()).append ("[]" ).toString ();
384
432
}
385
433
}
386
434
}
0 commit comments