|
7 | 7 | import java.lang.reflect.Method; |
8 | 8 | import java.lang.reflect.ParameterizedType; |
9 | 9 | import java.lang.reflect.Type; |
| 10 | +import java.lang.reflect.WildcardType; |
10 | 11 | import java.util.Arrays; |
11 | 12 | import java.util.List; |
12 | 13 |
|
@@ -85,10 +86,28 @@ private static Object resolveDependency(ResolutionContext context, Dependency de |
85 | 86 | } |
86 | 87 | if (List.class == parameterType) { |
87 | 88 | ParameterizedType parameterizedType = (ParameterizedType) genericType; |
88 | | - Class<?> listItemType = (Class<?>) parameterizedType.getActualTypeArguments()[0]; |
89 | | - return required |
90 | | - ? context.getAll(listItemType) |
91 | | - : context.getAllOrEmpty(listItemType); |
| 89 | + Type type = parameterizedType.getActualTypeArguments()[0]; |
| 90 | + if (type instanceof Class<?> listItemType) { |
| 91 | + return required |
| 92 | + ? context.getAll(listItemType) |
| 93 | + : context.getAllOrEmpty(listItemType); |
| 94 | + } |
| 95 | + if (type instanceof WildcardType wildcardItemType) { |
| 96 | + if (wildcardItemType.getUpperBounds() == null || wildcardItemType.getUpperBounds().length != 1) { |
| 97 | + throw new IllegalArgumentException("Invalid number of upper bound arguments"); |
| 98 | + } |
| 99 | + if (wildcardItemType.getLowerBounds() != null && wildcardItemType.getLowerBounds().length != 0) { |
| 100 | + throw new IllegalArgumentException("Unexpected lower bound type in list dependency"); |
| 101 | + } |
| 102 | + Type upperBound = wildcardItemType.getUpperBounds()[0]; |
| 103 | + if (upperBound instanceof Class<?> lowerBoundClass) { |
| 104 | + return required |
| 105 | + ? context.getAll(lowerBoundClass) |
| 106 | + : context.getAllOrEmpty(lowerBoundClass); |
| 107 | + } |
| 108 | + throw new IllegalArgumentException("Unexpected lower bound type: " + upperBound); |
| 109 | + } |
| 110 | + throw new ContextException("Invalid List generic type in dependency: " + name); |
92 | 111 | } |
93 | 112 | if (!name.isEmpty()) { |
94 | 113 | return required |
|
0 commit comments