How to use the reflection capabilities of Java in your code.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on coding Java for SilverStream applications in the Programmer's Guide |
Reflection allows Java code to obtain information about the fields, methods, and constructors of specified classes, and to use those reflected members to operate on their underlying counterparts in objects (within security restrictions). In the example, there are three general cases in which the reflective information on a selected class is displayed:
The following code includes three methods. Each method shows a particular manner of getting the name of the class for which reflective information is required.
private void handle_listClasses_valueChanged(AgoPropertyChangeEvent evt) { String sClassList; sClassList = (String) listClasses.getValue (); if (sClassList != null) { fldClassName.setText(sClassList); } } private void handle_rbMethods_valueChanged(AgoPropertyChangeEvent evt) { getClassInfo((String)listClasses.getValue()); } private void handle_tbShowSuper_actionPerformed(ActionEvent evt) { String sSuperClass = fldSuperClass.getText (); getClassInfo (sSuperClass); }
.getValue()
returns the class that is selected. Cast it to a String and call getClassInfo()
, supplying the String as the parameter. If the String is not null, getClassInfo()
gets the reflective information of the class whose name is supplied as the parameter.
fldSuperClass.getText()
gets the name of the selected class's super class. Call getClassInfo()
and supply this superclass name as the parameter to get reflective information on the superclass, such as the superclass's methods, constructors, and so on.
The following code shows part of the getClassInfo(String psClassName) method.
This line of code is from the getClassInfo()
method. It uses forName()
to get the Class associated with the supplied class name.
Class className = Class.forName (psClassName);
The following code checks if the user has selected the Methods option button. If so, it gets all the declared methods of the class as an array of Method objects, concatenates their String representations, and later displays the result in a text area. (See Displaying the class methods, properties, and constructors for display information.)
//This code is a fragment of the getClassInfo() method. if (rbMethods.getState()) { // The user wants a list of methods. // Get the method array, then loop through and add them to // the display string Method [] methods = className.getDeclaredMethods(); lblDisplay.setText("Methods"); for (int i = 0; i < methods.length; i++) { sDisplayList = sDisplayList + methods[i] + "\n"; } }
rbMethods.getState()
gets the state of the Methods option button (chkMethods
). If it returns true, the button is pressed.
className.getDeclaredMethods()
returns all the declared methods of the class as an array of Method objects.
lblDisplay.setText()
initially sets the text of the label to Methods. This text will later be extended to include the name of the class whose methods are displayed.
The following code checks if the user has selected the Properties option button. If so, it gets all the fields declared by the class as an array of Field objects, concatenates their String representations, and later displays the result in lblDisplay. (See Displaying the class methods, properties, and constructors for display information.)
//This code is a fragment of the getClassInfo() method. else if (rbProperties.getState()) { // The user wants a list of properties. // Get the field array, then loop through and // add them to the display string Field [] fields = className.getDeclaredFields (); lblDisplay.setText("Properties"); for (int i = 0; i < fields.length; i++) { sDisplayList = sDisplayList + fields[i] + "\n"; } }
className.getDeclaredFields()
returns all the fields of the class as an array of Field objects.
lblDisplay.setText()
initially sets the text of the label to Properties. This text will later be extended to include the name of the class whose properties are displayed.
The following code checks if the user has selected the Constructors option button. If so, it gets all the class's constructors as an array of Constructor objects, concatenates their String representations, and later displays the result in a text area. (See Displaying the class methods, properties, and constructors for display information.)
//This code is a fragment of the getClassInfo(String psClasname) method. else if (chkConstructors.getState()) { // The user wants a list of Constructors // Get the constructor array, then loop through and // add them to the display string Constructor [] constructors = className.getConstructors(); lblDisplay.setText("Constructors"); for (int i = 0; i < constructors.length; i++) { sDisplayList = sDisplayList + constructors[i] + "\n"; } }
className.getConstructors()
returns all the constructors of the class as an array of Constructor objects.
lblDisplay.setText()
initially sets the text of the label to "Constructors". This text will later be extended to include the name of the class whose constructors are displayed.
The following code displays the methods (or properties or constructors) for the selected class in the class list. It also sets the name of the class in the corresponding label to indicate the "owner" of the displayed methods.
//This code is a fragment of the getClassInfo() method. // Set the display area to the newly constructed String and // update the label to show the class we're reflecting. taDisplay.setText(sDisplayList); lblDisplay.setText(lblDisplay.getText() + " for Class: " + (String)listClasses.getValue());
taDisplay.setText(sDisplayList)
displays the String that is the concatenation (with newlines) of all the names of the methods (or properties or constructors) of the selected class. sDisplayList contains the String to display in the taDisplay text area.
The following code uses getSuperclass()
to get the a class's superclass. It then displays this superclass's name in a text field.
//This code is a fragment of the getClassInfo(String psClasname) method. Class c = className.getSuperclass(); if (c != null) fldSuperClass.setText(c.getName());
getSuperclass()
returns the superclass of the object that calls this method.
getName()
returns the name of the superclass as a text String.
fldSuperClass.setText()
displays the superclass name in the text field.