List of usage examples for java.lang Class getTypeParameters
@SuppressWarnings("unchecked") public TypeVariable<Class<T>>[] getTypeParameters()
From source file:Main.java
public static void main(String... args) { try {//from w ww .j av a2 s .com Class<?> c = Class.forName(args[0]); out.format("Class:%n %s%n%n", c.getCanonicalName()); out.format("Modifiers:%n %s%n%n", Modifier.toString(c.getModifiers())); out.format("Type Parameters:%n"); TypeVariable[] tv = c.getTypeParameters(); if (tv.length != 0) { out.format(" "); for (TypeVariable t : tv) out.format("%s ", t.getName()); out.format("%n%n"); } else { out.format(" -- No Type Parameters --%n%n"); } out.format("Implemented Interfaces:%n"); Type[] intfs = c.getGenericInterfaces(); if (intfs.length != 0) { for (Type intf : intfs) out.format(" %s%n", intf.toString()); out.format("%n"); } else { out.format(" -- No Implemented Interfaces --%n%n"); } out.format("Inheritance Path:%n"); List<Class> l = new ArrayList<Class>(); printAncestor(c, l); if (l.size() != 0) { for (Class<?> cl : l) out.format(" %s%n", cl.getCanonicalName()); out.format("%n"); } else { out.format(" -- No Super Classes --%n%n"); } out.format("Annotations:%n"); Annotation[] ann = c.getAnnotations(); if (ann.length != 0) { for (Annotation a : ann) out.format(" %s%n", a.toString()); out.format("%n"); } else { out.format(" -- No Annotations --%n%n"); } // production code should handle this exception more gracefully } catch (ClassNotFoundException x) { x.printStackTrace(); } }
From source file:GenericReflectionTest.java
public static void printClass(Class<?> cl) { System.out.print(cl);/* w w w. j ava 2 s. com*/ printTypes(cl.getTypeParameters(), "<", ", ", ">", true); Type sc = cl.getGenericSuperclass(); if (sc != null) { System.out.print(" extends "); printType(sc, false); } printTypes(cl.getGenericInterfaces(), " implements ", ", ", "", false); System.out.println(); }
From source file:MyClass.java
public static String getGenericTypeParams(Class c) { StringBuilder sb = new StringBuilder(); TypeVariable<?>[] typeParms = c.getTypeParameters(); if (typeParms.length > 0) { String[] paramNames = new String[typeParms.length]; for (int i = 0; i < typeParms.length; i++) { paramNames[i] = typeParms[i].getTypeName(); }//from w w w . j a v a2 s .com sb.append('<'); String parmsList = String.join(",", paramNames); sb.append(parmsList); sb.append('>'); } return sb.toString(); }
From source file:GenericsUtil.java
/** * Returns all of the {@link TypeVariables}s implemented * by the given class. If none are implemented then an array * of zero length is returned./*from w ww. jav a2s .c om*/ * @param clazz the class * @param genericClazz the generic class or interface to return * the TypeVariables from * @return an array of TypeVariable */ public static TypeVariable<?>[] getGenericTypeParameters(Class<?> clazz, Type genericClazz) { List<TypeVariable<?>> vars = new ArrayList<TypeVariable<?>>(); // add superclass for (TypeVariable<?> var : clazz.getSuperclass().getTypeParameters()) { if (genericClazz == null || genericClazz.equals(var.getGenericDeclaration())) { vars.add(var); } } // add interfaces for (Class<?> iface : clazz.getInterfaces()) { for (TypeVariable<?> var : iface.getTypeParameters()) { if (genericClazz == null || genericClazz.equals(var.getGenericDeclaration())) { vars.add(var); } } } // return list return vars.toArray(new TypeVariable<?>[0]); }
From source file:com.savoirtech.eos.util.TypeVariableUtils.java
@SuppressWarnings("unchecked") public static <T, C extends Type> C getTypeVariableBinding(Class<T> concreteClass, Class<? super T> definingClass, int varIndex) { final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(concreteClass, definingClass); TypeVariable<? extends Class<? super T>> entityTypeVar = definingClass.getTypeParameters()[varIndex]; return (C) typeArguments.get(entityTypeVar); }
From source file:org.dimitrovchi.conf.service.ServiceParameterUtils.java
static AnnotationParameters annotationParameters() { final Class<?>[] stack = ClassResolver.CLASS_RESOLVER.getClassContext(); final Class<?> caller = stack[3]; final List<Class<? extends Annotation>> interfaces = new ArrayList<>(); Class<?> topCaller = null; for (int i = 3; i < stack.length && caller.isAssignableFrom(stack[i]); i++) { final Class<?> c = stack[i]; topCaller = stack[i];//from w w w .j a v a2 s .co m if (c.getTypeParameters().length != 0) { final TypeVariable<? extends Class<?>> var = c.getTypeParameters()[0]; final List<Class<? extends Annotation>> bounds = new ArrayList<>(var.getBounds().length); for (final Type type : var.getBounds()) { if (type instanceof Class<?> && ((Class<?>) type).isAnnotation()) { bounds.add((Class) type); } } if (bounds.size() > interfaces.size()) { interfaces.clear(); interfaces.addAll(bounds); } } } final Map<Class<? extends Annotation>, List<Annotation>> annotationMap = new IdentityHashMap<>(); for (int i = 3; i < stack.length && caller.isAssignableFrom(stack[i]); i++) { final Class<?> c = stack[i]; for (final Class<? extends Annotation> itf : interfaces) { final Annotation annotation = c.getAnnotation(itf); if (annotation != null) { List<Annotation> annotationList = annotationMap.get(itf); if (annotationList == null) { annotationMap.put(itf, annotationList = new ArrayList<>()); } annotationList.add(0, annotation); } } } return new AnnotationParameters(topCaller, interfaces, annotationMap); }
From source file:JDBCPool.dbcp.demo.sourcecode.PoolImplUtils.java
/** * For a generic parameter, return either the Class used or if the type * is unknown, the index for the type in definition of the class * * @param clazz defining class/* w w w. ja va 2 s. com*/ * @param argType the type argument of interest * * @return An instance of {@link Class} representing the type used by the * type parameter or an instance of {@link Integer} representing * the index for the type in the definition of the defining class */ private static Object getTypeParameter(Class<?> clazz, Type argType) { if (argType instanceof Class<?>) { return argType; } else { TypeVariable<?>[] tvs = clazz.getTypeParameters(); for (int i = 0; i < tvs.length; i++) { if (tvs[i].equals(argType)) { return Integer.valueOf(i); } } return null; } }
From source file:com.iterranux.droolsjbpmCore.internal.AbstractGenericFactory.java
/** * Get the actual type arguments a child class has used to extend a generic base class. * * @param baseClass the base class//w ww . ja v a 2s. co m * @param childClass the child class * @return a list of the raw classes for the actual type arguments. */ @SuppressWarnings("all") 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:com.jaspersoft.jasperserver.war.helper.GenericParametersHelper.java
public static Class<?> getGenericTypeArgument(Class<?> classToParse, Class<?> genericClassToFind, Integer argumentIndex) {// w w w . j a v a 2 s.c o m Class<?> result = null; ParameterizedType parameterizedType = null; Class<?> currentClass = classToParse; Map<String, Class<?>> currentParameterValues = new HashMap<String, Class<?>>(); Type[] previousTypeArguments = null; while (parameterizedType == null) { final TypeVariable<? extends Class<?>>[] typeParameters = currentClass.getTypeParameters(); currentParameterValues = getCurrentParameterValues(typeParameters, previousTypeArguments, currentParameterValues); parameterizedType = findParametrizedType(currentClass, genericClassToFind, currentParameterValues); if (parameterizedType == null) { // current class doesn't extend/implement searched class directly. Should parse superclass final Type genericSuperclassType = currentClass.getGenericSuperclass(); if (genericSuperclassType instanceof Class<?>) { log.debug(classToParse.getName() + " is raw subclass of " + genericClassToFind.getName()); return null; } final ParameterizedType genericSuperclass = (ParameterizedType) genericSuperclassType; previousTypeArguments = genericSuperclass.getActualTypeArguments(); currentClass = (Class<?>) genericSuperclass.getRawType(); } } final Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); if (actualTypeArguments[argumentIndex] instanceof Class<?>) { result = (Class<?>) actualTypeArguments[argumentIndex]; } else if (actualTypeArguments[argumentIndex] instanceof TypeVariable) { result = currentParameterValues.get(((TypeVariable<?>) actualTypeArguments[argumentIndex]).getName()); } if (result == null) { log.debug("Class " + classToParse.getName() + " has unsupported inheritance structure"); } return result; }
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!) * //from w w w.ja v a 2s .c om * @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; }