HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Reflection and Introspection: Objects Exposed"
Vol. 3, Issue 5, p. 24

	

Listing 1: Cookie.java
  
// Cookie interface and derived classes  

interface Cookie {  
}  

class FortuneCookie implements Cookie{  
  public static Cookie newCookie(String str)  
  {  
    System.out.println("Fortunate " + str);  
    return new FortuneCookie();  
  }  
}  

class MisFortuneCookie implements Cookie{  
  public static Cookie newCookie(String str)  
  {  
    System.out.println("UnFortunate " + str);  
    return new MisFortuneCookie();  
  }  
}  

Listing 2: CookieFactory.java
  
import java.lang.*;  
import java.lang.reflect.*;  

public class CookieFactory {  

  // Instantiate a Cookie represented by the  
  // input String parameter.  
  public Cookie createCookie(String className)  
  {  
    // Get the class corresponding from String  
    Class c = null;  
    try {  
       c = Class.forName(className);  
    }  
     catch (ClassNotFoundException cnfe) {  
      cnfe.printStackTrace();  
    }  

    // Parameters for the "printCookie()" method  
    // of the Cookie interface.  
    Class pTypes[] = new Class[1];  
    pTypes[0] = String.class;  

    // Get "printCookie()" method for Cookie class  
    Method method = null;  
    try {  
      method = c.getMethod("newCookie", pTypes);  
    }  
    catch (NoSuchMethodException nsme) {  
      nsme.printStackTrace();  
    }  

    // Invoke "printCookie()" method to get Cookie  
    Cookie cookie = null;  
    try {  
      Object[] params = new Object[1];  
      params[0] = "Cookie";  

      cookie = (Cookie)(method.invoke(c, params));  
    }  
    catch (InvocationTargetException ite) {  
      ite.getTargetException().printStackTrace();  
    }  
    catch (IllegalAccessException iae) {  
      iae.printStackTrace();  
    }  
    return cookie;  
  }  

  // Simple test for the Cookie Factory  
  public static void main(String[] args)  
  {  
    CookieFactory cf = new CookieFactory();  
    Cookie fc = cf.createCookie("FortuneCookie");  
    Cookie mfc = cf.createCookie("MisFortuneCookie");  
  }  
}  

Listing 3: XRayBean.java
  
import java.lang.reflect.*;  
import java.beans.*;  
import java.util.*;  

public class XRayBean {  

  public Method[]  
    getMethodsFromBean(BeanInfo bi) {  

    Method[]           mList;  
    MethodDescriptor[] mDescriptorList;  

    mDescriptorList = bi.getMethodDescriptors();  
    mList = new Method[mDescriptorList.length];  

    for (int i = 0; i < mDescriptorList.length; i++) {  
      mList[i] = mDescriptorList[i].getMethod();  
    }  
    return mList;  
  }  

  public PropertyDescriptor[]  
    getPropsFromBean(BeanInfo bi) {  

    PropertyDescriptor[] pDescriptorList;  

    pDescriptorList =  
         bi.getPropertyDescriptors();  
    return pDescriptorList;  
  }  

  public EventSetDescriptor[]  
    getEventsFromBean(BeanInfo bi) {  

    EventSetDescriptor[] eDescriptorList;  

    eDescriptorList =  
        bi.getEventSetDescriptors();  
    return eDescriptorList;  
  }  

  public Method[]  
    getListenerMethods(EventSetDescriptor esd) {  

    Method mList[];  

    mList = esd.getListenerMethods();  
    return mList;  
  }  

  public static void main(String[] args) {  
    XRayBean xray = new XRayBean();  

    if (args.length != 2) {  
      System.out.println("Usage: java XRayClass " +  
       "<className> [methods | properties | events]");  
      System.exit(1);  
    }  

    try {  
      Class c     = Class.forName(args[0]);  
      BeanInfo bi = Introspector.getBeanInfo(c);  

      if (args[1].equals("methods")) {  
        Method[] mList = xray.getMethodsFromBean(bi);  

        for (int i = 0; i < mList.length; i++) {  
       System.out.println("Method[" + i +  
                          "]: " + mList[i]);  
        }  
      }  
      else if (args[1].equals("properties")) {  
     PropertyDescriptor[] pList =  
           xray.getPropsFromBean(bi);  

        for (int i = 0; i < pList.length; i++) {  
          System.out.println(  
           "---------------------------------");  
          System.out.println("Property Name: " +  
                             pList[i].getName());  
       System.out.println("Read  Method[" +  
            i + "]: " + pList[i].getReadMethod());  
       System.out.println("Write Method[" +  
         i + "]: " + pList[i].getWriteMethod());  
     }  
      }  
      else if (args[1].equals("events")) {  
     EventSetDescriptor[] eList =  
                xray.getEventsFromBean(bi);  

        for (int i = 0; i < eList.length; i++) {  
          System.out.println(  
            "--------------------------------");  
          System.out.println("Event Name: " +  
                            eList[i].getName());  

       Method mList[] =  
         xray.getListenerMethods(eList[i]);  

          for (int j = 0; j < mList.length; j++) {  
         System.out.println("Listener Method[" +  
                       j + "]: " + mList[j]);  
          }  
     }  
      }  
      else {  
        System.out.println("ERROR:" +  
         "The selected feature is not implemented");  
      }  
    }  
    catch (java.lang.ClassNotFoundException e) {  
      e.printStackTrace();  
    }  
    catch (java.beans.IntrospectionException e) {  
      e.printStackTrace();  
    }  
  }  
}  

