fix failing unit test and better coverage
This commit is contained in:
		@@ -4,6 +4,7 @@ import java.lang.reflect.Field;
 | 
			
		||||
import java.lang.reflect.ParameterizedType;
 | 
			
		||||
import java.lang.reflect.Type;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class ReflectionUtil {
 | 
			
		||||
 | 
			
		||||
    public static Field getField(final Class<?> aClass, final String fieldName) {
 | 
			
		||||
@@ -45,8 +46,16 @@ public class ReflectionUtil {
 | 
			
		||||
     * @param <T>          the expected class of the generics parameter at position 'index' in 'rawInterface'
 | 
			
		||||
     * @return the actual generics parameter
 | 
			
		||||
     */
 | 
			
		||||
    @SuppressWarnings("unchecked")
 | 
			
		||||
    public static <T> Class<T> determineGenericInterfaceParameter(final Class<?> clazz, final Class<?> rawInterface, final int paramIndex) {
 | 
			
		||||
        final Class<T> found = determineGenericInterfaceParameterImpl(clazz, rawInterface, paramIndex);
 | 
			
		||||
        if (found == null) {
 | 
			
		||||
            throw new AssertionError(clazz.getSimpleName() + " expected to implement " + rawInterface.getSimpleName() + "<...>");
 | 
			
		||||
        }
 | 
			
		||||
        return found;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @SuppressWarnings("unchecked")
 | 
			
		||||
    private static <T> Class<T> determineGenericInterfaceParameterImpl(final Class<?> clazz, final Class<?> rawInterface, final int paramIndex) {
 | 
			
		||||
        for (Type genericInterface : clazz.getGenericInterfaces()) {
 | 
			
		||||
            if (genericInterface instanceof ParameterizedType) {
 | 
			
		||||
                final ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
 | 
			
		||||
@@ -55,16 +64,19 @@ public class ReflectionUtil {
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (clazz.getSuperclass() != Object.class) {
 | 
			
		||||
            return determineGenericInterfaceParameter(clazz.getSuperclass(), rawInterface, paramIndex);
 | 
			
		||||
        }
 | 
			
		||||
        for (Class<?> implementedInterface : clazz.getInterfaces()) {
 | 
			
		||||
            final Class<T> found = determineGenericInterfaceParameter(implementedInterface, rawInterface, paramIndex);
 | 
			
		||||
        if (clazz.getSuperclass() != null) {
 | 
			
		||||
            final Class<T> found = determineGenericInterfaceParameterImpl(clazz.getSuperclass(), rawInterface, paramIndex);
 | 
			
		||||
            if (found != null) {
 | 
			
		||||
                return found;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        throw new AssertionError(clazz.getSimpleName() + " expected to implement " + rawInterface.getSimpleName() + "<...>");
 | 
			
		||||
        for (Class<?> implementedInterface : clazz.getInterfaces()) {
 | 
			
		||||
            final Class<T> found = determineGenericInterfaceParameterImpl(implementedInterface, rawInterface, paramIndex);
 | 
			
		||||
            if (found != null) {
 | 
			
		||||
                return found;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -76,8 +88,16 @@ public class ReflectionUtil {
 | 
			
		||||
     * @param <T>        the expected class of the generics parameter at position 'index' in 'rawClass'
 | 
			
		||||
     * @return the actual generics parameter
 | 
			
		||||
     */
 | 
			
		||||
    @SuppressWarnings("unchecked")
 | 
			
		||||
    public static <T> Class<T> determineGenericClassParameter(final Class<?> clazz, final Class<?> rawClass, final int paramIndex) {
 | 
			
		||||
        final Class<T> found = determineGenericClassParameterImpl(clazz, rawClass, paramIndex);
 | 
			
		||||
        if (found == null) {
 | 
			
		||||
            throw new AssertionError(clazz.getSimpleName() + " expected to extend " + rawClass.getSimpleName() + "<...>");
 | 
			
		||||
        }
 | 
			
		||||
        return found;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @SuppressWarnings("unchecked")
 | 
			
		||||
    private static <T> Class<T> determineGenericClassParameterImpl(final Class<?> clazz, final Class<?> rawClass, final int paramIndex) {
 | 
			
		||||
        final Type genericClass = clazz.getGenericSuperclass();
 | 
			
		||||
        if (genericClass instanceof ParameterizedType) {
 | 
			
		||||
            final ParameterizedType parameterizedType = (ParameterizedType) genericClass;
 | 
			
		||||
@@ -88,7 +108,7 @@ public class ReflectionUtil {
 | 
			
		||||
        if (clazz.getSuperclass() != Object.class) {
 | 
			
		||||
            return determineGenericClassParameter(clazz.getSuperclass(), rawClass, paramIndex);
 | 
			
		||||
        }
 | 
			
		||||
        throw new AssertionError(clazz.getSimpleName() + " expected to extend " + rawClass.getSimpleName() + "<...>");
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @SuppressWarnings("unchecked")
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user