List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments
Type[] getActualTypeArguments();
From source file:Main.java
public static Class<?> getRspClass(Class<?> cbkClass) { try {// w ww. j a va2s . c om java.lang.reflect.Type type = cbkClass.getGenericSuperclass(); if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Class<?> cls = (Class<?>) parameterizedType.getActualTypeArguments()[0]; return cls; } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static Class<?> getCollectionType(Field field) { if (field.getGenericType() instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) field.getGenericType(); return (Class<?>) type.getActualTypeArguments()[0]; }//from ww w .j a v a2s .c om return Object.class; }
From source file:Main.java
/** * Returns the generic class of multi-value objects. * /*from w w w .ja v a 2s . co m*/ * @param p * Field to examine * @return The Class<?> of generic type if any, otherwise null */ public static Class<?> getGenericMultivalueType(final Field p) { if (p.getType() instanceof Class<?>) { final Type genericType = p.getGenericType(); if (genericType != null && genericType instanceof ParameterizedType) { final ParameterizedType pt = (ParameterizedType) genericType; if (pt.getActualTypeArguments() != null && pt.getActualTypeArguments().length > 0) { if (((Class<?>) pt.getRawType()).isAssignableFrom(Map.class)) { if (pt.getActualTypeArguments()[1] instanceof Class<?>) { return (Class<?>) pt.getActualTypeArguments()[1]; } else if (pt.getActualTypeArguments()[1] instanceof ParameterizedType) return (Class<?>) ((ParameterizedType) pt.getActualTypeArguments()[1]).getRawType(); } else if (pt.getActualTypeArguments()[0] instanceof Class<?>) { return (Class<?>) pt.getActualTypeArguments()[0]; } else if (pt.getActualTypeArguments()[0] instanceof ParameterizedType) return (Class<?>) ((ParameterizedType) pt.getActualTypeArguments()[0]).getRawType(); } } else if (p.getType().isArray()) return p.getType().getComponentType(); } return null; }
From source file:Main.java
/** * Resgata todos os generics da classe./*from w w w. j a v a 2s.c o m*/ * @param clazz Classe generics * @return Generic Type */ public static Class<?>[] getTypeGenericsInstance(Class<?> clazz) { ParameterizedType parameterized = (ParameterizedType) clazz.getGenericSuperclass(); Type[] arguments = parameterized.getActualTypeArguments(); Class<?>[] classes = new Class[arguments.length]; for (int i = 0; i < classes.length; i++) { classes[i] = (Class<?>) arguments[i]; } return classes; }
From source file:Main.java
/** * Resgata o tipo generico da classe/*from w w w . j a v a 2s.co m*/ * @param clazz Classe generics * @return Generic Type */ public static Class<?> getTypeGenericInstance(Class<?> clazz) { ParameterizedType parameterized = (ParameterizedType) clazz.getGenericSuperclass(); Type[] arguments = parameterized.getActualTypeArguments(); return (Class<?>) arguments[0]; }
From source file:Main.java
/** * Resgata o tipo generico da classe pelo seu index * @param clazz Classe generics/*from www .j av a2 s . c o m*/ * @param i index do generic. * @return Generic Type */ public static Class<?> getTypeGenericInstance(Class<?> clazz, int i) { ParameterizedType parameterized = (ParameterizedType) clazz.getGenericSuperclass(); Type[] arguments = parameterized.getActualTypeArguments(); return (Class<?>) arguments[i]; }
From source file:Main.java
public static Class<?> getCollectionType(Method method) { if (method.getGenericReturnType() instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) method.getGenericReturnType(); return (Class<?>) type.getActualTypeArguments()[0]; }// w ww. j ava 2 s. c om return Object.class; }
From source file:Main.java
protected static List<Type> getGenericParameterList(Type type) { if (ParameterizedType.class.isInstance(type)) { final ParameterizedType paramType = ParameterizedType.class.cast(type); return Arrays.asList(paramType.getActualTypeArguments()); }/* w w w. j a v a 2s. c o m*/ if (GenericArrayType.class.isInstance(type)) { final GenericArrayType arrayType = GenericArrayType.class.cast(type); return getGenericParameterList(arrayType.getGenericComponentType()); } @SuppressWarnings("unchecked") List<Type> emptyList = Collections.EMPTY_LIST; return emptyList; }
From source file:Main.java
@SuppressWarnings("rawtypes") public static Class[] getGeneric(ParameterizedType type) { try {//from www . j a v a2 s. c o m if (type != null) { Type[] typeArgs = type.getActualTypeArguments(); if (typeArgs != null && typeArgs.length > 0) { Class[] args = new Class[typeArgs.length]; for (int i = 0; i < typeArgs.length; i++) { Type arg = typeArgs[i]; args[i] = (Class) arg; } return args; } } } catch (Exception e) { } return null; }
From source file:ReflectionUtils.java
public static Class<?> getTargetType(Field field) { // Generic type, case when we have a Collection of ? if (field.getGenericType() instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) field.getGenericType(); if (type.getActualTypeArguments().length == 1 && type.getActualTypeArguments()[0] instanceof Class) return (Class<?>) type.getActualTypeArguments()[0]; }/* w ww .ja v a 2 s .co m*/ return field.getType(); }