/** * Searches for the typevariable definition in the inClass hierarchy. * * @param type * @param inClass * @return the resolved type or type if unable to resolve it. */ public final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type, Class<?> inClass) { return resolveTypeVariable(type, genericDeclarationToClass(type.getGenericDeclaration()), inClass); }
/** * Searches for the typevariable definition in the inClass hierarchy. * * @param type * @param inClass * @return the resolved type or type if unable to resolve it. */ public final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type, Class<?> inClass) { return resolveTypeVariable(type, genericDeclarationToClass(type.getGenericDeclaration()), inClass); }
for (int i = 0; i < interfaces.length && resolvedType == null; i++) { superClass = interfaces[i]; resolvedType = resolveTypeVariable(type, declaringClass, superClass); genericSuperClass = inClass.getGenericInterfaces()[i]; resolvedType = resolveTypeVariable(type, declaringClass, superClass); genericSuperClass = inClass.getGenericSuperclass();
final static Type expand(Type type, Class<?> inClass) { Type expandedType = null; if (type instanceof TypeVariable) { @SuppressWarnings("unchecked") // for the moment we assume it is a class, we can later handle ctr and methods TypeVariable<GenericDeclaration> tvType = (TypeVariable<GenericDeclaration>) type; if (inClass == null) inClass = genericDeclarationToClass(tvType.getGenericDeclaration()); expandedType = resolveTypeVariable(tvType, inClass); if (type.equals(expandedType)) expandedType = tvType.getBounds()[0]; } else if (type instanceof WildcardType) { WildcardType wType = (WildcardType) type; expandedType = wType.getUpperBounds().length > 0 ? expand(wType.getUpperBounds()[0], inClass) : Object.class; } else return type; return expandedType == null || type.equals(expandedType) ? Object.class : expandedType; }
for (int i = 0; i < interfaces.length && resolvedType == null; i++) { superClass = interfaces[i]; resolvedType = resolveTypeVariable(type, declaringClass, superClass); genericSuperClass = inClass.getGenericInterfaces()[i]; resolvedType = resolveTypeVariable(type, declaringClass, superClass); genericSuperClass = inClass.getGenericSuperclass();
final static Type expand(Type type, Class<?> inClass) { Type expandedType = null; if (type instanceof TypeVariable) { @SuppressWarnings("unchecked") // for the moment we assume it is a class, we can later handle ctr and methods TypeVariable<GenericDeclaration> tvType = (TypeVariable<GenericDeclaration>) type; if (inClass == null) inClass = genericDeclarationToClass(tvType.getGenericDeclaration()); expandedType = resolveTypeVariable(tvType, inClass); if (type.equals(expandedType)) expandedType = tvType.getBounds()[0]; } else if (type instanceof WildcardType) { WildcardType wType = (WildcardType) type; expandedType = wType.getUpperBounds().length > 0 ? expand(wType.getUpperBounds()[0], inClass) : Object.class; } else return type; return expandedType == null || type.equals(expandedType) ? Object.class : expandedType; }
private final static Type[] getTypes(Type type) { if (type instanceof Class) { Class<?> tClass = (Class<?>) type; if (tClass.isArray()) return new Type[]{tClass.getComponentType()}; else { TypeVariable<?>[] tvs = ((Class<?>) type).getTypeParameters(); Type[] types = new Type[tvs.length]; int i = 0; for (TypeVariable<?> tv : tvs) { types[i++] = tv.getBounds()[0]; } return types; } } else if (type instanceof ParameterizedType) { return ((ParameterizedType) type).getActualTypeArguments(); } else if (type instanceof GenericArrayType) { return new Type[]{((GenericArrayType) type).getGenericComponentType()}; } else if (type instanceof WildcardType) { return Operations.union(Type[].class, ((WildcardType) type).getUpperBounds(), ((WildcardType) type).getLowerBounds()); } else if (type instanceof TypeVariable<?>) { @SuppressWarnings("unchecked") TypeVariable<Class<?>> tvType = (TypeVariable<Class<?>>) type; Type resolvedType = resolveTypeVariable(tvType, tvType.getGenericDeclaration()); return tvType.equals(resolvedType) ? tvType.getBounds() : new Type[]{resolvedType}; } else return new Type[0]; }
private final static Type[] getTypes(Type type) { if (type instanceof Class) { Class<?> tClass = (Class<?>) type; if (tClass.isArray()) return new Type[]{tClass.getComponentType()}; else { TypeVariable<?>[] tvs = ((Class<?>) type).getTypeParameters(); Type[] types = new Type[tvs.length]; int i = 0; for (TypeVariable<?> tv : tvs) { types[i++] = tv.getBounds()[0]; } return types; } } else if (type instanceof ParameterizedType) { return ((ParameterizedType) type).getActualTypeArguments(); } else if (type instanceof GenericArrayType) { return new Type[]{((GenericArrayType) type).getGenericComponentType()}; } else if (type instanceof WildcardType) { return Operations.union(Type[].class, ((WildcardType) type).getUpperBounds(), ((WildcardType) type).getLowerBounds()); } else if (type instanceof TypeVariable<?>) { @SuppressWarnings("unchecked") TypeVariable<Class<?>> tvType = (TypeVariable<Class<?>>) type; Type resolvedType = resolveTypeVariable(tvType, tvType.getGenericDeclaration()); return tvType.equals(resolvedType) ? tvType.getBounds() : new Type[]{resolvedType}; } else return new Type[0]; }
expandedType = resolveTypeVariable(tvType, getRawClass(rootType));
expandedType = resolveTypeVariable(tvType, getRawClass(rootType));