20-CS-694 Advanced Programming Techniques Spring 2012
Reflection

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

All lectures
Review

This series shows how to use many of the classes associated with reflection. Click on the class names in the following table to see class details.

Class Method Constructor Field Modifier Array
ClassLoader RMIClassLoader SecureClassLoader URLClassLoader

1.   ClassLoader  -
  • Takes a collection of bytes and turnes them into a class via defineClass
  • Is an abstract class
  • The loadClass and findClass methods may have to be implemented.
  • There is a built-in classloader called the "bootstrap class loader"
  • Every class has an associated classloader
  • Custom class loaders can load classes from anywhere - internet, files, etc.
  • The operation of a class loader is made relatively easy by the richness
    of information normally in a class file.
  • There are several specialized class loaders in the JVM library
    examples: RMIClassLoader, SecureClassLoader, URLClassLoader
 
2.   Class  -
  • An object of the Class class can be used to interrogate the information and resources associated with the corresponding class.
  • Works for interfaces too.
  • Works also for arrays and primitive types.
  • A Class object may be the result of a call to defineClass of a class loader.
  • A Class object may be the result of a call to the static forName method.
  • Some important methods:
    • getClassLoader - the class loader associated with this class
    • getName - name of class
    • forName - return class object from given resource name
    • getConstructor - public constructor of known parameter types
    • getConstructors - list of all public constructors
    • getDeclaredConstructor - constructor
    • getDeclaredConstructors - all constructors
    • getDeclaredMethod - a specific method
    • getDeclaredMethods - a list of all methods
    • getDeclaredField - a specific field
    • getDeclaredFields - a list of all fields
  • Example use:
      byte [] classbytes = (byte[])in.readObject();
      String name = "Hello";
      Class <?> cls = defineClass(name, classbytes, 0, classbytes.length);
  • Example use:
      class A { ... }
      Class <?> cls = Class.forName("A");
 
3.   Constructor  -
  • Choose which class constructor to use when creating an instance via reflection.
  • Important methods:
    • getExceptionTypes - declared exceptions that may be thrown.
    • getParameterTypes - parameter types, in order.
    • newInstance - create a new instance of this class
  • Example use:
      Class <?> cls = Class.forName("A");
      Class con_cls[] =
        new Class[] { this.getClass(), Integer.TYPE, Float.TYPE };
      Constructor ct = cls.getConstructor(con_cls);
      Object con_obj[] =
        new Object[] { this, new Integer(37), new Float(47.1) };
      Object retobj = ct.newInstance(con_obj);
 
4.   Method  -
  • Interrogate a method of a class object and invoke it.
  • Important methods:
    • getExceptionTypes - declared exceptions that may be thrown.
    • getParameterTypes - parameter types, in order.
    • invoke - invoke the method on a specified object.
  • Example use:
      Method m =
        cls.getMethod("add", new Class<?>[] { Integer.TYPE, Float.TYPE });
      C m_obj = (C)ct.newInstance();
      Object retobj =
        m.invoke(m_obj, new Object[] {new Integer(37),new Float(47.1)});
 
5.   Field  -
  • Interrogate a field of a class object and set its value.
  • Important methods:
    • getType - parameter type.
    • getLong - get the value if it is a long type
    • getBoolean - get the value if it is a boolean type
    • ... - more of the same
    • set - set the value of the field.
  • Example use:
      Field fld = cls.getField("d");
      double dbl = fld.getDouble();
      fld.setDouble(obj, 12.34);
 
6.   Modifier  -
  • Decodes member modifiers
  • Example use:
      int mod = fld.getModifiers();
      System.out.println("modifiers="+Modifier.toString(mod));
 
7.   Array  -
  • Interrogate elements of an array and set their values.
  • Important methods:
    • getLength - size of array.
    • getLong - get the value at an index if it is a long type
    • getBoolean - get the value at an index if it is a boolean type
    • ... - more of the same
    • get - get the object at an index.
    • set - set the value at an index.
  • Example use:
      Object arr = Array.newInstance(cls, 10);
      Array.set(arr, 5, "this is a test");
      System.out.println((String)Array.get(arr, 5));