SDSU CS 596 Java Programming
Fall Semester, 1998
Reflection
To Lecture Notes Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 21-Dec-98

Contents of Doc 35, Reflection


References


The Java Programming Language, 2 nd Ed., Arnold, Gosling, Addison-Wesley, 1998, pp. 303-314.

Java in a Nutshell, 2 nd Ed., Flanagan, O'Reilly, 1997, Chapter 12 (pp. 219-223)

On-line Java API Documentation

Listen Here!S-dec9 2mins Doc 35, Reflection Slide # 2

Reflection

Reflection refers to the ability of a program to inspect and manipulate itself. Java added reflection in JDK 1.1. The class java.lang.Class and the package java.lang.reflect (classes Array, Constructor, Field, Method, Modifier) implement Java's reflection. The methods of java.lang.Class are used to get information about a class. The methods of the classes in java.lang.reflect provide further information about a class and allow methods to be invoked.

java.lang.Class Methods
forName(String)
getClasses()
getClassLoader()
getComponentType()
getConstructor(Class[])
getConstructors()
getDeclaredClasses()
getDeclaredConstructor(Class[])
getDeclaredConstructors()
getDeclaredField(String)
getDeclaredFields()
getDeclaredMethod(String, Class[])
getDeclaredMethods()
getDeclaringClass()
getField(String)
getFields()
getInterfaces()
getMethod(String, Class[])
getMethods()
getModifiers()
getName()
getResource(String)
getResourceAsStream(String)
getSigners()
getSuperclass()
isArray()
isAssignableFrom(Class)
isInstance(Object)
isInterface()
isPrimitive()
newInstance()
toString()

Listen Here!S-dec9 3mins Doc 35, Reflection Slide # 3
Sample Class
The following class will be used in examples in this document.

public class Sample {
   static private int privateInt = 1;
   protected float protectedField = 1.2F;
   int[] data = { 4, 3, 2, 1 };
   public String message = "Hi mom";
   
   public Sample() {
      System.out.println( "Default Constructor" );
   }
   
   public Sample( String newMessage ) {
      System.out.println( "One arg Constructor"  );
      message = newMessage;
   }
   
   public final void setData( int[] newData ) {
      System.out.println( "Set data" );
      data = newData;
   }
   
   public void setFields( int anInt, float aFloat ) {
      System.out.println( "Set fields" );
      privateInt = anInt;
      protectedField = aFloat;
   }
   
   public String toString() {
      return "Sample(" + privateInt + ", " + protectedField + ", " + message +")";
   }
}

Doc 35, Reflection Slide # 4

Obtaining Class Objects


There are a number of ways to get a Class object for a class: Class.forName(), class literals (.class - class is a keyword in java), and Object.getClass(). With forName() the full class name must be used. Import statements do not affect forName(). With class literals, one can either use the full class name or use import statements. The class literal is faster than using forName(). However, forName() uses a string, which can be supplied dynamically.

public class Test  {
   public static void main( String args[] ) 
         throws ClassNotFoundException {
      Class aVectorClass = java.util.Vector.class;
      Class bVectorClass = Class.forName( "java.util.Vector" );
      
      Vector aVector = new Vector();
      Class cVectorClass = aVector.getClass();
      System.out.println( cVectorClass.getName() );
      
      Class sampleClass = Sample.class;
   }
Output
java.util.Vector

Doc 35, Reflection Slide # 5

Information about a Class

The following example shows how to get information about the fields of a class.
import java.lang.reflect.*;

public class Test  {
   public static void main( String args[] )  
         throws ClassNotFoundException {
      Class sampleClass = Sample.class;
      
      Field[] sampleFields = sampleClass.getDeclaredFields();
      
      System.out.println( "Field name, type, modifiers" );
      for ( int k = 0; k < sampleFields.length; k++ ) {
         String name = sampleFields[k].getName();
         Class type = sampleFields[k].getType();
         int modifiers = sampleFields[k].getModifiers();
         if ( type.isArray() )
            System.out.println( name + ", Array of: " + 
               type.getComponentType().getName() + ", " + 
               Modifier.toString( modifiers) );
         else
            System.out.println( name + ", " + type.getName() + ", " + 
               Modifier.toString( modifiers) );
         }
   }
}
Output
Field name, type, modifiers
privateInt, int, private static
protectedField, float, protected
data, Array of: int, 
message, java.lang.String, public

Listen Here!S-dec9 7mins Doc 35, Reflection Slide # 6

Creating Objects

The following example shows how to use reflection to create new objects. The first example, using newInstance(), calls the default constructor, which must be public. The second example, uses a Constructor object.
import java.lang.reflect.*;

public class Test  {
   public static void main( String args[] )  
         throws ClassNotFoundException, IllegalAccessException, 
                  InstantiationException,NoSuchMethodException,
                  InvocationTargetException {
      Class sampleClass = Sample.class;
      
      Sample aSample = (Sample) sampleClass.newInstance();
      System.out.println( aSample );      
      
      Class aClass = Class.forName( "Sample" );
      
      Class[] argumentTypes = { java.lang.String.class };
      Constructor oneArgument = 
         aClass.getConstructor( argumentTypes ); 
      
      Object[] arguments = { "Test" };
      Object newObject = oneArgument.newInstance( arguments );
      System.out.println( newObject.getClass() );
      System.out.println( newObject );
   }
}
Output
Default Constructor
Sample(1, 1.2, Hi mom)
One arg Constructor
class Sample
Sample(1, 1.2, Test)

Doc 35, Reflection Slide # 7

Calling Methods

This example shows how to call methods using reflection. If there are no arguments, set argumentTypes to new Class[0].
import java.lang.reflect.*;

public class Test  {
   public static void main( String args[] )  
         throws ClassNotFoundException, IllegalAccessException, 
                  InstantiationException,NoSuchMethodException,
                  InvocationTargetException  {
      Class sampleClass = Sample.class;
      Class[] argumentTypes = { Integer.TYPE, String.class };
      Method aMethod = 
         sampleClass.getMethod( "setFields", argumentTypes ); 
      Object aSample =  sampleClass.newInstance();
      System.out.println( aSample );
      Object[] arguments = { new Integer(23), "Bye" };
      aMethod.invoke( aSample, arguments );
      System.out.println( aSample );
   }
}
Output
Default Constructor
Sample(1, 1.2, Hi mom)
Set fields
Sample(23, 1.2, Bye)

Doc 35, Reflection Slide # 8
Array Parameters
This example shows how to handle array parameters.

import java.lang.reflect.*;

public class Test  {
   public static void main( String args[] )  
         throws ClassNotFoundException, 
                  IllegalAccessException, 
                  InstantiationException,
                  NoSuchMethodException,
                  InvocationTargetException 
                  {
      Class sampleClass = Sample.class;
      Class[] argumentTypes = { int[].class };
      Method aMethod = 
         sampleClass.getMethod( "setData", argumentTypes ); 
      Object aSample =  sampleClass.newInstance();
      System.out.println( aSample );
      int[] someData = { 1, 2, 3, 4 };
      Object[] arguments = { someData };
      aMethod.invoke( aSample, arguments );
      System.out.println( aSample );
   }
}

Copyright © 1998 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.

visitors since 09-Dec-98