List of usage examples for java.lang.reflect Field getGenericType
public Type getGenericType()
From source file:com.bosscs.spark.commons.utils.AnnotationUtils.java
/** * Returns the list of generic types associated to the provided field (if any). * * @param field the field instance to process. * @return the list of generic types associated to the provided field (if any). *//* w w w . j av a 2 s . c om*/ public static Class[] getGenericTypes(Field field) { try { ParameterizedType type = (ParameterizedType) field.getGenericType(); Type[] types = type.getActualTypeArguments(); Class<?>[] res = new Class<?>[types.length]; for (int i = 0; i < types.length; i++) { res[i] = (Class<?>) types[i]; } return res; } catch (ClassCastException e) { return new Class<?>[] { (Class<?>) field.getGenericType() }; } }
From source file:com.curl.orb.servlet.InstanceManagementUtil.java
/** * Get Generic types of fields.// ww w .ja va 2 s . c om */ public static Class<?>[] getGenericFieldTypes(Field field) { Type genericType = field.getGenericType(); return getGenericTypes(genericType); }
From source file:ca.uhn.fhir.util.ReflectionUtil.java
public static Class<?> getGenericCollectionTypeOfField(Field next) { Class<?> type;//from ww w . j a v a 2s .c om ParameterizedType collectionType = (ParameterizedType) next.getGenericType(); Type firstArg = collectionType.getActualTypeArguments()[0]; if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) { ParameterizedType pt = ((ParameterizedType) firstArg); type = (Class<?>) pt.getRawType(); } else { type = (Class<?>) firstArg; } return type; }
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>"); }/* w w w . j ava 2s . com*/ } 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:com.shrj.util.ReflectionUtils.java
public static Class getClassGenricType(final Class clazz, final int index) { try {// w w w.ja va2s . c o m Field f = clazz.getDeclaredField("typeList"); Type t = f.getGenericType(); if (t instanceof ParameterizedType) { Type[] params = ((ParameterizedType) t).getActualTypeArguments(); if (index >= params.length || index < 0) { return Object.class; } // System.out.println(params[index]); return null; } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Object.class; }
From source file:com.jb.statistics.common.utils.ReflectionUtils.java
public static Class getClassGenricType(final Class clazz, final int index) { try {// w w w. ja v a 2 s. c o m Field f = clazz.getDeclaredField("typeList"); Type t = f.getGenericType(); if (t instanceof ParameterizedType) { Type[] params = ((ParameterizedType) t).getActualTypeArguments(); if (index >= params.length || index < 0) { return Object.class; } // System.out.println(params[index]); return null; } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Object.class; }
From source file:com.github.juanmf.java2plant.Parser.java
protected static Set<String> getTypeParams(Field f) { Type tp = f.getGenericType(); if (tp instanceof ParameterizedType) { Set<String> typeVars = getTypeParams((ParameterizedType) tp); return typeVars; }/*from ww w . j a v a 2 s .co m*/ return Collections.emptySet(); }
From source file:org.alfresco.module.org_alfresco_module_rm.api.PublicAPITestUtil.java
/** * Get all classes involved in the type of the supplied field. For example {@code Pair<Set<String>, Integer> foo} * involves four classes.//from www .j a v a 2s. c om * * @param field The field to look at. * @return The set of classes. */ private static Set<Class<?>> getClassesFromField(Field field) { Set<Type> types = Sets.newHashSet(field.getGenericType()); return getClassesFromTypes(types); }
From source file:ca.uhn.fhir.util.ReflectionUtil.java
/** * For a field of type List<Enumeration<Foo>>, returns Foo */// ww w.j a v a 2s.c om public static Class<?> getGenericCollectionTypeOfFieldWithSecondOrderForList(Field next) { if (!List.class.isAssignableFrom(next.getType())) { return getGenericCollectionTypeOfField(next); } Class<?> type; ParameterizedType collectionType = (ParameterizedType) next.getGenericType(); Type firstArg = collectionType.getActualTypeArguments()[0]; if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) { ParameterizedType pt = ((ParameterizedType) firstArg); Type pt2 = pt.getActualTypeArguments()[0]; return (Class<?>) pt2; } type = (Class<?>) firstArg; return type; }
From source file:GenericClass.java
public static GenericClass forField(Field field) { return forGenericType(field.getGenericType()); }