ReflectionDemo.java

Download ReflectionDemo.java

 1: // Demo of simple reflection.
 2: // Note that all objects automatically have a "toString" method,
 3: // and using "System.out.println( ... + obj + ... );" will use it.
 4: //
 5: // Written 10/2010 by Wayne Pollock, Tampa Florida USA.
 6: // Updated 2/2018 by WP: Fixed Generics warning on line 21, by adding "<?>",
 7: //                       and the deprecation of NewInstance.
 8: 
 9: import java.lang.reflect.Method;
10: import java.util.Date;
11: import java.util.Scanner;
12: 
13: public class ReflectionDemo
14: {
15:    public static void main ( String [] args ) throws Exception
16:    {
17:       Scanner in = new Scanner ( System.in );
18: 
19:       System.out.print( "Enter a class name (e.g., java.util.Date): " );
20:       String name = in.next();
21: 
22:       Class<?> cl = Class.forName( name );  // Load some class into the JVM.
23: 
24:       System.out.println( name + ".class: " + cl.getName() );
25: 
26:       Object obj = cl.getDeclaredConstructor().newInstance();  // Uses no-arg constructor.
27:       System.out.println( "obj.toString(): " + obj );
28: 
29:       System.out.println( "Enter one of the following \"no-arg\" method "
30:         + "names for " + cl + ":\n" );
31: 
32:       Method[] methods = cl.getMethods();
33:       for ( Method m : methods )
34:       {
35:          if ( m.getParameterTypes().length == 0 )
36:             System.out.println( "\t" + m );  // Print methods that take no args
37:       }
38: 
39:       System.out.print( "\nEnter method name (e.g., \"toString\"): " );
40:       String methodName = in.next();
41: 
42:       @SuppressWarnings("unchecked") // Turn off warning about unchecked types.
43:       Method method = cl.getMethod( methodName );
44:       Object result = method.invoke( obj );
45:       System.out.println( "method " + cl.getName() + "." + method.getName()
46:          + " returned: " + result );
47: 
48: //    ---------------------------------------------------------------
49: 
50:       // Shows use of "class" final field (same as obj.getClass()):
51:       cl = Date.class;
52:       System.out.println( "\n\nDate.class: " + cl.getName() );
53: 
54:       /* Shows internal mangled names for "internal" types, such as
55:        * primitives and arrays.  Here's a list of these:
56:        *    B (byte)
57:        *    C (char)
58:        *    D (double)
59:        *    F (float)
60:        *    I (int)
61:        *    J (long)
62:        *    S (short)
63:        *    Z (boolean)
64:        *    Lclassname;  (object types)
65:        *    [type (array of type)
66:        *    V (void)
67:        */
68: 
69:       obj = new Double[10];  // Array of Double objects
70:       cl = obj.getClass();
71:       System.out.println( "Array of Double: " + cl.getName() );
72: 
73:       obj = new double[10];  // array of double primitives
74:       cl = obj.getClass();
75:       System.out.println( "Array of double: " + cl.getName() );
76:    }
77: }