|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.beans.Utility
public class Utility
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).
| 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 |
|---|
public Utility()
| Method Detail |
|---|
public static void main(String[] args)
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
args - Array of command line arguments.
public static void printBeanInfo(BeanInfo bi,
PrintWriter pw)
main(java.lang.String[]) method to
display information on the class specified on the command line.
bi - BeanInfo object which has information about the class.pw - PrintWriter object to dump the info to.main(java.lang.String[])
public static final void invokeMethods(Object[] results,
Object obj,
Method[] methods)
throws IllegalAccessException,
InvocationTargetException
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.
IllegalAccessException
InvocationTargetException - If you tried to invoke a method which required parameters, or
you weren't allowed access.getMethods(java.lang.Object, java.lang.String[])
public static Method[] getMethods(Object o,
String[] names)
throws NoSuchMethodException
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).
NoSuchMethodException - If you request a method which required parameters, or
did not exist.invokeMethods(java.lang.Object[], java.lang.Object, java.lang.reflect.Method[]),
formatObjects(java.text.MessageFormat, java.lang.Object[], java.lang.String[])
public static StringBuffer formatObjects(MessageFormat fmt,
Object[] objs,
String[] methNames)
throws NoSuchMethodException,
IllegalAccessException,
InvocationTargetException
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.
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.
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.getMethods(java.lang.Object, java.lang.String[])
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||