8.5 Reflection

The Java 1.1 reflection API offers another way to access classes that are loaded with class loaders. You can examine the class to find out what fields and methods it has and even invoke the methods and read or assign the fields. Check out the getMethods and getFields methods in Class. These enable a program to call methods and use fields it did not know about when the program was compiled.

Reflection requires several new classes to represent fields and methods. The new classes are found in the package java.lang.reflect. Fields are represented by instances of class Field, methods by instances of Method.

The Logo program shown has two variables, X and Y. If the Logo compiler compiles these as fields in the Program1 class, they can be set like this:

 Field x = programOneClass.getField("X"); Field y = programOneClass.getField("Y"); x.setInt(o, 90);                    // Set X to 90 y.setInt(o, 20);                    // Set Y to 20 

Now the fields X and Y are set to 90 and 20, respectively, in the object.

Reflection also permits you to call constructors that take arguments. Reflection uses a special class called Constructor to represent constructors. If the compiler added a constructor Program1(int x, int y) to Program1, then a new Program1 can be created with

 // The arguments array represents the types we want to pass as // arguments // Integer.TYPE is the type for the int type. Class[] arg_types = {Integer.TYPE, Integer.TYPE }; Constructor programOneConstructor =    programOneClass.getConstructor(arg_types); Object[] arguments = {new Integer(1000), new Integer(12) }; Object newProg1 = programOneConstructor.newInstance(arguments); 

This code will create a new object of type Program1, calling the constructor that takes two ints instead of the no-argument constructor. The method getConstructor returns the constructor taking the arguments given by the arguments array, in this case, two ints.

The method newInstance takes a list of the arguments to the constructor. It should have the same length as the argument array and the types should correspond to the constructor types. Since you can't put ints into an array of Objects, you have to wrap them up as Integers; the system is smart enough to unwrap them before it passes them to the constructor as an int.

Similar techniques can be used to call methods on the objects. Take this Logo program:

 to square params [x] repeat 2 [forward :x left 90 forward :x left 90] end 

You can compile this into the class Program2, which has a method called square taking an int argument. This method can be compiled and invoked like this:

 // Compile a Logo program into the class Program2 byte[] bytecodes = LogoCompiler.compile(    "Program2",    "to square " +    "params [x] " +    "repeat 2 [forward :x left 90 forward :x left 90]" +    "end"); // Define it to the virtual machine loader.store("Program2", bytecodes); // Load it and resolve it Class program2Class = loader.loadClass("Program2", true); // Create an instance of Program2 Object program2object = program2Class.newInstance(); Class[] arg_types = {Integer.TYPE }; Method rectangle = program2class.getDeclaredMethod("square",        arg_types); Object[] square_size = {new Integer(99) }; rectangle.invoke(program2object, square_size); 

These features were added to the Java API to support JavaBeans, a component architecture that makes much use of dynamically loaded classes. A JavaBeans builder is a visual application developer. A builder can read in the definition of a class using a class loader and use reflection to get the list of methods and fields. The list can be presented to the user, who can use the methods and fields to connect methods together to build new classes.

Exercise 8.1

Create your own class loader that reads the class definition from a file. Have it print a message whenever it starts to load a class, when it resolves it, and when it's done. Try it out on some classes, and observe the order in which it loads and resolves classes.



Programming for the Java Virtual Machine
Programming for the Javaв„ў Virtual Machine
ISBN: 0201309726
EAN: 2147483647
Year: 1998
Pages: 158
Authors: Joshua Engel

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net