|
23 | 23 | import java.util.Arrays;
|
24 | 24 | import java.util.NoSuchElementException;
|
25 | 25 |
|
| 26 | +import static feign.Util.checkState; |
| 27 | + |
26 | 28 | /**
|
27 | 29 | * Static methods for working with types.
|
28 | 30 | *
|
@@ -325,6 +327,36 @@ public static Type resolveReturnType(Type baseType, Type overridingType) {
|
325 | 327 | return baseType;
|
326 | 328 | }
|
327 | 329 |
|
| 330 | + /** |
| 331 | + * Resolves the last type parameter of the parameterized {@code supertype}, based on the {@code |
| 332 | + * genericContext}, into its upper bounds. |
| 333 | + * <p/> |
| 334 | + * Implementation copied from {@code |
| 335 | + * retrofit.RestMethodInfo}. |
| 336 | + * |
| 337 | + * @param genericContext Ex. {@link java.lang.reflect.Field#getGenericType()} |
| 338 | + * @param supertype Ex. {@code Decoder.class} |
| 339 | + * @return in the example above, the type parameter of {@code Decoder}. |
| 340 | + * @throws IllegalStateException if {@code supertype} cannot be resolved into a parameterized type |
| 341 | + * using {@code context}. |
| 342 | + */ |
| 343 | + public static Type resolveLastTypeParameter(Type genericContext, Class<?> supertype) |
| 344 | + throws IllegalStateException { |
| 345 | + Type resolvedSuperType = |
| 346 | + Types.getSupertype(genericContext, Types.getRawType(genericContext), supertype); |
| 347 | + checkState(resolvedSuperType instanceof ParameterizedType, |
| 348 | + "could not resolve %s into a parameterized type %s", |
| 349 | + genericContext, supertype); |
| 350 | + Type[] types = ParameterizedType.class.cast(resolvedSuperType).getActualTypeArguments(); |
| 351 | + for (int i = 0; i < types.length; i++) { |
| 352 | + Type type = types[i]; |
| 353 | + if (type instanceof WildcardType) { |
| 354 | + types[i] = ((WildcardType) type).getUpperBounds()[0]; |
| 355 | + } |
| 356 | + } |
| 357 | + return types[types.length - 1]; |
| 358 | + } |
| 359 | + |
328 | 360 | static final class ParameterizedTypeImpl implements ParameterizedType {
|
329 | 361 |
|
330 | 362 | private final Type ownerType;
|
|
0 commit comments