use of java.lang.reflect.TypeVariable in project aries by apache.
the class GenericType method parametersOf.
static GenericType[] parametersOf(Type type) {
if (type instanceof Class) {
Class clazz = (Class) type;
if (clazz.isArray()) {
GenericType t = new GenericType(clazz.getComponentType());
if (t.size() > 0) {
return new GenericType[] { t };
} else {
return EMPTY;
}
} else {
return EMPTY;
}
}
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
Type[] parameters = pt.getActualTypeArguments();
GenericType[] gts = new GenericType[parameters.length];
for (int i = 0; i < gts.length; i++) {
gts[i] = new GenericType(parameters[i]);
}
return gts;
}
if (type instanceof GenericArrayType) {
return new GenericType[] { new GenericType(((GenericArrayType) type).getGenericComponentType()) };
}
if (type instanceof WildcardType) {
return EMPTY;
}
if (type instanceof TypeVariable) {
return EMPTY;
}
throw new IllegalStateException();
}
use of java.lang.reflect.TypeVariable in project beam by apache.
the class CoderRegistry method verifyCompatible.
/**
* Returns {@code true} if the given {@link Coder} can possibly encode elements
* of the given type.
*/
@VisibleForTesting
static <T, CoderT extends Coder<T>, CandidateT> void verifyCompatible(CoderT coder, Type candidateType) throws IncompatibleCoderException {
// Various representations of the coder's class
@SuppressWarnings("unchecked") Class<CoderT> coderClass = (Class<CoderT>) coder.getClass();
TypeDescriptor<CoderT> coderDescriptor = TypeDescriptor.of(coderClass);
// Various representations of the actual coded type
@SuppressWarnings("unchecked") TypeDescriptor<T> codedDescriptor = CoderUtils.getCodedType(coderDescriptor);
@SuppressWarnings("unchecked") Class<T> codedClass = (Class<T>) codedDescriptor.getRawType();
Type codedType = codedDescriptor.getType();
// Various representations of the candidate type
@SuppressWarnings("unchecked") TypeDescriptor<CandidateT> candidateDescriptor = (TypeDescriptor<CandidateT>) TypeDescriptor.of(candidateType);
@SuppressWarnings("unchecked") Class<CandidateT> candidateClass = (Class<CandidateT>) candidateDescriptor.getRawType();
// to erasure, then we cannot rule it out.
if (candidateType instanceof TypeVariable) {
return;
}
// coder compatibility
if (!codedClass.isAssignableFrom(candidateClass)) {
throw new IncompatibleCoderException(String.format("Cannot encode elements of type %s with coder %s because the" + " coded type %s is not assignable from %s", candidateType, coder, codedClass, candidateType), coder, candidateType);
}
// we have established that this is a covariant upcast... though
// coders are invariant, we are just checking one direction
@SuppressWarnings("unchecked") TypeDescriptor<T> candidateOkDescriptor = (TypeDescriptor<T>) candidateDescriptor;
// compatible.
if ((codedType instanceof ParameterizedType) && !isNullOrEmpty(coder.getCoderArguments())) {
ParameterizedType parameterizedSupertype = ((ParameterizedType) candidateOkDescriptor.getSupertype(codedClass).getType());
Type[] typeArguments = parameterizedSupertype.getActualTypeArguments();
List<? extends Coder<?>> typeArgumentCoders = coder.getCoderArguments();
if (typeArguments.length < typeArgumentCoders.size()) {
throw new IncompatibleCoderException(String.format("Cannot encode elements of type %s with coder %s:" + " the generic supertype %s has %s type parameters, which is less than the" + " number of coder arguments %s has (%s).", candidateOkDescriptor, coder, parameterizedSupertype, typeArguments.length, coder, typeArgumentCoders.size()), coder, candidateOkDescriptor.getType());
}
for (int i = 0; i < typeArgumentCoders.size(); i++) {
try {
verifyCompatible(typeArgumentCoders.get(i), candidateDescriptor.resolveType(typeArguments[i]).getType());
} catch (IncompatibleCoderException exn) {
throw new IncompatibleCoderException(String.format("Cannot encode elements of type %s with coder %s" + " because some component coder is incompatible", candidateType, coder), coder, candidateType, exn);
}
}
}
}
use of java.lang.reflect.TypeVariable in project deltaspike by apache.
the class HierarchyDiscovery method resolveType.
private Type resolveType(Class<?> clazz) {
if (clazz.getTypeParameters().length > 0) {
TypeVariable<?>[] actualTypeParameters = clazz.getTypeParameters();
@SuppressWarnings("UnnecessaryLocalVariable") ParameterizedType parameterizedType = new ParameterizedTypeImpl(clazz, actualTypeParameters, clazz.getDeclaringClass());
return parameterizedType;
} else {
return clazz;
}
}
use of java.lang.reflect.TypeVariable in project deltaspike by apache.
the class HierarchyDiscovery method resolveTypeParameter.
private Type resolveTypeParameter(ParameterizedType type, Type beanType, TypeVariable<?> typeVariable) {
// step1. raw type
Class<?> actualType = (Class<?>) type.getRawType();
TypeVariable<?>[] typeVariables = actualType.getTypeParameters();
Type[] actualTypes = type.getActualTypeArguments();
for (int i = 0; i < typeVariables.length; i++) {
if (typeVariables[i].equals(typeVariable) && !actualTypes[i].equals(typeVariable)) {
return resolveType(this.type, beanType, actualTypes[i]);
}
}
// step2. generic super class
Type genericSuperType = actualType.getGenericSuperclass();
Type resolvedGenericSuperType = resolveType(genericSuperType, beanType, typeVariable);
if (!(resolvedGenericSuperType instanceof TypeVariable<?>)) {
return resolvedGenericSuperType;
}
// step3. generic interfaces
if (beanType instanceof ParameterizedType) {
for (Type interfaceType : ((Class<?>) ((ParameterizedType) beanType).getRawType()).getGenericInterfaces()) {
Type resolvedType = resolveType(interfaceType, interfaceType, typeVariable);
if (!(resolvedType instanceof TypeVariable<?>)) {
return resolvedType;
}
}
}
// don't resolve type variable
return typeVariable;
}
use of java.lang.reflect.TypeVariable in project spock by spockframework.
the class GenericTypeReflector method getExactDirectSuperTypes.
/**
* Returns the direct supertypes of the given type. Resolves type parameters.
*/
private static Type[] getExactDirectSuperTypes(Type type) {
if (type instanceof ParameterizedType || type instanceof Class) {
Class<?> clazz;
if (type instanceof ParameterizedType) {
clazz = (Class<?>) ((ParameterizedType) type).getRawType();
} else {
clazz = (Class<?>) type;
if (clazz.isArray())
return getArrayExactDirectSuperTypes(clazz);
}
Type[] superInterfaces = clazz.getGenericInterfaces();
Type superClass = clazz.getGenericSuperclass();
Type[] result;
int resultIndex;
if (superClass == null) {
result = new Type[superInterfaces.length];
resultIndex = 0;
} else {
result = new Type[superInterfaces.length + 1];
resultIndex = 1;
result[0] = mapTypeParameters(superClass, type);
}
for (Type superInterface : superInterfaces) {
result[resultIndex++] = mapTypeParameters(superInterface, type);
}
return result;
} else if (type instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) type;
return tv.getBounds();
} else if (type instanceof WildcardType) {
// But it does happen if the upper bound of a type variable contains a wildcard
return ((WildcardType) type).getUpperBounds();
} else if (type instanceof CaptureType) {
return ((CaptureType) type).getUpperBounds();
} else if (type instanceof GenericArrayType) {
return getArrayExactDirectSuperTypes(type);
} else {
throw new RuntimeException("not implemented type: " + type);
}
}
Aggregations