(Class<T> type, Class<? extends T> clazz)
| 173 | |
| 174 | |
| 175 | private static <T> TypeResult getGenericType(Class<T> type, Class<? extends T> clazz) { |
| 176 | |
| 177 | // Look to see if this class implements the interface of interest |
| 178 | |
| 179 | // Get all the interfaces |
| 180 | Type[] interfaces = clazz.getGenericInterfaces(); |
| 181 | for (Type iface : interfaces) { |
| 182 | // Only need to check interfaces that use generics |
| 183 | if (iface instanceof ParameterizedType pi) { |
| 184 | // Look for the interface of interest |
| 185 | if (pi.getRawType() instanceof Class) { |
| 186 | if (type.isAssignableFrom((Class<?>) pi.getRawType())) { |
| 187 | return getTypeParameter(clazz, pi.getActualTypeArguments()[0]); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Interface not found on this class. Look at the superclass. |
| 194 | @SuppressWarnings("unchecked") |
| 195 | Class<? extends T> superClazz = (Class<? extends T>) clazz.getSuperclass(); |
| 196 | if (superClazz == null) { |
| 197 | // Finished looking up the class hierarchy without finding anything |
| 198 | throw new IllegalStateException(); |
| 199 | } |
| 200 | |
| 201 | TypeResult superClassTypeResult = getGenericType(type, superClazz); |
| 202 | int dimension = superClassTypeResult.getDimension(); |
| 203 | if (superClassTypeResult.getIndex() == -1 && dimension == 0) { |
| 204 | // Superclass implements interface and defines explicit type for |
| 205 | // the interface of interest |
| 206 | return superClassTypeResult; |
| 207 | } |
| 208 | |
| 209 | if (superClassTypeResult.getIndex() > -1) { |
| 210 | // Superclass implements interface and defines unknown type for |
| 211 | // the interface of interest |
| 212 | // Map that unknown type to the generic types defined in this class |
| 213 | ParameterizedType superClassType = (ParameterizedType) clazz.getGenericSuperclass(); |
| 214 | TypeResult result = |
| 215 | getTypeParameter(clazz, superClassType.getActualTypeArguments()[superClassTypeResult.getIndex()]); |
| 216 | result.incrementDimension(superClassTypeResult.getDimension()); |
| 217 | if (result.getClazz() != null && result.getDimension() > 0) { |
| 218 | superClassTypeResult = result; |
| 219 | } else { |
| 220 | return result; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (superClassTypeResult.getDimension() > 0) { |
| 225 | StringBuilder className = new StringBuilder(); |
| 226 | className.append("[".repeat(Math.max(0, dimension))); |
| 227 | className.append('L'); |
| 228 | className.append(superClassTypeResult.getClazz().getCanonicalName()); |
| 229 | className.append(';'); |
| 230 | |
| 231 | Class<?> arrayClazz; |
| 232 | try { |
no test coverage detected