use of java.lang.reflect.GenericArrayType in project flink by apache.
the class Serializers method recursivelyRegisterGenericType.
private static void recursivelyRegisterGenericType(Type fieldType, ExecutionConfig config, Set<Class<?>> alreadySeen) {
if (fieldType instanceof ParameterizedType) {
// field has generics
ParameterizedType parameterizedFieldType = (ParameterizedType) fieldType;
for (Type t : parameterizedFieldType.getActualTypeArguments()) {
if (TypeExtractionUtils.isClassType(t)) {
recursivelyRegisterType(TypeExtractionUtils.typeToClass(t), config, alreadySeen);
}
}
recursivelyRegisterGenericType(parameterizedFieldType.getRawType(), config, alreadySeen);
} else if (fieldType instanceof GenericArrayType) {
GenericArrayType genericArrayType = (GenericArrayType) fieldType;
recursivelyRegisterGenericType(genericArrayType.getGenericComponentType(), config, alreadySeen);
} else if (fieldType instanceof Class) {
Class<?> clazz = (Class<?>) fieldType;
recursivelyRegisterType(clazz, config, alreadySeen);
}
}
use of java.lang.reflect.GenericArrayType in project rest.li by linkedin.
the class ReflectionUtils method getClass.
/**
* Get the underlying class for a type, or null if the type is a variable type.
*
* @param type the type
* @return the underlying class
*/
public static Class<?> getClass(final Type type) {
if (type instanceof Class) {
return (Class) type;
} else if (type instanceof ParameterizedType) {
return getClass(((ParameterizedType) type).getRawType());
} else if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
Class<?> componentClass = getClass(componentType);
if (componentClass != null) {
return Array.newInstance(componentClass, 0).getClass();
} else {
return null;
}
} else {
return null;
}
}
use of java.lang.reflect.GenericArrayType in project retrofit by square.
the class Utils method resolve.
static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
// This implementation is made a little more complicated in an attempt to avoid object-creation.
while (true) {
if (toResolve instanceof TypeVariable) {
TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
if (toResolve == typeVariable) {
return toResolve;
}
} else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) {
Class<?> original = (Class<?>) toResolve;
Type componentType = original.getComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
return componentType == newComponentType ? original : new GenericArrayTypeImpl(newComponentType);
} else if (toResolve instanceof GenericArrayType) {
GenericArrayType original = (GenericArrayType) toResolve;
Type componentType = original.getGenericComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
return componentType == newComponentType ? original : new GenericArrayTypeImpl(newComponentType);
} else if (toResolve instanceof ParameterizedType) {
ParameterizedType original = (ParameterizedType) toResolve;
Type ownerType = original.getOwnerType();
Type newOwnerType = resolve(context, contextRawType, ownerType);
boolean changed = newOwnerType != ownerType;
Type[] args = original.getActualTypeArguments();
for (int t = 0, length = args.length; t < length; t++) {
Type resolvedTypeArgument = resolve(context, contextRawType, args[t]);
if (resolvedTypeArgument != args[t]) {
if (!changed) {
args = args.clone();
changed = true;
}
args[t] = resolvedTypeArgument;
}
}
return changed ? new ParameterizedTypeImpl(newOwnerType, original.getRawType(), args) : original;
} else if (toResolve instanceof WildcardType) {
WildcardType original = (WildcardType) toResolve;
Type[] originalLowerBound = original.getLowerBounds();
Type[] originalUpperBound = original.getUpperBounds();
if (originalLowerBound.length == 1) {
Type lowerBound = resolve(context, contextRawType, originalLowerBound[0]);
if (lowerBound != originalLowerBound[0]) {
return new WildcardTypeImpl(new Type[] { Object.class }, new Type[] { lowerBound });
}
} else if (originalUpperBound.length == 1) {
Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
if (upperBound != originalUpperBound[0]) {
return new WildcardTypeImpl(new Type[] { upperBound }, EMPTY_TYPE_ARRAY);
}
}
return original;
} else {
return toResolve;
}
}
}
use of java.lang.reflect.GenericArrayType in project moshi by square.
the class Types method resolve.
static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
// This implementation is made a little more complicated in an attempt to avoid object-creation.
while (true) {
if (toResolve instanceof TypeVariable) {
TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
if (toResolve == typeVariable)
return toResolve;
} else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) {
Class<?> original = (Class<?>) toResolve;
Type componentType = original.getComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
return componentType == newComponentType ? original : arrayOf(newComponentType);
} else if (toResolve instanceof GenericArrayType) {
GenericArrayType original = (GenericArrayType) toResolve;
Type componentType = original.getGenericComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
return componentType == newComponentType ? original : arrayOf(newComponentType);
} else if (toResolve instanceof ParameterizedType) {
ParameterizedType original = (ParameterizedType) toResolve;
Type ownerType = original.getOwnerType();
Type newOwnerType = resolve(context, contextRawType, ownerType);
boolean changed = newOwnerType != ownerType;
Type[] args = original.getActualTypeArguments();
for (int t = 0, length = args.length; t < length; t++) {
Type resolvedTypeArgument = resolve(context, contextRawType, args[t]);
if (resolvedTypeArgument != args[t]) {
if (!changed) {
args = args.clone();
changed = true;
}
args[t] = resolvedTypeArgument;
}
}
return changed ? new ParameterizedTypeImpl(newOwnerType, original.getRawType(), args) : original;
} else if (toResolve instanceof WildcardType) {
WildcardType original = (WildcardType) toResolve;
Type[] originalLowerBound = original.getLowerBounds();
Type[] originalUpperBound = original.getUpperBounds();
if (originalLowerBound.length == 1) {
Type lowerBound = resolve(context, contextRawType, originalLowerBound[0]);
if (lowerBound != originalLowerBound[0]) {
return supertypeOf(lowerBound);
}
} else if (originalUpperBound.length == 1) {
Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
if (upperBound != originalUpperBound[0]) {
return subtypeOf(upperBound);
}
}
return original;
} else {
return toResolve;
}
}
}
use of java.lang.reflect.GenericArrayType in project gradle by gradle.
the class AsmBackedClassGeneratorTest method includesGenericTypeInformationForOverriddenConstructor.
@Test
public void includesGenericTypeInformationForOverriddenConstructor() throws Exception {
Class<?> generatedClass = generator.generate(BeanWithComplexConstructor.class);
Constructor<?> constructor = generatedClass.getDeclaredConstructors()[0];
assertThat(constructor.getTypeParameters().length, equalTo(3));
assertThat(constructor.getGenericParameterTypes().length, equalTo(12));
// Callable
Type paramType = constructor.getGenericParameterTypes()[0];
assertThat(paramType, equalTo((Type) Callable.class));
// Callable<String>
paramType = constructor.getGenericParameterTypes()[1];
assertThat(paramType, instanceOf(ParameterizedType.class));
ParameterizedType parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], equalTo((Type) String.class));
// Callable<? extends String>
paramType = constructor.getGenericParameterTypes()[2];
assertThat(paramType, instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
WildcardType wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
assertThat(wildcard.getUpperBounds().length, equalTo(1));
assertThat(wildcard.getUpperBounds()[0], equalTo((Type) String.class));
assertThat(wildcard.getLowerBounds().length, equalTo(0));
// Callable<? super String>
paramType = constructor.getGenericParameterTypes()[3];
assertThat(paramType, instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
assertThat(wildcard.getUpperBounds().length, equalTo(1));
assertThat(wildcard.getUpperBounds()[0], equalTo((Type) Object.class));
assertThat(wildcard.getLowerBounds().length, equalTo(1));
assertThat(wildcard.getLowerBounds()[0], equalTo((Type) String.class));
// Callable<?>
paramType = constructor.getGenericParameterTypes()[4];
assertThat(paramType, instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
assertThat(wildcard.getUpperBounds().length, equalTo(1));
assertThat(wildcard.getUpperBounds()[0], equalTo((Type) Object.class));
assertThat(wildcard.getLowerBounds().length, equalTo(0));
// Callable<? extends Callable<?>>
paramType = constructor.getGenericParameterTypes()[5];
assertThat(paramType, instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
assertThat(wildcard.getUpperBounds().length, equalTo(1));
assertThat(wildcard.getLowerBounds().length, equalTo(0));
assertThat(wildcard.getUpperBounds()[0], instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) wildcard.getUpperBounds()[0];
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
assertThat(wildcard.getUpperBounds().length, equalTo(1));
assertThat(wildcard.getUpperBounds()[0], equalTo((Type) Object.class));
assertThat(wildcard.getLowerBounds().length, equalTo(0));
// Callable<S>
paramType = constructor.getGenericParameterTypes()[6];
assertThat(paramType, instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(TypeVariable.class));
TypeVariable typeVariable = (TypeVariable) parameterizedType.getActualTypeArguments()[0];
assertThat(typeVariable.getName(), equalTo("S"));
assertThat(typeVariable.getBounds()[0], instanceOf(ParameterizedType.class));
// Callable<? extends T>
paramType = constructor.getGenericParameterTypes()[7];
assertThat(paramType, instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) paramType;
assertThat(parameterizedType.getRawType(), equalTo((Type) Callable.class));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
wildcard = (WildcardType) parameterizedType.getActualTypeArguments()[0];
assertThat(wildcard.getUpperBounds().length, equalTo(1));
assertThat(wildcard.getLowerBounds().length, equalTo(0));
assertThat(wildcard.getUpperBounds()[0], instanceOf(TypeVariable.class));
typeVariable = (TypeVariable) wildcard.getUpperBounds()[0];
assertThat(typeVariable.getName(), equalTo("T"));
assertThat(typeVariable.getBounds()[0], equalTo((Type) IOException.class));
// V
paramType = constructor.getGenericParameterTypes()[8];
assertThat(paramType, instanceOf(TypeVariable.class));
typeVariable = (TypeVariable) paramType;
assertThat(typeVariable.getName(), equalTo("V"));
assertThat(typeVariable.getBounds()[0], equalTo((Type) Object.class));
GenericArrayType arrayType;
// String[]
paramType = constructor.getGenericParameterTypes()[9];
assertThat(paramType, equalTo((Type) String[].class));
assertThat(((Class<?>) paramType).getComponentType(), equalTo((Type) String.class));
// List<? extends String>[]
paramType = constructor.getGenericParameterTypes()[10];
assertThat(paramType, instanceOf(GenericArrayType.class));
arrayType = (GenericArrayType) paramType;
assertThat(arrayType.getGenericComponentType(), instanceOf(ParameterizedType.class));
parameterizedType = (ParameterizedType) arrayType.getGenericComponentType();
assertThat(parameterizedType.getRawType(), equalTo((Type) List.class));
assertThat(parameterizedType.getActualTypeArguments().length, equalTo(1));
assertThat(parameterizedType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
// boolean
paramType = constructor.getGenericParameterTypes()[11];
assertThat(paramType, equalTo((Type) Boolean.TYPE));
assertThat(constructor.getGenericExceptionTypes().length, equalTo(2));
// throws Exception
Type exceptionType = constructor.getGenericExceptionTypes()[0];
assertThat(exceptionType, equalTo((Type) Exception.class));
// throws T
exceptionType = constructor.getGenericExceptionTypes()[1];
assertThat(exceptionType, instanceOf(TypeVariable.class));
typeVariable = (TypeVariable) exceptionType;
assertThat(typeVariable.getName(), equalTo("T"));
}
Aggregations