Home | History | Annotate | Download | only in src
      1 import otherpackage.OtherPackageClass;
      2 
      3 import java.lang.reflect.AccessibleObject;
      4 import java.lang.reflect.Constructor;
      5 import java.lang.reflect.Field;
      6 import java.lang.reflect.InvocationTargetException;
      7 import java.lang.reflect.Method;
      8 import java.lang.reflect.Type;
      9 
     10 public class ClassAttrs {
     11     ClassAttrs() {
     12         /* local, not anonymous, not member */
     13         class ConsInnerNamed {
     14             public void showMe() {
     15                 printClassAttrs(this.getClass());
     16             }
     17         }
     18 
     19         ConsInnerNamed cinner = new ConsInnerNamed();
     20         cinner.showMe();
     21     }
     22 
     23     public static void main() {
     24         printClassAttrs(ClassAttrs.class);
     25         printClassAttrs(OtherClass.class);
     26         printClassAttrs(OtherPackageClass.class);
     27 
     28         /* local, not anonymous, not member */
     29         class InnerNamed {
     30             public void showMe() {
     31                 printClassAttrs(this.getClass());
     32             }
     33         }
     34         InnerNamed inner = new InnerNamed();
     35         inner.showMe();
     36 
     37         ClassAttrs attrs = new ClassAttrs();
     38 
     39         /* anonymous, not local, not member */
     40         printClassAttrs((new OtherClass() { int i = 5; }).getClass());
     41 
     42         /* member, not anonymous, not local */
     43         printClassAttrs(MemberClass.class);
     44 
     45         try {
     46             Constructor cons;
     47             cons = MemberClass.class.getConstructor(
     48                     new Class[] { MemberClass.class });
     49             System.out.println("constructor signature: "
     50                     + getSignatureAttribute(cons));
     51 
     52             Method meth;
     53             meth = MemberClass.class.getMethod("foo", (Class[]) null);
     54             System.out.println("method signature: "
     55                     + getSignatureAttribute(meth));
     56 
     57             Field field;
     58             field = MemberClass.class.getField("mWha");
     59             System.out.println("field signature: "
     60                     + getSignatureAttribute(field));
     61         } catch (NoSuchMethodException nsme) {
     62             System.err.println("FAILED: " + nsme);
     63         } catch (NoSuchFieldException nsfe) {
     64             System.err.println("FAILED: " + nsfe);
     65         } catch (RuntimeException re) {
     66             System.err.println("FAILED: " + re);
     67             re.printStackTrace();
     68         }
     69     }
     70 
     71     /* to call the (out-of-scope) <code>getSignatureAttribute</code> methods */
     72     public static String getSignatureAttribute(Object obj) {
     73         Method method;
     74         try {
     75             if (obj instanceof AccessibleObject) {
     76                 method = AccessibleObject.class.getDeclaredMethod(
     77                         "getSignatureAttribute");
     78             } else {
     79                 // Should be a Class.
     80                 method = Class.class.getDeclaredMethod(
     81                         "getSignatureAttribute");
     82             }
     83             method.setAccessible(true);
     84         } catch (NoSuchMethodException ex) {
     85             System.err.println("getSignatureAttribute() not defined.");
     86             ex.printStackTrace();
     87             return "<unknown>";
     88         }
     89 
     90         try {
     91             return (String) method.invoke(obj);
     92         } catch (IllegalAccessException ex) {
     93             throw new RuntimeException(ex);
     94         } catch (InvocationTargetException ex) {
     95             throw new RuntimeException(ex);
     96         }
     97     }
     98 
     99     /* for reflection testing */
    100     static class MemberClass<XYZ> {
    101         public MemberClass<XYZ> mWha;
    102 
    103         public MemberClass(MemberClass<XYZ> memb) {
    104             mWha = memb;
    105         }
    106 
    107         public Class<XYZ> foo() throws NoSuchMethodException {
    108             return null;
    109         }
    110     }
    111 
    112     /* for reflection testing (getClasses vs getDeclaredClasses) */
    113     static public class PublicMemberClass {
    114         float mBlah;
    115     }
    116 
    117     /*
    118      * Dump a variety of class attributes.
    119      */
    120     public static void printClassAttrs(Class clazz) {
    121         final boolean WORKING = false;
    122         Class clazz2;
    123 
    124         System.out.println("***** " + clazz + ":");
    125 
    126         System.out.println("  name: "
    127             + clazz.getName());
    128         System.out.println("  canonical: "
    129             + clazz.getCanonicalName());
    130         System.out.println("  simple: "
    131             + clazz.getSimpleName());
    132         System.out.println("  genericSignature: "
    133             + getSignatureAttribute(clazz));
    134 
    135         System.out.println("  super: "
    136             + clazz.getSuperclass());
    137         if (WORKING) System.out.println("  genericSuperclass: "
    138             + clazz.getGenericSuperclass());
    139         System.out.println("  declaring: "
    140             + clazz.getDeclaringClass());
    141         System.out.println("  enclosing: "
    142             + clazz.getEnclosingClass());
    143         System.out.println("  enclosingCon: "
    144             + clazz.getEnclosingConstructor());
    145         System.out.println("  enclosingMeth: "
    146             + clazz.getEnclosingMethod());
    147         System.out.println("  modifiers: "
    148             + clazz.getModifiers());
    149         System.out.println("  package: "
    150             + clazz.getPackage());
    151 
    152         System.out.println("  declaredClasses: "
    153             + stringifyTypeArray(clazz.getDeclaredClasses()));
    154         System.out.println("  member classes: "
    155             + stringifyTypeArray(clazz.getClasses()));
    156 
    157         System.out.println("  isAnnotation: "
    158             + clazz.isAnnotation());
    159         System.out.println("  isAnonymous: "
    160             + clazz.isAnonymousClass());
    161         System.out.println("  isArray: "
    162             + clazz.isArray());
    163         System.out.println("  isEnum: "
    164             + clazz.isEnum());
    165         System.out.println("  isInterface: "
    166             + clazz.isInterface());
    167         System.out.println("  isLocalClass: "
    168             + clazz.isLocalClass());
    169         System.out.println("  isMemberClass: "
    170             + clazz.isMemberClass());
    171         System.out.println("  isPrimitive: "
    172             + clazz.isPrimitive());
    173         System.out.println("  isSynthetic: "
    174             + clazz.isSynthetic());
    175 
    176         if (WORKING) System.out.println("  genericInterfaces: "
    177             + stringifyTypeArray(clazz.getGenericInterfaces()));
    178     }
    179 
    180     /*
    181      * Convert an array of Type into a string.  Start with an array count.
    182      */
    183     private static String stringifyTypeArray(Type[] types) {
    184         StringBuilder stb = new StringBuilder();
    185         boolean first = true;
    186 
    187         stb.append("[" + types.length + "]");
    188 
    189         for (Type t: types) {
    190             if (first) {
    191                 stb.append(" ");
    192                 first = false;
    193             } else {
    194                 stb.append(", ");
    195             }
    196             stb.append(t.toString());
    197         }
    198 
    199         return stb.toString();
    200     }
    201 }
    202