CS3340:   Intro OOP and Design

 

Reflection

  • the process by which a computer program can observe (do type introspection) and modify its own structure and behavior at runtime.
  • allows your software to gain access to metadata at runtime

 

What could you do with this?

  • Make your code discover what methods and fields a class that is "delivered" (possibly across a network) to your program.
  • You could then invoke those methods
  • Could you create a subclass that alters the methods ---dynamic code building....could you creating self-modifying applications?

Java and Reflection ---SIMPLE example

   import java.lang.reflect.*;       
    public class DumpMethods {        
        public static void main(String args[])        {           
             try {
              Class c = Class.forName(args[0]);
              Method m[] = c.getDeclaredMethods();
              for (int i = 0; i < m.length; i++)
                System.out.println(m[i].toString());
             }
             catch (Throwable e) {
              System.err.println(e);           }        
         }
     }  

For an invocation of:

  java DumpMethods java.util.Stack  

the output is:

  public java.lang.Object java.util.Stack.push(java.lang.Object)     
  public synchronized java.lang.Object java.util.Stack.pop()
  public synchronized java.lang.Object java.util.Stack.peek()
  public boolean java.util.Stack.empty()
  public synchronized int java.util.Stack.search(java.lang.Object)  

 

 

Setting Up to Use Reflection

The reflection classes, such as Method, are found in java.lang.reflect. There are three steps that must be followed to use these classes. The first step is to obtain a java.lang.Class object for the class that you want to manipulate. java.lang.Class is used to represent classes and interfaces in a running Java program.

Finding Out About Class Fields

It's also possible to find out which data fields are defined in a class. To do this, the following code can be used:

     import java.lang.reflect.*;
     public class field1 {
        private double d;
        public static final int i = 37;
        String s = "testing";
        public static void main(String args[])        {
           try {
              Class cls = Class.forName("field1");
              Field fieldlist[]  = cls.getDeclaredFields();
              for (int i=0; i < fieldlist.length; i++) {
                 Field fld = fieldlist[i];
                 System.out.println("name = " + fld.getName());
                 System.out.println("decl class = " + fld.getDeclaringClass());
                 System.out.println("type = " + fld.getType());
                 int mod = fld.getModifiers();
                 System.out.println("modifiers = " + Modifier.toString(mod));
                 System.out.println("-----");
              }
            }
            catch (Throwable e) {
               System.err.println(e);            }
           }
        }  

This example is similar to the previous ones. One new feature is the use of Modifier. This is a reflection class that represents the modifiers found on a field member, for example "private int". The modifiers themselves are represented by an integer, and Modifier.toString is used to return a string representation in the "official" declaration order (such as "static" before "final"). The output of the program is:
  name = d     
     decl class = class field1
     type = double
     modifiers = private
     -----
     name = i
     decl class = class field1
     type = int
     modifiers = public static final
     -----
     name = s
     decl class = class field1
     type = class java.lang.String
     modifiers =     
     -----   


As with methods, it's possible to obtain information about just the fields declared in a class (getDeclaredFields), or to also get information about fields defined in superclasses (getFields).

 

 

Invoking Methods by Name

So far the examples that have been presented all relate to obtaining class information. But it's also possible to use reflection in other ways, for example to invoke a method of a specified name.

To see how this works, consider the following example:

   import java.lang.reflect.*;
   public class method2 {
        public int add(int a, int b)
        {     return a + b;        }
        public static void main(String args[])
        {
           try {
             Class cls = Class.forName("method2");
             Class partypes[] = new Class[2];
              partypes[0] = Integer.TYPE;
              partypes[1] = Integer.TYPE;
              Method meth = cls.getMethod( "add", partypes);
              method2 methobj = new method2();
              Object arglist[] = new Object[2];
              arglist[0] = new Integer(37);
              arglist[1] = new Integer(47);
              Object retobj = meth.invoke(methobj, arglist);
              Integer retval = (Integer)retobj;
              System.out.println(retval.intValue());
           }
           catch (Throwable e) {
              System.err.println(e);
           }
         }
     }  

Suppose that a program wants to invoke the add method, but doesn't know this until execution time. That is, the name of the method is specified during execution (this might be done by a JavaBeans development environment, for example). The above program shows a way of doing this.

getMethod is used to find a method in the class that has two integer parameter types and that has the appropriate name. Once this method has been found and captured into a Method object, it is invoked upon an object instance of the appropriate type. To invoke a method, a parameter list must be constructed, with the fundamental integer values 37 and 47 wrapped in Integer objects. The return value (84) is also wrapped in an Integer object.

Creating New Objects

There is no equivalent to method invocation for constructors, because invoking a constructor is equivalent to creating a new object (to be the most precise, creating a new object involves both memory allocation and object construction). So the nearest equivalent to the previous example is to say:

   import java.lang.reflect.*;
   public class constructor2 {
        public constructor2()
        {        }

        public constructor2(int a, int b)
        {  System.out.println("a = " + a + " b = " + b);    }

        public static void main(String args[])
        {
           try {
             Class cls = Class.forName("constructor2");
             Class partypes[] = new Class[2];
             partypes[0] = Integer.TYPE;
             partypes[1] = Integer.TYPE;
             Constructor ct  = cls.getConstructor(partypes);
             Object arglist[] = new Object[2];
             arglist[0] = new Integer(37);
             arglist[1] = new Integer(47);
             Object retobj = ct.newInstance(arglist);
           }
           catch (Throwable e) {
              System.err.println(e);
           }
        }
     }  

which finds a constructor that handles the specified parameter types and invokes it, to create a new instance of the object. The value of this approach is that it's purely dynamic, with constructor lookup and invocation at execution time, rather than at compilation time.

Changing Values of Fields

Another use of reflection is to change the values of data fields in objects. The value of this is again derived from the dynamic nature of reflection, where a field can be looked up by name in an executing program and then have its value changed. This is illustrated by the following example:

   import java.lang.reflect.*;
   public class field2 {
        public double d;

        public static void main(String args[])
        {
           try {
              Class cls = Class.forName("field2");
              Field fld = cls.getField("d");
              field2 f2obj = new field2();
              System.out.println("d = " + f2obj.d);
              fld.setDouble(f2obj, 12.34);
              System.out.println("d = " + f2obj.d);
           }
           catch (Throwable e) {
              System.err.println(e);
           }
        }
     }  

In this example, the d field has its value set to 12.34.

 

 

CLICK HERE FOR OTHER EXAMPLES

 

© Lynne Grewe