use of java.lang.reflect.GenericArrayType in project spring-framework by spring-projects.
the class SerializableTypeWrapperTests method genericArrayType.
@Test
public void genericArrayType() throws Exception {
GenericArrayType type = (GenericArrayType) SerializableTypeWrapper.forField(Fields.class.getField("genericArrayType"));
assertThat(type.toString(), equalTo("java.util.List<java.lang.String>[]"));
assertSerializable(type);
assertSerializable(type.getGenericComponentType());
}
use of java.lang.reflect.GenericArrayType in project robovm by robovm.
the class GenericArrayTypeTest method testGetGenericComponentType.
public void testGetGenericComponentType() throws Exception {
@SuppressWarnings("unchecked") Class<? extends A> clazz = GenericArrayTypeTest.A.class;
Field field = clazz.getDeclaredField("array");
Type genericType = field.getGenericType();
assertInstanceOf(GenericArrayType.class, genericType);
Type componentType = ((GenericArrayType) genericType).getGenericComponentType();
assertEquals(getTypeParameter(clazz), componentType);
assertInstanceOf(TypeVariable.class, componentType);
TypeVariable<?> componentTypeVariable = (TypeVariable<?>) componentType;
assertEquals("T", componentTypeVariable.getName());
assertEquals(clazz, componentTypeVariable.getGenericDeclaration());
}
use of java.lang.reflect.GenericArrayType in project cdap by caskdata.
the class TypeResolver method resolveGenericArrayType.
private Type resolveGenericArrayType(GenericArrayType type) {
Type componentType = type.getGenericComponentType();
Type resolvedComponentType = resolveType(componentType);
return Types.newArrayType(resolvedComponentType);
}
use of java.lang.reflect.GenericArrayType in project cdap by caskdata.
the class PipelineTypeValidator method validateTypes.
/**
* Takes in an unresolved type list and resolves the types and verifies if the types are assignable.
* Ex: An unresolved type could be : String, T, List<T>, List<String>
* The above will resolve to : String, String, List<String>, List<String>
* And the assignability will be checked : String --> String && List<String> --> List<String>
* which is true in the case above.
*/
@VisibleForTesting
static void validateTypes(List<Type> unresTypeList) {
Preconditions.checkArgument(unresTypeList.size() % 2 == 0, "ETL Stages validation expects even number of types");
List<Type> resTypeList = Lists.newArrayListWithCapacity(unresTypeList.size());
// Add the source output to resolved type list as the first resolved type.
resTypeList.add(unresTypeList.get(0));
try {
// Resolve the second type using just the first resolved type.
Type nType = (new TypeResolver()).where(unresTypeList.get(1), resTypeList.get(0)).resolveType(unresTypeList.get(1));
resTypeList.add(nType);
} catch (IllegalArgumentException e) {
// If unable to resolve type, add the second type as is, to the resolved list.
resTypeList.add(unresTypeList.get(1));
}
for (int i = 2; i < unresTypeList.size(); i++) {
// ActualType is previous resolved type; FormalType is previous unresolved type;
// ToResolveType is current unresolved type;
// Ex: Actual = String; Formal = T; ToResolve = List<T>; ==> newType = List<String>
Type actualType = resTypeList.get(i - 1);
Type formalType = unresTypeList.get(i - 1);
Type toResolveType = unresTypeList.get(i);
try {
Type newType;
// newType should be List<String>. Hence resolve only from the previous resolved type (Actual)
if ((toResolveType instanceof TypeVariable) || (toResolveType instanceof GenericArrayType)) {
newType = (new TypeResolver()).where(toResolveType, actualType).resolveType(toResolveType);
} else {
newType = (new TypeResolver()).where(formalType, actualType).resolveType(toResolveType);
}
resTypeList.add(newType);
} catch (IllegalArgumentException e) {
// If resolution failed, add the type as is to the resolved list.
resTypeList.add(toResolveType);
}
}
// transform (which takes in type on its left and emits the type on its right).
for (int i = 0; i < resTypeList.size(); i += 2) {
Type firstType = resTypeList.get(i);
Type secondType = resTypeList.get(i + 1);
// Check if secondType can accept firstType
Preconditions.checkArgument(TypeToken.of(secondType).isAssignableFrom(firstType), "Types between stages didn't match. Mismatch between %s -> %s", firstType, secondType);
}
}
use of java.lang.reflect.GenericArrayType in project stanbol by apache.
the class JerseyUtils method testType.
// /**
// * Getter for a Service from the {@link ServletContext} by using the
// * {@link Class#getName()} as key for {@link ServletContext#getAttribute(String)}.
// * In case the Service can not be found a {@link WebApplicationException} is
// * thrown with the message that the Service is currently not available.
// * @param <T> The type of the Service
// * @param service the Service interface
// * @param context the context used to search the service
// * @return the Service instance
// * @throws WebApplicationException in case the service instance was not found
// * in the parsed servlet context
// * @throws IllegalArgumentException if <code>null</code> is parsed as
// * service or context
// */
// @SuppressWarnings("unchecked")
// public static <T> T getService(Class<T> service, ServletContext context) throws WebApplicationException, IllegalArgumentException {
// if(service == null){
// throw new IllegalArgumentException("The parsed ServiceInterface MUST NOT be NULL!");
// }
// if(context == null){
// throw new IllegalArgumentException("The parsed ServletContext MUST NOT be NULL");
// }
// T serviceInstance = (T) context.getAttribute(service.getName());
// if(serviceInstance == null){
// throw new WebApplicationException(new IllegalStateException(
// "The "+service.getSimpleName()+" Service is currently not available " +
// "(full name= "+service+"| " +
// "servlet context name = "+context.getServletContextName()+")"),
// Response.Status.INTERNAL_SERVER_ERROR);
// }
// return serviceInstance;
// }
/**
* Tests if a generic type (may be <?>, <? extends {required}>
* or <? super {required}>) is compatible with the required one.
* TODO: Should be moved to an utility class
* @param required the required class the generic type MUST BE compatible with
* @param genericType the required class
* @return if the generic type is compatible with the required class
*/
public static boolean testType(Class<?> required, Type type) {
//for the examples let assume that a Set is the raw type and the
//requested generic type is a Representation with the following class
//hierarchy:
// Object
// -> Representation
// -> RdfRepresentation
// -> InMemoryRepresentation
// -> InputStream
// -> Collection<T>
boolean typeOK = false;
// types.add(type);
if (type instanceof Class<?>) {
typeOK = required.isAssignableFrom((Class<?>) type);
type = ((Class<?>) type).getGenericSuperclass();
} else if (type instanceof WildcardType) {
//In cases <? super {class}>, <? extends {class}, <?>
WildcardType wildcardSetType = (WildcardType) type;
if (wildcardSetType.getLowerBounds().length > 0) {
Type lowerBound = wildcardSetType.getLowerBounds()[0];
//OK
// Set<? super RdfRepresentation>
// Set<? super Representation>
//NOT OK
// Set<? super InputStream>
// Set<? super Collection<Representation>>
typeOK = lowerBound instanceof Class<?> && required.isAssignableFrom((Class<?>) lowerBound);
} else if (wildcardSetType.getUpperBounds().length > 0) {
Type upperBound = wildcardSetType.getUpperBounds()[0];
//OK
// Set<? extends Representation>
// Set<? extends Object>
//NOT OK
// Set<? extends RdfRepresentation>
// Set<? extends InputStream>
// Set<? extends Collection<Representation>
typeOK = upperBound instanceof Class<?> && ((Class<?>) upperBound).isAssignableFrom(required);
} else {
//no upper nor lower bound
// Set<?>
typeOK = true;
}
} else if (required.isArray() && type instanceof GenericArrayType) {
//In case the required type is an array we need also to support
//possible generic Array specifications
GenericArrayType arrayType = (GenericArrayType) type;
typeOK = testType(required.getComponentType(), arrayType.getGenericComponentType());
} else if (type instanceof ParameterizedType) {
ParameterizedType pType = ((ParameterizedType) type);
typeOK = pType.getRawType() instanceof Class<?> && required.isAssignableFrom((Class<?>) pType.getRawType());
type = null;
} else {
//GenericArrayType but !required.isArray() -> incompatible
//TypeVariable -> no variables define -> incompatible
typeOK = false;
// type = null; //end
}
// }
return typeOK;
}
Aggregations