List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments
Type[] getActualTypeArguments();
From source file:com.yx.baseframe.util.ReflectionUtils.java
/** * Get the actual type arguments a child class has used to extend a generic * base class. (Taken from http://www.artima.com/weblogs/viewpost.jsp?thread=208860. Thanks * mathieu.grenonville for finding this solution!) * /* w ww.j a v a2 s. c o m*/ * @param baseClass * the base class * @param childClass * the child class * @return a list of the raw classes for the actual type arguments. */ public static <T> List<Class<?>> getTypeArguments(Class<T> baseClass, Class<? extends T> childClass) { Map<Type, Type> resolvedTypes = new HashMap<Type, Type>(); Type type = childClass; // start walking up the inheritance hierarchy until we hit baseClass while (!getClass(type).equals(baseClass)) { if (type instanceof Class) { // there is no useful information for us in raw types, so just // keep going. type = ((Class) type).getGenericSuperclass(); } else { ParameterizedType parameterizedType = (ParameterizedType) type; Class<?> rawType = (Class) parameterizedType.getRawType(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); TypeVariable<?>[] typeParameters = rawType.getTypeParameters(); for (int i = 0; i < actualTypeArguments.length; i++) { resolvedTypes.put(typeParameters[i], actualTypeArguments[i]); } if (!rawType.equals(baseClass)) { type = rawType.getGenericSuperclass(); } } } // finally, for each actual type argument provided to baseClass, // determine (if possible) // the raw class for that type argument. Type[] actualTypeArguments; if (type instanceof Class) { actualTypeArguments = ((Class) type).getTypeParameters(); } else { actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments(); } List<Class<?>> typeArgumentsAsClasses = new ArrayList<Class<?>>(); // resolve types by chasing down type variables. for (Type baseType : actualTypeArguments) { while (resolvedTypes.containsKey(baseType)) { baseType = resolvedTypes.get(baseType); } typeArgumentsAsClasses.add(getClass(baseType)); } return typeArgumentsAsClasses; }
From source file:org.eiichiro.bootleg.Types.java
/** * Returns the element type of the specified collection type. * The specified type must be collection or array. To make it sure, use * {@code #isCollection(Type)} method or {@code #isArray(Type)} method. * //w w w . j a v a 2 s . c o m * @param type Collection or array type. * @return The element type of the specified collection or array. * @throws IllegalArgumentException If the specified 'type' is not a * collection or array. */ public static Class<?> getElementType(Type type) { if (isCollection(type)) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; return (Class<?>) parameterizedType.getActualTypeArguments()[0]; } else { return Object.class; } } else if (isArray(type)) { if (type instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) type; return (Class<?>) genericArrayType.getGenericComponentType(); } else { Class<?> clazz = (Class<?>) type; return clazz.getComponentType(); } } else { throw new IllegalArgumentException("'type' must be a collection or array"); } }
From source file:org.evosuite.setup.TestUsageChecker.java
public static boolean canUse(java.lang.reflect.Type t) { if (t instanceof Class<?>) { return canUse((Class<?>) t); } else if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) t; for (java.lang.reflect.Type parameterType : pt.getActualTypeArguments()) { if (!canUse(parameterType)) return false; }//from w w w . j av a2s. c o m if (!canUse(pt.getOwnerType())) { return false; } } // If it's not declared, let's assume it's ok return true; }
From source file:com.sunchenbin.store.feilong.core.lang.reflect.TypeUtil.java
/** * Extract actual type argument class array. * * @param parameterizedType//from ww w.jav a2 s. c om * the parameterized type * @return the class<?>[] * @see java.lang.reflect.ParameterizedType#getActualTypeArguments() * @since 1.1.1 */ private static Class<?>[] extractActualTypeArgumentClassArray(ParameterizedType parameterizedType) { if (Validator.isNullOrEmpty(parameterizedType)) { throw new NullPointerException("parameterizedType can't be null/empty!"); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("parameterizedType info:{}", JsonUtil.format(parameterizedType)); } Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); if (Validator.isNotNullOrEmpty(actualTypeArguments)) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("actualTypeArguments:{}", JsonUtil.format(actualTypeArguments)); } int length = actualTypeArguments.length; Class<?>[] klasses = new Class<?>[length]; for (int i = 0, j = length; i < j; ++i) { klasses[i] = (Class<?>) actualTypeArguments[i]; } return klasses; } throw new NullPointerException("the actualTypeArguments is null or empty!"); }
From source file:de.ifgi.fmt.utils.Utils.java
/** * /* www . j a v a 2 s .co m*/ * @param t * @param collClass * @param itemClass * @return */ public static boolean isParameterizedWith(Type t, Class<?> collClass, Class<?> itemClass) { if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) t; if (collClass.isAssignableFrom((Class<?>) pt.getRawType())) { Type argT = pt.getActualTypeArguments()[0]; Class<?> tV = null; if (argT instanceof ParameterizedType) { tV = (Class<?>) ((ParameterizedType) argT).getRawType(); } else if (argT instanceof Class) { tV = (Class<?>) argT; } else { return false; } return itemClass.isAssignableFrom(tV); } } return false; }
From source file:com.feilong.commons.core.lang.reflect.TypeUtil.java
/** * Extract actual type argument class array. * * @param parameterizedType//from www .j a va 2s. co m * the parameterized type * @return the class<?>[] * @see java.lang.reflect.ParameterizedType#getActualTypeArguments() * @since 1.1.1 */ private static Class<?>[] extractActualTypeArgumentClassArray(ParameterizedType parameterizedType) { if (Validator.isNullOrEmpty(parameterizedType)) { throw new NullPointerException("parameterizedType can't be null/empty!"); } if (log.isDebugEnabled()) { log.debug("parameterizedType info:{}", JsonUtil.format(parameterizedType)); } Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); if (Validator.isNotNullOrEmpty(actualTypeArguments)) { if (log.isDebugEnabled()) { log.debug("actualTypeArguments:{}", JsonUtil.format(actualTypeArguments)); } int length = actualTypeArguments.length; Class<?>[] klasses = new Class<?>[length]; for (int i = 0, j = length; i < j; ++i) { klasses[i] = (Class<?>) actualTypeArguments[i]; } return klasses; } throw new NullPointerException("the actualTypeArguments is null or empty!"); }
From source file:org.evosuite.utils.generic.GenericAccessibleObject.java
/** * Returns the exact return type of the given method in the given type. This * may be different from <tt>m.getGenericReturnType()</tt> when the method * was declared in a superclass, or <tt>type</tt> has a type parameter that * is used in the return type, or <tt>type</tt> is a raw type. *//*from w w w .j av a2 s. co m*/ protected static Type getTypeFromExactReturnType(ParameterizedType returnType, ParameterizedType type) { Map<TypeVariable<?>, Type> typeMap = TypeUtils.getTypeArguments(returnType); Type[] actualParameters = new Type[type.getActualTypeArguments().length]; int num = 0; for (TypeVariable<?> parameterType : ((Class<?>) type.getRawType()).getTypeParameters()) { //for(Type parameterType : type.getActualTypeArguments()) { // if(parameterType instanceof TypeVariable<?>) { boolean replaced = false; for (TypeVariable<?> var : typeMap.keySet()) { // D'oh! Why the heck do we need this?? if (var.getName().equals(parameterType.getName())) { //if(typeMap.containsKey(parameterType)) { actualParameters[num] = typeMap.get(var); replaced = true; break; //} else { } } if (!replaced) { actualParameters[num] = parameterType; } //} // } else { // LoggingUtils.getEvoLogger().info("Not a type variable "+parameterType); // actualParameters[num] = parameterType; // } num++; } return new ParameterizedTypeImpl((Class<?>) type.getRawType(), actualParameters, null); }
From source file:net.jodah.typetools.TypeResolver.java
/** * Returns an array of raw classes representing arguments for the {@code genericType} using type variable information * from the {@code subType}. Arguments for {@code genericType} that cannot be resolved are returned as * {@code Unknown.class}. If no arguments can be resolved then {@code null} is returned. * * @param genericType to resolve arguments for * @param subType to extract type variable information from * @return array of raw classes representing arguments for the {@code genericType} else {@code null} if no type * arguments are declared//from w w w . j a va2 s. c o m */ public static Class<?>[] resolveRawArguments(Type genericType, Class<?> subType) { Class<?>[] result = null; Class<?> functionalInterface = null; // Handle lambdas if (SUPPORTS_LAMBDAS && subType.isSynthetic()) { Class<?> fi = genericType instanceof ParameterizedType && ((ParameterizedType) genericType).getRawType() instanceof Class ? (Class<?>) ((ParameterizedType) genericType).getRawType() : genericType instanceof Class ? (Class<?>) genericType : null; if (fi != null && fi.isInterface()) functionalInterface = fi; } if (genericType instanceof ParameterizedType) { ParameterizedType paramType = (ParameterizedType) genericType; Type[] arguments = paramType.getActualTypeArguments(); result = new Class[arguments.length]; for (int i = 0; i < arguments.length; i++) result[i] = resolveRawClass(arguments[i], subType, functionalInterface); } else if (genericType instanceof TypeVariable) { result = new Class[1]; result[0] = resolveRawClass(genericType, subType, functionalInterface); } else if (genericType instanceof Class) { TypeVariable<?>[] typeParams = ((Class<?>) genericType).getTypeParameters(); result = new Class[typeParams.length]; for (int i = 0; i < typeParams.length; i++) result[i] = resolveRawClass(typeParams[i], subType, functionalInterface); } return result; }
From source file:de.bund.bva.pliscommon.serviceapi.core.serviceimpl.MappingHelper.java
/** * Mappt eine Collection.// w w w. ja v a2 s . co m * * @param mapper * der Dozer-Mapper * @param source * eine Collection * @param parDestinationType * der Typ der Ziel-Collection * @param result * die instanziierte, leere Ziel-Collection * @return die gemappte Collection */ static Collection<Object> mapCollection(Mapper mapper, Object source, ParameterizedType parDestinationType, Collection<Object> result) { if (!(source instanceof Collection)) { throw new IllegalArgumentException("Ein Objekt vom Typ " + source.getClass() + " kann nicht auf " + parDestinationType + " gemappt werden"); } Collection<?> sourceCollection = (Collection<?>) source; Type destElemType = parDestinationType.getActualTypeArguments()[0]; for (Object sourceElem : sourceCollection) { Object destElem = map(mapper, sourceElem, destElemType); result.add(destElem); } return result; }
From source file:org.assertj.assertions.generator.util.ClassUtil.java
public static Set<Class<?>> getClassesRelatedTo(Type type) { Set<Class<?>> classes = new HashSet<Class<?>>(); // non generic type : just add current type. if (type instanceof Class) { classes.add((Class<?>) type); return classes; }// w w w . j a v a 2s. c o m // generic type : add current type and its parameter types if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; for (Type actualTypeArgument : parameterizedType.getActualTypeArguments()) { if (actualTypeArgument instanceof ParameterizedType) { classes.addAll(getClassesRelatedTo(actualTypeArgument)); } else if (actualTypeArgument instanceof Class) { classes.add((Class<?>) actualTypeArgument); } else if (actualTypeArgument instanceof GenericArrayType) { classes.addAll(getClassesRelatedTo(actualTypeArgument)); } } Type rawType = parameterizedType.getRawType(); if (rawType instanceof Class) { classes.add((Class<?>) rawType); } } return classes; }