Training Index
Reflection and MethodDescriptors for NervousText
By Greg Voss, JavaSoft
[Tutorial Contents]
In this section the property editor and basic NervousText classes remain the
same (except name changes for version 08) while some changes are made to the
NervousText08BeanInfo class to reduce the number of methods exposed
to builder tools.
copy NervousText07.java to
NervousText08.java
copy NervousText07BeanInfo.java
to NervousText08BeanInfo.java
copy NervousText07TextPropertyEditor.java
to NervousText08TextPropertyEditor.java
|
Make the appropriate changes to the class names and debug messages stubs in
each file (changing NervousText07 to NervousText08
throughout each file).
Now for some reflection. You'll be making the following changes to
NervousText08BeanInfo.
To use the Method class, you'll need to add an import statement
at the top of the file.
import java.lang.reflect.Method;
As before, change the name displayed by the Bean inside builder tools. Inside
the getBeanDescriptor method NervousText08BeanInfo
change
bd.setDisplayName("Uneasy Text 07");
to
bd.setDisplayName("Uneasy Text 08");
You may have already caught this if you did a global search and replace,
changing 07 to 08.
The goal of the next set of changes is to reduce the number of event handler
methods discovered by a builder tool. You might write a dozen or more public
methods, but only want two of them to be exposed. The methods displayed are
limited to one ActionEvent event handler to respond to button
presses from other objects, and one PropertyChangeEvent event
handler. Recall the PropertyChangeEvent event was used to support
bound properties in an earlier version of NervousText so that changes to the
text could notify other interested objects.
This reduction in visible methods is achieved by adding a new method called
getMethodDescriptors whose structure resembles
getPropertyDescriptors. Instead of returning an array of
PropertyDescriptor, getMethodDescriptors returns
an array of MethodDescriptor objects. The basic skeleton is as
follows:
public MethodDescriptor[] getMethodDescriptors() {
...
MethodDescriptor result[] = { ... };
...
return result;
}
|
Of course there's a bit involved in setting up the array of MethodDescriptor
objects to be returned. You're going to return four methods in all: a
startMethod, a stopMethod and a
changeDirectionMethod to respond to ActionEvents from buttons, as
well as a propertyChangeMethod to respond to property change
notifications from other Beans.
Each of these is declared as
a local variable inside of
getMethodDescriptors:
public MethodDescriptor[] getMethodDescriptors() {
Method startMethod, stopMethod,
changeDirectionMethod;
Method propertyChangeMethod;
...
}
|
You'll be calling getMethod for the Class object automatically
associated with NervousText08. Recall from a previous section that
this Class object can be retrieved by direct reference:
NervousText08.class
So the call to get getMethod has the form
NervousText08.class.getMethod( ... );
Two arguments are required. The first is a String naming the method. The
second is an array of objects that are the Classes of the
arguments required by the method you are trying to look up. In other words,
the arguments to getMethod represent the signature of
the method you want to reference.
The start and stop methods don't take any arguments,
so the array of arguments passed to getMethod is empty.
Class args[] = { };
With this argument declared, you can now retrieve references to the first three
methods:
try {
startMethod = NervousText08.class.getMethod(
"start", args);
stopMethod = NervousText08.class.getMethod(
"stop", args);
|
The changeDirection method in NervousText08 takes
an ActionEvent as an argument. The second argument to
NervousText08.class.getMethod for this method must therefore be an
array of one element:
Class actionEventArgs[] = {
java.awt.event.ActionEvent.class };
With the array declared you can retrieve the method:
changeDirectionMethod =
NervousText08.class.getMethod(
"changeDirection", actionEventArgs);
The PropertyChangeEvent is set up in a similar manner:
Class propertyChangeEventArgs[] = {
PropertyChangeEvent.class };
...
propertyChangeMethod = NervousText08.class.getMethod(
"makeChange", propertyChangeEventArgs);
|
Now that you have all the Methods, you can build an array of
MethodDescriptor objects to be returned by
getMethodDescriptors.
// Now create the MethodDescriptor array
// with visible event response methods:
MethodDescriptor result[] = {
new MethodDescriptor(startMethod),
new MethodDescriptor(stopMethod),
new MethodDescriptor(changeDirectionMethod),
new MethodDescriptor(propertyChangeMethod)
};
|
That's all there is to it. Here's the entire method in one piece:
public MethodDescriptor[] getMethodDescriptors() {
System.err.println("ENTER--->
NervousText08BeanInfo.getMethodDescriptors");
// First find the "method" objects.
Method startMethod, stopMethod, changeDirectionMethod;
Method propertyChangeMethod;
Class args[] = { };
Class actionEventArgs[] = {
java.awt.event.ActionEvent.class };
Class propertyChangeEventArgs[] = {
PropertyChangeEvent.class };
try {
startMethod = NervousText08.class.getMethod(
"start", args);
stopMethod = NervousText08.class.getMethod(
"stop", args);
// ActionEvent handler methods
//FIND METHOD:
// public void changeDirection(ActionEvent x)
changeDirectionMethod =
NervousText08.class.getMethod(
"changeDirection", actionEventArgs);
// PropertyChangeEvent handler methods
// FIND METHOD public void makeChange (
// PropertyChangeEvent evt) {
propertyChangeMethod = NervousText08.class.getMethod(
"makeChange", propertyChangeEventArgs);
} catch (Exception ex) {
// "should never happen"
throw new Error("Missing method: " + ex);
}
// Now create the MethodDescriptor array
// with visible event response methods:
MethodDescriptor result[] = {
new MethodDescriptor(startMethod),
new MethodDescriptor(stopMethod),
new MethodDescriptor(changeDirectionMethod),
new MethodDescriptor(propertyChangeMethod)
};
System.err.println("EXIT---->
NervousText08BeanInfo.getMethodDescriptors");
return result;
}
|
Now, when you place NervousText08Bean in the BeanBox and try to hook up action
events from buttons, or property change events, the resulting hookup dialog will
only display the above four methods.
Important JDK classes used in this example:
-
Method
-
Class
-
MethodDescriptor
-
PropertyDescriptor
-
SimpleBeanInfo
NervousText08.java
NervousText08BeanInfo.java
NervousText08TextPropertyEditor.java
|