vanOekel

ThingType

Jan 29th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. import java.lang.reflect.ParameterizedType;
  2. import java.lang.reflect.Type;
  3. import java.lang.reflect.TypeVariable;
  4.  
  5. // http://stackoverflow.com/q/28143029/3080094
  6. public class ThingType {
  7.  
  8.     class ObjectThing<O> {}
  9.     class NumberThing<N extends Number> extends ObjectThing<N> {}
  10.     class IntegerThing extends NumberThing<Integer> {}
  11.    
  12.     public static void main(String[] args) {
  13.        
  14.         try {
  15.             ThingType tt = new ThingType();
  16.             tt.reflectThing(ObjectThing.class);
  17.             tt.reflectThing(NumberThing.class);
  18.             tt.reflectThing(IntegerThing.class);
  19.         } catch (Exception e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.  
  24.     void reflectThing(Class<?> thingClass) {
  25.        
  26.         String classDecl = getClassDeclName(thingClass);
  27.         System.out.println("Class: " + thingClass.getName());
  28.         classDecl += reflectThingTypeVars(thingClass);
  29.        
  30.         Class<?> thingSuperClass = thingClass.getSuperclass();
  31.         if (thingSuperClass != null && thingSuperClass != Object.class) {
  32.             System.out.println("Super class: " + thingSuperClass.getName());
  33.             classDecl = classDecl + " extends " + getClassDeclName(thingSuperClass);
  34.             classDecl += reflectThingSuperTypeArgs(thingClass.getGenericSuperclass());
  35.         }
  36.         System.out.println("Source declaration: " + classDecl);
  37.         System.out.println();
  38.     }
  39.    
  40.     String getClassDeclName(Class<?> c) {
  41.        
  42.         String cname = c.getName().replace('$', '.');
  43.         if (cname.indexOf('.') > 0) {
  44.             cname = cname.substring(cname.lastIndexOf('.') + 1);
  45.         }
  46.         return cname;
  47.     }
  48.    
  49.     String reflectThingTypeVars(Class<?> thingClass) {
  50.        
  51.         TypeVariable<?>[] typeVars = thingClass.getTypeParameters();
  52.         if (typeVars.length == 0) {
  53.             System.out.println("No type variables for " + thingClass);
  54.             return "";
  55.         }
  56.         System.out.println("Typevar: " + typeVars[0] + " (of " + typeVars.length + ")");
  57.         Type[] types = typeVars[0].getBounds();
  58.         System.out.println("Type: " + types[0]+ " (of " + types.length + ")");
  59.         String decl;
  60.         if (types[0] == Object.class) {
  61.             decl = typeVars[0].toString();
  62.         } else {
  63.             decl = typeVars[0].toString() + " extends " + getClassDeclName((Class<?>) types[0]);
  64.         }
  65.         return "<" + decl + ">";
  66.     }
  67.    
  68.     String reflectThingSuperTypeArgs(Type genSuperClass) {
  69.        
  70.         String ta = "";
  71.         if (genSuperClass instanceof ParameterizedType) {
  72.             ParameterizedType genSuperPt = (ParameterizedType) genSuperClass;
  73.             Type[] typeArgs = genSuperPt.getActualTypeArguments();
  74.             System.out.println("Type arg: " + typeArgs[0] + " (of " + typeArgs.length + ")");
  75.             if (typeArgs[0] instanceof Class<?>) {
  76.                 ta = getClassDeclName((Class<?>) typeArgs[0]);
  77.             } else {
  78.                 ta = typeArgs[0].toString();
  79.             }
  80.             ta = "<" + ta + ">";
  81.         }
  82.         return ta;
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment