Application Techniques



Using Reflection

How to use the reflection capabilities of Java in your code.

About this technique

Details

Category

Core Programming Techniques> Java

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

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:

Getting the name of a selected class   Top of page

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); 
    } 
   

Notes about the code

Getting a class   Top of page

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); 

Getting the class methods   Top of page

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"; 
          } 
        } 

Notes about the code

Getting the class properties   Top of page

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"; 
          } 
        } 

Notes about the code

Getting the constructors   Top of page

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"; 
        } 
     } 

Notes about the code

Displaying the class methods, properties, and constructors   Top of page

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()); 

Notes about the code

Getting a superclass and displaying its name   Top of page

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()); 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.