![]() |
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 |
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() |
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 +")"; } }
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; }
java.util.Vector
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) ); } } }
Field name, type, modifiers privateInt, int, private static protectedField, float, protected data, Array of: int, message, java.lang.String, public
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 ); } }
Default Constructor Sample(1, 1.2, Hi mom) One arg Constructor class Sample Sample(1, 1.2, Test)
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 ); } }
Default Constructor Sample(1, 1.2, Hi mom) Set fields Sample(23, 1.2, Bye)
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 ); } }