Accessing the Declared Fields of a Class

   

With constructors and methods covered, that leaves fields as the major remaining aspect of class Reflection. As you might have guessed, the methods used in this endeavor are getDeclaredFields , getDeclaredField, getFields, and getField. This section focuses on getDeclaredFields and getDeclaredField. The other two forms differ from these only in that they reflect superclasses and superinterfaces, and return only public fields.

In the previous Provider class, there was already a field named attribute, so you can modify Requestor slightly to access it, as shown in Listing 28.11.

Listing 28.11 Requestor Application That Gets Fields
 /*  * Requestor  */ import java.lang.reflect.*; public class Requestor {   public void requestConstuctors() {     try {       // load the Provider class and use Reflection       // to get an array of constructor information       Class provClass = Class.forName("Provider");       Constructor con[] = provClass.getDeclaredConstructors();       for (int x=0; x<con.length; x++)         System.out.println("Constructor " + x + " = " + con[x]);       // get an array of Provider method information       Method meth[] = provClass.getDeclaredMethods();       for (int x=0; x<meth.length; x++)         System.out.println("Method " + x + " = " + meth[x]);       // get an array of Provider field information       Field field[] = provClass.getDeclaredFields();       for (int x=0; x<field.length; x++)         System.out.println("Field " + x + " = " + field[x]);     }     catch (ClassNotFoundException ce) {       // Class.forName was unable to load Provider.class       System.out.println("Could not locate class");     }     catch (SecurityException se) {       // Reflection permission was not granted       System.out.println("Not allowed to get class info");     }   }   public static void main(String args[]) {     // construct a Requestor and ask it for the     // constructors, methods, and fields declared by Provider     Requestor req = new Requestor();     req.requestConstuctors();   } } /*  *  Provider  */ class Provider {   String attribute;   // construct with a String to assign to the attribute   public Provider(String s) {     attribute = s;   }   // construct with an int to assign to the attribute   private Provider(int i) {     attribute = String.valueOf(i);   }   // set the attribute String   public void setAttribute(String attribute) {     this.attribute = attribute;   }   // get the attribute String   public String getAttribute() {     return attribute;   } } 

The resulting output now includes an entry for the String field attribute:

 Constructor 0 = private Provider(int) Constructor 1 = public Provider(java.lang.String) Method 0 = public void Provider.setAttribute(java.lang.String) Method 1 = public java.lang.String Provider.getAttribute() Field 0 = java.lang.String Provider.attribute 

An additional use of Field is that it provides a number of accessor and mutator methods that you can use to query the value of a field or set it. These methods all accept an Object parameter that specifies the class instance to which the operations are applied.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net