com.ccg.beans
Class Utility

java.lang.Object
  extended by com.ccg.beans.Utility

public class Utility
extends Object

General utility class of static functions to work with beans. This class contains static methods which one may find handy for working with Java classes (in particular bean type objects). Its main function is to allow one to simplify the process of dynamically invoking methods of beans at run time.

This class can also be run from the command line to dump information about a Java class (see main(java.lang.String[]) for details).

Since:
1.0
Version:
$Revision: 1.4 $
Author:
$Author: pkb $

Constructor Summary
Utility()
           
 
Method Summary
static StringBuffer formatObjects(MessageFormat fmt, Object[] objs, String[] methNames)
          Run time dynamic method to format objects.
static Method[] getMethods(Object o, String[] names)
          Fetch a set of named methods associated with an object/class.
static void invokeMethods(Object[] results, Object obj, Method[] methods)
          Invoke a set of methods (taking no parameters) on a object.
static void main(String[] args)
          Command line utility to display class methods.
static void printBeanInfo(BeanInfo bi, PrintWriter pw)
          Prints all of the methods of the class the BeanInfo object defines.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utility

public Utility()
Method Detail

main

public static void main(String[] args)
Command line utility to display class methods. Simple command line utility to dump information about a class specified on the command line. You can invoke this in the following manner:
 java com.ccg.beans.Utility -class CLASS
 

A list of methods for the class (along with return values, parameters and exceptions) will be returned. Go ahead, try it with something like:

 java com.ccg.beans.Utility -class java.lang.Object
 

You should see something like:

 public final native void java.lang.Object.notifyAll()
 public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
 public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
 public java.lang.String java.lang.Object.toString()
 public final native void java.lang.Object.notify()
 public boolean java.lang.Object.equals(java.lang.Object)
 public native int java.lang.Object.hashCode()
 public final native java.lang.Class java.lang.Object.getClass()
 public final void java.lang.Object.wait() throws java.lang.InterruptedException
 

Parameters:
args - Array of command line arguments.

printBeanInfo

public static void printBeanInfo(BeanInfo bi,
                                 PrintWriter pw)
Prints all of the methods of the class the BeanInfo object defines. You can pass a BeanInfo object to this class and get a dump of all of the methods (one per line) of the class the BeanInfo object refers to. This is used by the main(java.lang.String[]) method to display information on the class specified on the command line.

Parameters:
bi - BeanInfo object which has information about the class.
pw - PrintWriter object to dump the info to.
Since:
1.0
See Also:
main(java.lang.String[])

invokeMethods

public static final void invokeMethods(Object[] results,
                                       Object obj,
                                       Method[] methods)
                                throws IllegalAccessException,
                                       InvocationTargetException
Invoke a set of methods (taking no parameters) on a object. This class is used to invoke a set of methods (all of which must not require any parameters) on a particular object. This can be useful for generating dynamic argument lists to the java.text.MessageFormat method to create very dynamic run-time defined format capabilities.

Parameters:
results - Array to store results of the methods invoked. Must be at least as large in size as the methods array.
obj - Object to invoke the methods upon (must not be null).
methods - Array of Method objects to be invoked. All methods will be invoked on the passed object (with no arguments) and the corresponding results will be stored in the results array.
Throws:
IllegalAccessException
InvocationTargetException - If you tried to invoke a method which required parameters, or you weren't allowed access.
Since:
1.0
See Also:
getMethods(java.lang.Object, java.lang.String[])

getMethods

public static Method[] getMethods(Object o,
                                  String[] names)
                           throws NoSuchMethodException
Fetch a set of named methods associated with an object/class. This method allows one to fetch a set of named methods for a specific class. You may either pass a instance of the object you are interested in, or a java.lang.Class object which refers to the class of interest. You then also pass a array of method names you want to access. You may only request methods which have no parameters.

Parameters:
obj - Instance of the class (any Object), or a java.lang.Class which you want to fetch Method objects from.
names - Array of method names you want to fetch Method objects for (must not be null).
Returns:
Set of Method objects corresponding to your request.
Throws:
NoSuchMethodException - If you request a method which required parameters, or did not exist.
Since:
1.0
See Also:
invokeMethods(java.lang.Object[], java.lang.Object, java.lang.reflect.Method[]), formatObjects(java.text.MessageFormat, java.lang.Object[], java.lang.String[])

formatObjects

public static StringBuffer formatObjects(MessageFormat fmt,
                                         Object[] objs,
                                         String[] methNames)
                                  throws NoSuchMethodException,
                                         IllegalAccessException,
                                         InvocationTargetException
Run time dynamic method to format objects. This method allows one to take the java.text.MessageFormat one step further. It allows one to dynamically (at run time) define what the parameters will be by using method indirection. An example is probably the best way to understand this method:

Suppose you had a class like com.ccg.app.calc.Timelog which has a ton of accessor methods (methods which get information about the object). You could then use something like the following to format fields:

 public static String formatLD(LD[] data) {
   String[] fields = { "getDescription", "getBillTo", "getStartTime" };
   String fmt = "<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>\n";
   return com.ccg.beans.Utility.formatObjects(fmt,data,fields);
 }
 

The most important thing to note in the above example is the fields variable. It contains a set of Strings to the methods you want to use on the object. This level of indirection allows for very flexible runtime output.

Parameters:
fmt - Formatter for arguments fetched from each of the objects (see MessageFormat for details. Note, if you want new-lines in your output, be sure to include them in your format string.
objs - Array of objects to format (must not be null - though it is OK to have null entries in the array - they will be skipped over).
methodNames - Array of method names to invoke on the objects to form the parameter subsitution list passed to fmt.format(Object[]) for each object in the array of objects.
Returns:
StringBuffer containing your formatted output.
Throws:
IllegalAccessException
InvocationTargetException - If you tried to invoke a method which required parameters, or you weren't allowed access.
NoSuchMethodException - If you request a method which required parameters, or did not exist.
Since:
1.0
See Also:
getMethods(java.lang.Object, java.lang.String[])


Copyright 1998-1998-2006 null. All Rights Reserved.