CS 596 Java Programming Fall Semester, 1998 Reflection |
||
---|---|---|
© 1998, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 21-Dec-98 |
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()
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 +")"; } }
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
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
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)
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)
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