CSC/ECE 517 Summer 2008/wiki1 3 jb: Difference between revisions
No edit summary |
|||
Line 54: | Line 54: | ||
*The next thing to note is the first statement within the main method. An instance of the [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html Class] class is retrieved from the MountainBike class' static variable. The Java Object class, the base class of all objects, provides the Class member variable to expose reflection capabilities. Notice also that an instance of a Class can also be obtained from an instance of an object by calling the getClass method. | *The next thing to note is the first statement within the main method. An instance of the [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html Class] class is retrieved from the MountainBike class' static variable. The Java Object class, the base class of all objects, provides the Class member variable to expose reflection capabilities. Notice also that an instance of a Class can also be obtained from an instance of an object by calling the getClass method. | ||
*Next, a [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Method.html Method] object is retrieved from the Class object, via the getMethod | *Next, a [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Method.html Method] object is retrieved from the Class object, via the getMethod method. The getMethod object takes a method name as a String, and a variable length list of Class objects for arguments. In this case, the setDiscBrakes method takes an instance of the DiscBrakes class as a parameter. | ||
*Finally, the Method object's invoke method is used to call the setDiscBrakes method on the instance of the MountainBike class. The invoke method takes as its first parameter, the object on which to invoke the method, and then a variable length list of Objects for arguments. | *Finally, the Method object's invoke method is used to call the setDiscBrakes method on the instance of the MountainBike class. The invoke method takes as its first parameter, the object on which to invoke the method, and then a variable length list of Objects for arguments. |
Revision as of 01:00, 4 June 2008
This wiki will explore how reflection is implemented with both Java and Ruby, with the goal of showing which language's implementation makes it easier to write, easier to understand, and more efficient. Reflection refers to the ability of a program to peer inside itself and observe the components that it is comprised of. Reflection, as supported in Object Oriented languages such Java and Ruby, provides facilities to query about the methods and attributes of a specified class, and to execute methods that are discovered at runtime. These two features of reflection in Java and Ruby will explored in detail below.
To illustrate the concept of reflection, a simple example will be implemented in both Java and Ruby. The data model in the example will consist of a mountain bike class and disc brake class. A mountain bike in this example is composed of disc brakes. The mountain bike class will expose a public setter method method to set the disc brakes object. This setter method will be queried for by name at runtime, and executed.
Reflection in Java
The following is an implementation of the example in Java. A detailed description of the code follows.
import java.lang.reflect.*; class DiscBrakes { } class MountainBike { private DiscBrakes m_discBrakes = null; public boolean hasDiscBrakes() { return m_discBrakes != null; } public void setDiscBrakes(DiscBrakes discBrakes) { m_discBrakes = discBrakes; } public static void main (String argv[]) { Class c = MountainBike.class; MountainBike mb = new MountainBike(); DiscBrakes db = new DiscBrakes(); Method meth = null; // Class c = mb.getClass(); System.out.println(mb.hasDiscBrakes()); try { meth = c.getMethod("setDiscBrakes", DiscBrakes.class); } catch (NoSuchMethodException e) { } try { meth.invoke(mb, db); } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } System.out.println(mb.hasDiscBrakes()); } }
- The first thing to note in the Java implementation is the import of the java.lang.reflect package.
- The next thing to note is the first statement within the main method. An instance of the Class class is retrieved from the MountainBike class' static variable. The Java Object class, the base class of all objects, provides the Class member variable to expose reflection capabilities. Notice also that an instance of a Class can also be obtained from an instance of an object by calling the getClass method.
- Next, a Method object is retrieved from the Class object, via the getMethod method. The getMethod object takes a method name as a String, and a variable length list of Class objects for arguments. In this case, the setDiscBrakes method takes an instance of the DiscBrakes class as a parameter.
- Finally, the Method object's invoke method is used to call the setDiscBrakes method on the instance of the MountainBike class. The invoke method takes as its first parameter, the object on which to invoke the method, and then a variable length list of Objects for arguments.
Reflection in Ruby
class DiscBrakes end class MountainBike @m_discBrakes = nil def hasDiscBrakes return @m_discBrakes != nil end def setDiscBrakes(discBrakes) @m_discBrakes = discBrakes end end mb = MountainBike.new puts mb.hasDiscBrakes db = DiscBrakes.new setter = mb.method("setDiscBrakes") setter.call(db) puts mb.hasDiscBrakes
Comparison
Conclusions
Java Reflection Links
Pros and cons of reflection, written for Java but pretty generic
Using Java reflection
Javadoc for the java.lang.Class class
Javadoc for the java.lang.reflect package