List of usage examples for java.lang.reflect WildcardType getUpperBounds
Type[] getUpperBounds();
From source file:org.openflexo.antar.binding.TypeUtils.java
/** * Return flag indicating if this type is considered as generic A generic type is a type that is parameterized with type variable(s). If * this type is resolved but contains a type in it definition containing itself a generic definition, then this type is also generic * (this 'isGeneric' property is recursively transmissible). * // w ww. j a v a2 s . c o m * @return a flag indicating whether this type is resolved or not */ public static boolean isGeneric(Type type) { if (type instanceof CustomType) { return false; } if (type instanceof Class) { return false; } if (type instanceof GenericArrayType) { return isGeneric(((GenericArrayType) type).getGenericComponentType()); } if (type instanceof ParameterizedType) { for (Type t : ((ParameterizedType) type).getActualTypeArguments()) { if (isGeneric(t)) { return true; } } return false; } if (type instanceof TypeVariable) { return true; } if (type instanceof WildcardType) { WildcardType w = (WildcardType) type; if (w.getUpperBounds() != null && w.getUpperBounds().length > 0) { for (Type b : w.getUpperBounds()) { if (isGeneric(b)) { return true; } } } if (w.getLowerBounds() != null && w.getLowerBounds().length > 0) { for (Type b : w.getLowerBounds()) { if (isGeneric(b)) { return true; } } } return false; } logger.warning("Unexpected " + type); return false; }
From source file:org.openflexo.antar.binding.TypeUtils.java
/** * Build instanciated DMType considering supplied type is generic (contains TypeVariable definitions) Returns a clone of DMType where * all references to TypeVariable are replaced by values defined in context type. For example, given type=Enumeration<E> and * context=Vector<String>, returns Enumeration<String> If supplied type is not generic, return type value (without cloning!) * //from w w w . ja va 2 s.c o m * @param type * : type to instanciate * @param context * : context used to instanciate type * @return */ public static Type makeInstantiatedType(Type type, Type context) { if (type == null) { return null; } if (!isGeneric(type)) { return type; } if (type instanceof ParameterizedType) { Type[] actualTypeArguments = new Type[((ParameterizedType) type).getActualTypeArguments().length]; for (int i = 0; i < ((ParameterizedType) type).getActualTypeArguments().length; i++) { actualTypeArguments[i] = makeInstantiatedType( ((ParameterizedType) type).getActualTypeArguments()[i], context); } return new ParameterizedTypeImpl((Class) ((ParameterizedType) type).getRawType(), actualTypeArguments); } if (type instanceof GenericArrayType) { return new GenericArrayTypeImpl( makeInstantiatedType(((GenericArrayType) type).getGenericComponentType(), context)); } if (type instanceof TypeVariable) { TypeVariable<GenericDeclaration> tv = (TypeVariable<GenericDeclaration>) type; GenericDeclaration gd = tv.getGenericDeclaration(); // System.out.println("Found type variable "+tv+" name="+tv.getName()+" GD="+tv.getGenericDeclaration()); if (gd instanceof Class) { if (context instanceof ParameterizedType) { for (int i = 0; i < gd.getTypeParameters().length; i++) { if (gd.getTypeParameters()[i].equals(tv)) { // Found matching parameterized type if (i < ((ParameterizedType) context).getActualTypeArguments().length) { // logger.info("********* return instantiatedType for "+type+" context="+context+" gd="+gd); if (!((ParameterizedType) context).getRawType().equals(gd)) { return makeInstantiatedType(type, getSuperType(context)); } return ((ParameterizedType) context).getActualTypeArguments()[i]; } else { logger.warning("Could not retrieve parameterized type " + tv + " with context " + simpleRepresentation(context)); return type; } } } } else if (context instanceof Class && ((Class) context).getGenericSuperclass() != null) { return makeInstantiatedType(type, ((Class) context).getGenericSuperclass()); } } else if (gd instanceof Method) { return type; } logger.warning("Not found type variable " + tv + " in context " + context + " GenericDeclaration=" + tv.getGenericDeclaration()); // throw new InvalidKeyValuePropertyException("Not found type variable "+tv+" in context "+context); return type; } if (type instanceof WildcardType) { WildcardType wt = (WildcardType) type; Type[] upperBounds = new Type[wt.getUpperBounds().length]; for (int i = 0; i < wt.getUpperBounds().length; i++) { upperBounds[i] = makeInstantiatedType(wt.getUpperBounds()[i], context); } Type[] lowerBounds = new Type[wt.getLowerBounds().length]; for (int i = 0; i < wt.getLowerBounds().length; i++) { lowerBounds[i] = makeInstantiatedType(wt.getLowerBounds()[i], context); } return new WilcardTypeImpl(upperBounds, lowerBounds); } logger.warning("Unexpected " + type); return type; }
From source file:org.soybeanMilk.core.bean.DefaultGenericConverter.java
/** * ?WildcardType???// www.j a v a2 s . co m * @param type * @return * @date 2012-5-20 */ protected boolean isSimpleWildcardType(Type type) { if (!(type instanceof WildcardType)) return false; WildcardType wt = (WildcardType) type; Type[] lb = wt.getLowerBounds(); Type[] ub = wt.getUpperBounds(); if ((lb == null || lb.length == 0) && (ub == null || ub.length == 0)) return true; else return false; }
From source file:org.soybeanMilk.SbmUtils.java
/** * // w ww.ja va2 s . c o m * @param type * @param variableTypesMap * @return * @date 2012-5-14 */ private static Type reifyInner(Type type, Map<TypeVariable<?>, Type> variableTypesMap) { Type result = null; if (type instanceof Class<?>) { result = type; } else if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; Type[] at = pt.getActualTypeArguments(); Type[] cat = new Type[at.length]; //pt?pt?? boolean reified = true; for (int i = 0; i < at.length; i++) { cat[i] = reifyInner(at[i], variableTypesMap); if (cat[i] != at[i]) reified = false; } if (reified) result = pt; else result = new CustomParameterizedType(pt.getRawType(), pt.getOwnerType(), cat); } else if (type instanceof GenericArrayType) { GenericArrayType gap = (GenericArrayType) type; Type ct = gap.getGenericComponentType(); Type cct = reifyInner(ct, variableTypesMap); if (cct == ct) result = gap; else result = new CustomGenericArrayType(cct); } else if (type instanceof TypeVariable<?>) { TypeVariable<?> tv = (TypeVariable<?>) type; if (variableTypesMap != null) result = variableTypesMap.get(tv); if (result == null) { Type[] bounds = tv.getBounds(); if (bounds == null || bounds.length == 0) result = Object.class; else result = bounds[0]; } result = reifyInner(result, variableTypesMap); } else if (type instanceof WildcardType) { WildcardType wt = (WildcardType) type; Type[] upperBounds = wt.getUpperBounds(); Type upperType = (upperBounds != null && upperBounds.length > 0 ? upperBounds[0] : null); if (upperType == null) upperType = Object.class; result = reifyInner(upperType, variableTypesMap); } else result = type; return result; }
From source file:org.tdar.core.service.ReflectionService.java
/** * Get the Class of the return type/or generic type * //from www .ja va 2 s. c o m * @param type * @return */ private static Class<?> getType(Type type) { Logger logger = LoggerFactory.getLogger(ReflectionService.class); if (WildcardType.class.isAssignableFrom(type.getClass())) { WildcardType subType = (WildcardType) type; logger.trace(" wildcard type: {} [{}]", type, type.getClass()); logger.trace(" lower: {} upper: {}", subType.getLowerBounds(), subType.getUpperBounds()); return subType.getUpperBounds().getClass(); } if (type instanceof ParameterizedType) { ParameterizedType collectionType = (ParameterizedType) type; logger.trace(" parameterized type: {} [{} - {}]", type, type.getClass(), collectionType.getActualTypeArguments()); Type subtype = collectionType.getActualTypeArguments()[0]; logger.trace(" type: {} subtype: {} ", type, subtype); if (subtype instanceof Type) { return getType(subtype); } return (Class<?>) subtype; } if (type instanceof Class<?>) { return (Class<?>) type; } return null; }
From source file:org.vulpe.commons.util.VulpeReflectUtil.java
/** * Returns class of Type./*from w ww . j ava2s . co m*/ * * @param clazz * @param type * @return */ public static DeclaredType getDeclaredType(final Class<?> clazz, final Type type) { if (type == null) { return null; } DeclaredType declaredType = null; if (type instanceof Class) { declaredType = new DeclaredType(); declaredType.setType((Class<?>) type); } else if (type instanceof ParameterizedType) { declaredType = new DeclaredType(); final ParameterizedType parameterizedType = (ParameterizedType) type; final DeclaredType rawType = getDeclaredType(clazz, parameterizedType.getRawType()); declaredType.setType(rawType.getType()); for (int i = 0; i < parameterizedType.getActualTypeArguments().length; i++) { declaredType.getItems().add(getDeclaredType(clazz, parameterizedType.getActualTypeArguments()[i])); } } else if (type instanceof TypeVariable) { return getDeclaredTypeVariable(clazz, (TypeVariable<?>) type); } else if (type instanceof WildcardType) { declaredType = new DeclaredType(); final WildcardType wildcardType = (WildcardType) type; if (wildcardType.getLowerBounds().length > 0) { declaredType.setType(getDeclaredType(clazz, wildcardType.getLowerBounds()[0]).getType()); } else { declaredType.setType(null); } for (int i = 0; i < wildcardType.getUpperBounds().length; i++) { declaredType.getTypeItems().add(getDeclaredType(clazz, wildcardType.getUpperBounds()[i])); } return declaredType; } return declaredType; }