Advertisement
MrFrAnTA

Get generic parameter of class

Feb 13th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1.   @SuppressWarnings("unchecked")
  2.   public Class<T> getValueType() {
  3.     for (Class<?> clazz = getClass(); clazz.getSuperclass() != null; clazz = clazz.getSuperclass()) {
  4.       Type genericSuperclass = clazz.getGenericSuperclass();
  5.       if (genericSuperclass instanceof ParameterizedType) {
  6.         Type[] typeArguments = ((ParameterizedType)genericSuperclass).getActualTypeArguments();
  7.         if (typeArguments.length > 0) {
  8.           // we are looking for first type argument
  9.           Type type = typeArguments[0];
  10.           try {
  11.             if (type instanceof ParameterizedType) {
  12.               // type argument is parameterized type, we have to get raw type
  13.               return (Class<T>)((ParameterizedType)type).getRawType();
  14.             }
  15.             else if (type instanceof GenericArrayType) {
  16.               // type argument is array type, we have to analyze generic component
  17.               Type componentType = ((GenericArrayType)type).getGenericComponentType();
  18.  
  19.               // analyze the dimensions
  20.               StringBuilder sb = new StringBuilder();
  21.               sb.append('[');
  22.               while (componentType instanceof GenericArrayType) {
  23.                 sb.append('[');
  24.                 componentType = ((GenericArrayType)componentType).getGenericComponentType();
  25.               }
  26.  
  27.               // analyze the component class
  28.               Class<?> componentClass = null;
  29.               if (componentType instanceof ParameterizedType) {
  30.                 componentClass = (Class<?>)((ParameterizedType)componentType).getRawType();
  31.               }
  32.               else {
  33.                 componentClass = (Class<?>)componentType;
  34.               }
  35.  
  36.               // finish the class name
  37.               if (componentClass.isPrimitive()) {
  38.                 sb.append(PrimitiveUtils.abbreviate(componentClass));
  39.               }
  40.               else {
  41.                 sb.append('L').append(componentClass.getName()).append(';');
  42.               }
  43.  
  44.               // find the class
  45.               return (Class<T>)Class.forName(sb.toString());
  46.             }
  47.             else if (type instanceof TypeVariable) {
  48.               // type argument is type variable, we will return first bound
  49.               Type[] bounds = ((TypeVariable<?>)type).getBounds();
  50.               if (bounds.length > 0) {
  51.                 if (bounds[0] instanceof ParameterizedType) {
  52.                   return (Class<T>)((ParameterizedType)bounds[0]).getRawType();
  53.                 }
  54.                 else {
  55.                   return (Class<T>)bounds[0];
  56.                 }
  57.               }
  58.             }
  59.             else {
  60.               return (Class<T>)type;
  61.             }
  62.           }
  63.           catch (ClassCastException exc) {
  64.             // only for catching
  65.           }
  66.           catch (ClassNotFoundException exc) {
  67.             // only for catching
  68.           }
  69.         }
  70.       }
  71.     }
  72.  
  73.     return (Class<T>)Object.class;
  74.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement