@Test(expected=IllegalArgumentException.class) public void testNoGenericClass() { Class<?> cls = NoGenericClass.class; GenericType gt = new GenericType(cls); GenericType typeParam1 = gt.getTypeParameter(Collection.class.getTypeParameters()[0]); }
public GenericType getTypeParameter(TypeVariable<? extends Class<?>> variable) throws IllegalArgumentException, IllegalStateException { assertThatTypeVariableDoesParameterizeAClass(variable); GenericType result = null; if (type instanceof Class<?>) { Class<?> cls = (Class<?>) type; VariableMapping aMapping = resolveTypeVariableInClass(variable, cls, mapping); if (aMapping != null) { result = getGenericType(aMapping, variable); } } else if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; Class<?> rawType = (Class<?>) pType.getRawType(); VariableMapping aMapping = resolveTypeVariableInClass(variable, rawType, mapping); if (aMapping != null) { result = getGenericType(aMapping, variable); } } else { throw new UnsupportedOperationException(String.format("Type parameter resolution not supported for %s", type)); } if (result == null) { throw new IllegalArgumentException(String.format("%s does not parameterize this type %s!", variable, type)); } else { return result; } }
GenericType { public GenericType getTypeParameter(TypeVariable<? extends Class<?>> variable) throws IllegalArgumentException, IllegalStateException { assertThatTypeVariableDoesParameterizeAClass(variable); GenericType result = null; if (type instanceof Class<?>) { Class<?> cls = (Class<?>) type; VariableMapping aMapping = resolveTypeVariableInClass(variable, cls, mapping); if (aMapping != null) { result = getGenericType(aMapping, variable); } } else if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; Class<?> rawType = (Class<?>) pType.getRawType(); VariableMapping aMapping = resolveTypeVariableInClass(variable, rawType, mapping); if (aMapping != null) { result = getGenericType(aMapping, variable); } } else { throw new UnsupportedOperationException(String.format("Type parameter resolution not supported for %s", type)); } if (result == null) { throw new IllegalArgumentException(String.format("%s does not parameterize this type %s!", variable, type)); } else { return result; } } }
GenericType { public GenericType getTypeParameter(TypeVariable<? extends Class<?>> variable) throws IllegalArgumentException, IllegalStateException { assertThatTypeVariableDoesParameterizeAClass(variable); GenericType result = null; if (type instanceof Class<?>) { Class<?> cls = (Class<?>) type; VariableMapping aMapping = resolveTypeVariableInClass(variable, cls, mapping); if (aMapping != null) { result = getGenericType(aMapping, variable); } } else if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; Class<?> rawType = (Class<?>) pType.getRawType(); VariableMapping aMapping = resolveTypeVariableInClass(variable, rawType, mapping); if (aMapping != null) { result = getGenericType(aMapping, variable); } } else { throw new UnsupportedOperationException(String.format("Type parameter resolution not supported for %s", type)); } if (result == null) { throw new IllegalArgumentException(String.format("%s does not parameterize this type %s!", variable, type)); } else { return result; } } private  GenericType(VariableMapping aMapping, ParameterizedType aParameterizedType); private  GenericType(VariableMapping aMapping, Class aClass); private  GenericType(VariableMapping aMapping, TypeVariable<?> aTypeVariable);  GenericType(Class<?> cls); }
GenericType { public GenericType getTypeParameter(TypeVariable<? extends Class<?>> variable) throws IllegalArgumentException, IllegalStateException { assertThatTypeVariableDoesParameterizeAClass(variable); GenericType result = null; if (type instanceof Class<?>) { Class<?> cls = (Class<?>) type; VariableMapping aMapping = resolveTypeVariableInClass(variable, cls, mapping); if (aMapping != null) { result = getGenericType(aMapping, variable); } } else if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; Class<?> rawType = (Class<?>) pType.getRawType(); VariableMapping aMapping = resolveTypeVariableInClass(variable, rawType, mapping); if (aMapping != null) { result = getGenericType(aMapping, variable); } } else { throw new UnsupportedOperationException(String.format("Type parameter resolution not supported for %s", type)); } if (result == null) { throw new IllegalArgumentException(String.format("%s does not parameterize this type %s!", variable, type)); } else { return result; } } private  GenericType(VariableMapping aMapping, ParameterizedType aParameterizedType); private  GenericType(VariableMapping aMapping, Class aClass); private  GenericType(VariableMapping aMapping, TypeVariable<?> aTypeVariable);  GenericType(Class<?> cls); Type getType(); boolean isTypeVariable(); boolean isClass(); Class<?> asClass(); boolean isParameterizedType(); boolean isWildcardType(); GenericType getTypeParameter(TypeVariable<? extends Class<?>> variable); GenericType getFieldType(String fieldname); GenericType getMethodReturnType(String methodName); Type tryResolve(Type type); Type tryResolve(TypeVariable<?> var); Type narrow(Type aType, Class<?> targetType); }
GenericType { public GenericType getTypeParameter(TypeVariable<? extends Class<?>> variable) throws IllegalArgumentException, IllegalStateException { assertThatTypeVariableDoesParameterizeAClass(variable); GenericType result = null; if (type instanceof Class<?>) { Class<?> cls = (Class<?>) type; VariableMapping aMapping = resolveTypeVariableInClass(variable, cls, mapping); if (aMapping != null) { result = getGenericType(aMapping, variable); } } else if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; Class<?> rawType = (Class<?>) pType.getRawType(); VariableMapping aMapping = resolveTypeVariableInClass(variable, rawType, mapping); if (aMapping != null) { result = getGenericType(aMapping, variable); } } else { throw new UnsupportedOperationException(String.format("Type parameter resolution not supported for %s", type)); } if (result == null) { throw new IllegalArgumentException(String.format("%s does not parameterize this type %s!", variable, type)); } else { return result; } } private  GenericType(VariableMapping aMapping, ParameterizedType aParameterizedType); private  GenericType(VariableMapping aMapping, Class aClass); private  GenericType(VariableMapping aMapping, TypeVariable<?> aTypeVariable);  GenericType(Class<?> cls); Type getType(); boolean isTypeVariable(); boolean isClass(); Class<?> asClass(); boolean isParameterizedType(); boolean isWildcardType(); GenericType getTypeParameter(TypeVariable<? extends Class<?>> variable); GenericType getFieldType(String fieldname); GenericType getMethodReturnType(String methodName); Type tryResolve(Type type); Type tryResolve(TypeVariable<?> var); Type narrow(Type aType, Class<?> targetType);  }