Listing 4: XRayClass.java
  
import java.lang.reflect.*;  
import java.util.*;  

public class XRayClass {  

  public Method[]  
    getLocalMethodsFromClass(Class c) {  

    Method[] mList;  

    mList = c.getDeclaredMethods();  
    return mList;  
  }  

  public Method[]  
    getAllMethodsFromClass(Class c) {  

    Method[] mList;  

    mList = c.getMethods();  
    return mList;  
  }  

  public Constructor[]  
    getAllConstructorsFromClass(Class c) {  

    Constructor[] cList;  

    cList = c.getDeclaredConstructors();  
    return cList;  
  }  

  public Field[] getFieldsFromClass(Class c) {  
    Field[] fList;  

    fList = c.getDeclaredFields();  
    return fList;  
  }  

  public Class[] getClassesFromClass(Class c) {  
    Vector cList = new Vector();  
    Class  cTemp;  
    Class[] aList;  

    cTemp = c;  
    while ((cTemp =  
            cTemp.getSuperclass()) != null) {  
      cList.addElement(cTemp);  
    }  

    aList = new Class[cList.size()];  
    for (int i = 0; i < cList.size(); i++) {  
      aList[i] = (Class) cList.elementAt(i);  
    }  
    return aList;  
  }  

  public Class[]  
    getInterfacesFromClass(Class c) {  

    Class[] cList;  

    cList = c.getInterfaces();  
    return cList;  
  }  

  public static void main(String[] args) {  

    XRayClass xray = new XRayClass();  

    if (args.length != 2) {  
      System.out.println("Usage: " +  
        "java XRayClass <className>" +  
        "[localMethods | allMethods | " +  
        "Constructors | fields | inheritance |" +  
        "interfaces] ");  
      System.exit(1);  
    }  

    try {  
      Class c = Class.forName(args[0]);  

      if (args[1].equals("localMethods")) {  
        Method[] mList =  
          xray.getLocalMethodsFromClass(c);  

        for (int i = 0; i < mList.length; i++) {  
       System.out.println("Method[" + i +  
                         "]: " + mList[i]);  
     }  
      }  
      else if (args[1].equals("allMethods")) {  
     Method[] mList =  
       xray.getAllMethodsFromClass(c);  

        for (int i = 0; i < mList.length; i++) {  
       System.out.println("Method[" + i +  
                        "]: " + mList[i]);  
     }  
      }  
      else if (args[1].equals("Constructors")) {  
     Constructor[] cList =  
       xray.getAllConstructorsFromClass(c);  

        for (int i = 0; i < cList.length; i++) {  
       System.out.println("Constructor[" + i + "]: " + cList[i]);  
     }  
      }  
      else if (args[1].equals("fields")) {  
        Field[] fList =  
          xray.getFieldsFromClass(c);  

        for (int i = 0; i < fList.length; i++) {  
       System.out.println("Field[" + i +  
                          "]: " + fList[i]);  
     }  
      }  
      else if (args[1].equals("inheritance")) {  
        Class[] cList =  
          xray.getClassesFromClass(c);  

        for (int i = 0; i < cList.length; i++) {  
       System.out.println("Class[" + i +  
                          "]: " + cList[i]);  
     }  
      }  
      else if (args[1].equals("interfaces")) {  
        Class[] cList =  
          xray.getInterfacesFromClass(c);  

        for (int i = 0; i < cList.length; i++) {  
          System.out.println("Interface[" + i +  
                             "]: " + cList[i]);  
     }  
      }  
      else {  
     System.out.println("ERROR:" +  
       "The selected feature is not implemented");  
      }  
    }  
    catch (java.lang.ClassNotFoundException e) {  
      e.printStackTrace();  
    }
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.