List of usage examples for java.lang.reflect Type getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:Main.java
/** * Returns an array of {@code Type} objects representing the actual type * arguments to this object.//from w w w.j a v a 2s . com * If the returned value is null, then this object represents a non-parameterized * object. * * @param object the {@code object} whose type arguments are needed. * @return an array of {@code Type} objects representing the actual type * arguments to this object. * * @see {@link Class#getGenericSuperclass()} * @see {@link ParameterizedType#getActualTypeArguments()} */ public static Type[] getParameterizedTypes(Object object) { Type superclassType = object.getClass().getGenericSuperclass(); if (!ParameterizedType.class.isAssignableFrom(superclassType.getClass())) { return null; } return ((ParameterizedType) superclassType).getActualTypeArguments(); }
From source file:Main.java
/** * getChildClass//from w ww . ja va 2s . c om * * @param field List<Child> children type * @return thie child class */ public static Class getChildClass(Field field) { Type t = field.getGenericType(); Type actualType = ((ParameterizedType) t).getActualTypeArguments()[0]; Class subclass = null; try { subclass = Class.forName(actualType.getClass().getName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println(subclass.getSimpleName()); return subclass; }
From source file:com.comcast.cereal.impl.ReflectionHelper.java
public static Class<?> getGenericClass(Type type, int arg) { if ((null == type) || !ParameterizedType.class.isAssignableFrom(type.getClass())) { return null; }/* w ww. ja v a2s.c o m*/ try { ParameterizedType paramType = (ParameterizedType) type; return (Class<?>) paramType.getActualTypeArguments()[arg]; } catch (Throwable throwable) { return null; } }
From source file:GenericsUtil.java
/** * Returns the class defined for the type variable * of the given name. //from w w w.j a v a 2s .c om * @param clazz the class * @param genericClazz the generic class or interface to check the type for * @param name the name of the type variable * @param recursive whether or not to recurse up the * object's inheritance hierarchy. * @return the class */ public static Class<?> getTypeVariableClassByName(Class<?> clazz, Type genericClazz, String name, Boolean recursive) { // we hit the end of the line here :) if (clazz == null || clazz.equals(Object.class)) { return null; } // loop through all of the types implemented for (ParameterizedType pType : getGenericTypes(clazz)) { // do all of them, or one of them if (genericClazz == null || genericClazz.equals(pType.getRawType())) { // get super class type variables TypeVariable<?>[] typeVars = getGenericTypeParameters(clazz, pType.getRawType()); for (int i = 0; i < typeVars.length; i++) { if ((genericClazz == null || genericClazz.equals(typeVars[i].getGenericDeclaration())) && typeVars[i].getName().equals(name)) { // get the type Type type = pType.getActualTypeArguments()[i]; if (Class.class.isAssignableFrom(type.getClass())) { return (Class<?>) type; } else if (ParameterizedType.class.isAssignableFrom(type.getClass())) { return (Class<?>) ((ParameterizedType) type).getRawType(); } } } } } // none found return (recursive) ? getTypeVariableClassByName(clazz.getSuperclass(), genericClazz, name, recursive) : null; }
From source file:ca.uhn.fhir.util.ReflectionUtil.java
public static Class<?> getGenericCollectionTypeOfField(Field next) { Class<?> type;// ww w.j a va2 s .c o m 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:ca.uhn.fhir.util.ReflectionUtil.java
public static Class<?> getGenericCollectionTypeOfMethodParameter(Method theMethod, int theParamIndex) { Class<?> type;/* w w w. j av a 2s . c o m*/ Type genericParameterType = theMethod.getGenericParameterTypes()[theParamIndex]; if (Class.class.equals(genericParameterType)) { return null; } ParameterizedType collectionType = (ParameterizedType) genericParameterType; 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:ca.uhn.fhir.util.ReflectionUtil.java
/** * For a field of type List<Enumeration<Foo>>, returns Foo *//*w ww.j av a2 s .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:ca.uhn.fhir.util.ReflectionUtil.java
@SuppressWarnings({ "rawtypes" }) public static Class<?> getGenericCollectionTypeOfMethodReturnType(Method theMethod) { Class<?> type;//from w w w .ja v a 2 s . c o m Type genericReturnType = theMethod.getGenericReturnType(); if (!(genericReturnType instanceof ParameterizedType)) { return null; } ParameterizedType collectionType = (ParameterizedType) genericReturnType; Type firstArg = collectionType.getActualTypeArguments()[0]; if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) { ParameterizedType pt = ((ParameterizedType) firstArg); type = (Class<?>) pt.getRawType(); } else if (firstArg instanceof TypeVariable<?>) { Type decl = ((TypeVariable) firstArg).getBounds()[0]; return (Class<?>) decl; } else if (firstArg instanceof WildcardType) { Type decl = ((WildcardType) firstArg).getUpperBounds()[0]; return (Class<?>) decl; } else { type = (Class<?>) firstArg; } return type; }
From source file:io.github.sparta.helpers.reflex.Reflections.java
/** * ??, Class?.//from w w w. ja v a 2 s . co m * , Object.class. * <p/> * public UserDao extends HibernateDao<User,Long> * * @param clazz clazz The class to introspect * @param index the Index of the generic ddeclaration,start from 0. * @return the index generic declaration, or Object.class if cannot be determined */ @SuppressWarnings("rawtypes") public static Class getSuperClassGenricType(final Class clazz, final int index) { Type genType = clazz.getGenericSuperclass(); if (!(genType.getClass().isAssignableFrom(ParameterizedType.class))) { log.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType"); return Object.class; } Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { log.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: " + params.length); return Object.class; } if (!(params[index].getClass().isAssignableFrom(Class.class))) { log.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter"); return Object.class; } return (Class) params[index]; }
From source file:org.dozer.util.ReflectionUtils.java
public static Class<?> determineGenericsType(Type type) { Class<?> result = null; if (type != null && ParameterizedType.class.isAssignableFrom(type.getClass())) { Type genericType = ((ParameterizedType) type).getActualTypeArguments()[0]; if (genericType != null) { if (!(genericType instanceof TypeVariable)) { result = (Class<?>) genericType; }/* w ww . ja v a2 s. co m*/ } } return result; }