/** * Gets the generic type of given matcher. For example, * for matcher class that extends BaseMatcher[Integer] this method returns Integer */ public static Class<?> genericTypeOfMatcher(Class<?> matcherClass) { //TODO SF check if we can reuse it for Mockito ArgumentMatcher return genericTypeOf(matcherClass, BaseMatcher.class, Matcher.class); } }
return extractGeneric(match.getGenericSuperclass()); Type genericInterface = findGenericInterface(match, targetBaseInterface); if (genericInterface != null) { return extractGeneric(genericInterface);
/** * Finds generic interface implementation based on the source class and the target interface. * Returns null if not found. Recurses the interface hierarchy. */ private static Type findGenericInterface(Class<?> sourceClass, Class<?> targetBaseInterface) { for (int i = 0; i < sourceClass.getInterfaces().length; i++) { Class<?> inter = sourceClass.getInterfaces()[i]; if (inter == targetBaseInterface) { return sourceClass.getGenericInterfaces()[0]; } else { Type deeper = findGenericInterface(inter, targetBaseInterface); if (deeper != null) { return deeper; } } } return null; }