public static TypeClassPair getTypeArgumentAndClass(Type parameterizedType) throws IllegalArgumentException { final Type t = getTypeArgumentOfParameterizedType(parameterizedType); if (t == null) { return null; } final Class c = getClassOfType(t); if (c == null) { throw new IllegalArgumentException("Generic type not supported"); } return new TypeClassPair(t, c); }
public static TypeClassPair getTypeArgumentAndClass(Type parameterizedType) throws IllegalArgumentException { final Type t = getTypeArgumentOfParameterizedType(parameterizedType); if (t == null) { return null; } final Class c = getClassOfType(t); if (c == null) { throw new IllegalArgumentException("Generic type not supported"); } return new TypeClassPair(t, c); }
/** * Get the class that is the type argument of a parameterized type. * * @param parameterizedType must be an instance of ParameterizedType * and have exactly one type argument. * @return the class of the actual type argument. If the type argument * is a class then the class is returned. If the type argument * is a generic array type and the generic component type is a * class then class of the array is returned. if the type argument * is a parameterized type and it's raw type is a class then * that class is returned. * If the parameterizedType is not an instance of ParameterizedType * or contains more than one type argument null is returned. * @throws IllegalArgumentException if the single type argument is not of * a class, or a generic array type, or the generic component type * of the generic array type is not class, or not a parameterized * type with a raw type that is not a class. */ public static Class getGenericClass(Type parameterizedType) throws IllegalArgumentException { final Type t = getTypeArgumentOfParameterizedType(parameterizedType); if (t == null) { return null; } final Class c = getClassOfType(t); if (c == null) { throw new IllegalArgumentException("Type not supported"); } return c; }
/** * Get the parameterized class arguments for a declaring class that declares a generic interface type. * * @param p the declaring class * @return the parameterized class arguments, or null if the generic interface type is not a parameterized type. */ public static Class[] getParameterizedClassArguments(DeclaringClassInterfacePair p) { if (p.genericInterface instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) p.genericInterface; Type[] as = pt.getActualTypeArguments(); Class[] cas = new Class[as.length]; for (int i = 0; i < as.length; i++) { Type a = as[i]; if (a instanceof Class) { cas[i] = (Class) a; } else if (a instanceof ParameterizedType) { pt = (ParameterizedType) a; cas[i] = (Class) pt.getRawType(); } else if (a instanceof TypeVariable) { ClassTypePair ctp = resolveTypeVariable(p.concreteClass, p.declaringClass, (TypeVariable) a); cas[i] = (ctp != null) ? ctp.c : Object.class; } else if (a instanceof GenericArrayType) { cas[i] = getClassOfType(a); } } return cas; } else { return null; } }
/** * Get the class that is the type argument of a parameterized type. * * @param parameterizedType must be an instance of ParameterizedType * and have exactly one type argument. * @return the class of the actual type argument. If the type argument * is a class then the class is returned. If the type argument * is a generic array type and the generic component type is a * class then class of the array is returned. if the type argument * is a parameterized type and it's raw type is a class then * that class is returned. * If the parameterizedType is not an instance of ParameterizedType * or contains more than one type argument null is returned. * @throws IllegalArgumentException if the single type argument is not of * a class, or a generic array type, or the generic component type * of the generic array type is not class, or not a parameterized * type with a raw type that is not a class. */ public static Class getGenericClass(Type parameterizedType) throws IllegalArgumentException { final Type t = getTypeArgumentOfParameterizedType(parameterizedType); if (t == null) { return null; } final Class c = getClassOfType(t); if (c == null) { throw new IllegalArgumentException("Type not supported"); } return c; }
/** * Get the parameterized class arguments for a declaring class that declares a generic interface type. * * @param p the declaring class * @return the parameterized class arguments, or null if the generic interface type is not a parameterized type. */ public static Class[] getParameterizedClassArguments(DeclaringClassInterfacePair p) { if (p.genericInterface instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) p.genericInterface; Type[] as = pt.getActualTypeArguments(); Class[] cas = new Class[as.length]; for (int i = 0; i < as.length; i++) { Type a = as[i]; if (a instanceof Class) { cas[i] = (Class) a; } else if (a instanceof ParameterizedType) { pt = (ParameterizedType) a; cas[i] = (Class) pt.getRawType(); } else if (a instanceof TypeVariable) { ClassTypePair ctp = resolveTypeVariable(p.concreteClass, p.declaringClass, (TypeVariable) a); cas[i] = (ctp != null) ? ctp.c : Object.class; } else if (a instanceof GenericArrayType) { cas[i] = getClassOfType(a); } } return cas; } else { return null; } }