List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments
Type[] getActualTypeArguments();
From source file:antre.TypeResolver.java
/** * Resolves the arguments for the {@code genericType} using the type variable information for the * {@code targetType}. Returns {@code null} if {@code genericType} is not parameterized or if * arguments cannot be resolved./*from w ww . ja v a 2 s .c om*/ */ public static Class<?>[] resolveArguments(Type genericType, Class<?> targetType) { Class<?>[] result = null; 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] = resolveClass(arguments[i], targetType); } else if (genericType instanceof TypeVariable) { result = new Class[1]; result[0] = resolveClass(genericType, targetType); } return result; }
From source file:GenericsUtil.java
private static void gatherTypeVariables(final Type type, final Map<TypeVariable<?>, Type> map) { if (ParameterizedType.class.isInstance(type)) { final ParameterizedType parameterizedType = ParameterizedType.class.cast(type); final TypeVariable<?>[] typeVariables = GenericDeclaration.class.cast(parameterizedType.getRawType()) .getTypeParameters();/*from www. j a va 2s.c om*/ final Type[] actualTypes = parameterizedType.getActualTypeArguments(); for (int i = 0; i < actualTypes.length; ++i) { map.put(typeVariables[i], actualTypes[i]); } } }
From source file:org.jodah.typetools.TypeResolver.java
/** * Returns an array of raw classes representing type 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. * /*from w ww .j a v a2 s .co m*/ * @param genericType * to resolve arguments for * @param subType * to extract type variable information from * @return array of raw classes representing type arguments for the {@code genericType} else {@code null} if no type * arguments are declared */ public static Class<?>[] resolveRawArguments(Type genericType, Class<?> subType) { Class<?>[] result = null; 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); } else if (genericType instanceof TypeVariable) { result = new Class[1]; result[0] = resolveRawClass(genericType, subType); } return result; }
From source file:com.autentia.common.util.ClassWithList.java
private static void print(ParameterizedType pt) { System.out.println("Parameterized type"); System.out.println("Owner: " + pt.getOwnerType()); System.out.println("Raw type: " + pt.getRawType()); for (Type actualType : pt.getActualTypeArguments()) { print(actualType);//www . j a v a 2 s. c o m } }
From source file:kina.utils.UtilMongoDB.java
private static <T> Object subDocumentListCase(Type type, List<T> bsonOject) throws IllegalAccessException, InstantiationException, InvocationTargetException { ParameterizedType listType = (ParameterizedType) type; Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0]; List list = new ArrayList(); for (T t : bsonOject) { if (BSONObject.class.isAssignableFrom(t.getClass())) { list.add(getObjectFromBson(listClass, (BSONObject) t)); } else {//from w w w.j a v a 2 s .c o m list.add(t); } } return list; }
From source file:com.github.juanmf.java2plant.Parser.java
protected static Set<String> getTypeParams(ParameterizedType f) { Set<String> typeVars = new HashSet<>(); Type[] actualTypeArguments = f.getActualTypeArguments(); for (Type t : actualTypeArguments) { typeVars.add(t.toString().replace("class ", "")); }//ww w . j a v a 2 s .c o m return typeVars; }
From source file:com.oltpbenchmark.util.ClassUtil.java
private static void getGenericTypesImpl(ParameterizedType ptype, List<Class<?>> classes) { // list the actual type arguments for (Type t : ptype.getActualTypeArguments()) { if (t instanceof Class) { // System.err.println("C: " + t); classes.add((Class<?>) t); } else if (t instanceof ParameterizedType) { ParameterizedType next = (ParameterizedType) t; // System.err.println("PT: " + next); classes.add((Class<?>) next.getRawType()); getGenericTypesImpl(next, classes); }/* ww w . j a v a 2s . c om*/ } // FOR return; }
From source file:org.apache.batchee.cli.BatchEECLI.java
private static Runnable instantiate(final Class<? extends Runnable> cmd, final CliConfiguration configuration, final Map<String, Field> fields, final boolean hasArgs, final CommandLine line) throws InstantiationException, IllegalAccessException { final Runnable commandInstance = cmd.newInstance(); if (hasArgs) { // we have few commands we can execute without args even if we have a bunch of config for (final Map.Entry<String, Field> option : fields.entrySet()) { final String key = option.getKey(); if (key.isEmpty()) { // arguments, not an option final List<String> list = line.getArgList(); if (list != null) { final Field field = option.getValue(); final Type expectedType = field.getGenericType(); if (ParameterizedType.class.isInstance(expectedType)) { final ParameterizedType pt = ParameterizedType.class.cast(expectedType); if ((pt.getRawType() == List.class || pt.getRawType() == Collection.class) && pt.getActualTypeArguments().length == 1 && pt.getActualTypeArguments()[0] == String.class) { field.set(commandInstance, list); } else { throw new IllegalArgumentException("@Arguments only supports List<String>"); }/*from w ww . ja v a2 s . co m*/ } else { throw new IllegalArgumentException("@Arguments only supports List<String>"); } } } else { final String value = line.getOptionValue(key); if (value != null) { final Field field = option.getValue(); field.set(commandInstance, configuration.coerce(value, field.getGenericType())); } } } } return commandInstance; }
From source file:JDBCPool.dbcp.demo.sourcecode.PoolImplUtils.java
/** * Obtain the concrete type used by an implementation of an interface that * uses a generic type.//www.j a v a 2s . com * * @param type The interface that defines a generic type * @param clazz The class that implements the interface with a concrete type * @param <T> The interface type * * @return concrete type used by the implementation */ private static <T> Object getGenericType(Class<T> type, Class<? extends T> clazz) { // Look to see if this class implements the generic interface // Get all the interfaces Type[] interfaces = clazz.getGenericInterfaces(); for (Type iface : interfaces) { // Only need to check interfaces that use generics if (iface instanceof ParameterizedType) { ParameterizedType pi = (ParameterizedType) iface; // Look for the generic interface if (pi.getRawType() instanceof Class) { if (type.isAssignableFrom((Class<?>) pi.getRawType())) { return getTypeParameter(clazz, pi.getActualTypeArguments()[0]); } } } } // Interface not found on this class. Look at the superclass. @SuppressWarnings("unchecked") Class<? extends T> superClazz = (Class<? extends T>) clazz.getSuperclass(); Object result = getGenericType(type, superClazz); if (result instanceof Class<?>) { // Superclass implements interface and defines explicit type for // generic return result; } else if (result instanceof Integer) { // Superclass implements interface and defines unknown type for // generic // Map that unknown type to the generic types defined in this class ParameterizedType superClassType = (ParameterizedType) clazz.getGenericSuperclass(); return getTypeParameter(clazz, superClassType.getActualTypeArguments()[((Integer) result).intValue()]); } else { // Error will be logged further up the call stack return null; } }
From source file:org.datalorax.populace.core.util.TypeUtils.java
private static Type ensureConsistentParameterisedType(final ParameterizedType type) { final Type[] consistentTypeArgs = Arrays.stream(type.getActualTypeArguments()) .map(TypeUtils::ensureConsistentType).toArray(Type[]::new); return parameterise(getRawType(type, null), consistentTypeArgs); }