Reflection in Java

/* The Reflection Test Class used to realize the functions of
   java.lang.reflect, referrenced by Corejava Volume I
   Display the fields, constructors, and methods, as well as annotations
   and exceptions respectively
   by kingmmxtj Nov 20, 2008
  
http://hi.baidu.com/kingmmxtj
*/

import java.util.*;
import java.lang.reflect.*;
import java.text.*;
import java.lang.annotation.Annotation; // pay attention to differ the java.text.annotation

public class ClassAnalyzer
{
    public static void main(String[] args)
    {
         // read the class u input
         String name;
         if(args.length > 0)
                name = args[0];
         else
         {
                System.out.println(“Enter one class naem(e.g. java.uitl.Date): “);
                Scanner in = new Scanner(System.in);
                name = in.next();
         }
  
    try
    {
           // print class name and super class name
           Class cl = Class.forName(name);
           Class supercl = cl.getSuperclass();
           System.out.print(“Class ” + name);
           if(supercl != null) // if(supercl != null && supercl != Object.class)
           System.out.print(” extends ” + supercl.getName());
           System.out.print(“n{n”);
          // leave 4 blank at the beginning
           System.out.println(”    ****** Fields of the Class ******”);
            printFields(cl);
           System.out.println();
           System.out.println(”    ****** Constructors of the Class ******”);
            printConstructors(cl);
           System.out.println();
          System.out.println(”    ****** Methods of the Class ******”);
            printMethods(cl);
           System.out.println(“}”);
     }
     catch(ClassNotFoundException e)
      {
              e.printStackTrace();
     }
   }
   
/* print all fields in the class
   @param cl a class
*/
     public static void printFields(Class cl)
     {
               Field[] fields = cl.getDeclaredFields();
     
               for(Field f : fields)
              {
                       Class type = f.getType();
                       String name = f.getName();
                       System.out.print(”    ” + Modifier.toString(f.getModifiers()));
                        System.out.println(” ” + type.getName() + ” ” + name + “;”);
                      // print all the annotations in the field
                       Annotation[] annote = f.getDeclaredAnnotations();
                        for(int i = 0; i < annote.length; i++)
                       {
                                 System.out.println(”    // ” + annote[i].toString());
                       }
                }
      }
    
/* print all constructors in the class
   @param cl a class
*/
    public static void printConstructors(Class cl)
     {
                 Constructor[] constructors = cl.getDeclaredConstructors();
     
               for(Constructor c : constructors)
               {
                        String name = c.getName();
                        System.out.print(”    ” + Modifier.toString(c.getModifiers()));
                        System.out.print(” ” + name + “(“);
       
                        // print parameter types
                        Class[] paramTypes = c.getParameterTypes();
                        for(int j = 0; j < paramTypes.length; j++)
                        {
                                 if(j > 0)
                                         System.out.print(“, “);
                                 System.out.print(paramTypes[j].getName());
                         }
                        System.out.print(“)”);
                        // print the Exceptions in the constructor
                         Class[] ex = c.getExceptionTypes();
                        if(ex.length == 0)
                                      System.out.println(“;”);
                         else
                       {
                                   System.out.print(” throws “);
                                   for(int j = 0; j < ex.length; j++)
                                  {
                                               if(j > 0)
                                                         System.out.print(“, “);
                                                System.out.print(ex[j].getName());
                                 }
                                  System.out.println(“;”);
                       }
                       // print all the annotations in the constructor
                      Annotation[] annote = c.getDeclaredAnnotations();
                        for(int i = 0; i < annote.length; i++)
                       {
                               System.out.println(”    // ” + annote[i].toString());
                       }
                 }
     }
    
/* print all methods in the class
   @param cl a class
*/
      public static void printMethods(Class cl)
      {
                Method[] methods = cl.getDeclaredMethods();
      
                for(Method m : methods)
               {
                         Class retType = m.getReturnType();
                         String name = m.getName();
       
                       // print modifiers, return value and method name
                        System.out.print(”    ” + Modifier.toString(m.getModifiers()));
                        System.out.print(” ” + retType.getName() + ” ” + name + “(“);
       
                       // print parameter types
                       Class[] paramTypes = m.getParameterTypes();
                        for(int j = 0; j < paramTypes.length; j++)
                       {
                               if(j > 0)
                                        System.out.print(“, “);
                               System.out.print(paramTypes[j].getName());
                     }
                       System.out.print(“)”);
                      // print the Exceptions in the method
                       Class[] ex = m.getExceptionTypes();
                      if(ex.length == 0)
                                     System.out.println(“;”);
                        else
                     {
                                   System.out.print(” throws “);
                                   for(int j = 0; j < ex.length; j++)
                                  {
                                            if(j > 0)
                                                      System.out.print(“, “);
                                            System.out.print(ex[j].getName());
                                    }
                                     System.out.println(“;”);
                     }
                    // print all the annotations in the method
                     Annotation[] annote = m.getDeclaredAnnotations();
                      for(int i = 0; i < annote.length; i++)
                   {
                              System.out.println(“    // ” + annote[i].toString());
                     }
             }
      }
}  

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Dave's Tools and tagged , . Bookmark the permalink.

One Response to Reflection in Java

  1. Whats up. I actually would like to leave a quick remark and inform you know that in fact I’ve been pursuing your web page for quite some time. Keep up the very extraordinary efforts and I will be looking back another time relatively soon.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s