CS 4773 Object Oriented Systems Java Reflection
This material is taken from Chapter 5 of Core Java volume 1
and the Java documentation
Introduction
The reflection API is a toolset for manipulating Java code dynamically.
It allows you to:
- analyze the capabilities of classes at run time.
- inspect objects at run time.
- use method pointers !
The Class class
The java runtime system keeps track of the class to which each object belongs.
You can get this information with the getClass() method:
Object myObject;
Class myObjectClass;
...
myObjectClass = myObject.getClass();
You can also get a class from its name:
Class dateClass = Class.forName("java.util.Date");
Once you have the class, some of the things you can do are:
- get the name of the class
- get a particular constructor for this class
- get all constructors for this class
- get a method for this class
- get all methods for this class
The Constructor class
An instance of this represents a constructor for a given class.
get the name of the constructor
get the parameter types of the constructor
create a new instance of the class corresponding to this constructor.
The Method class
An instance of this represents a method of a given class.
Having one fo these allows you to:
- get the name of the method
- get the parameter types of the method
- get the return value type of the method
- execute the method
Many of the methods in the reflection API throw exceptions when an error
occurs.
Many of the methods in the reflection API take advantage of the new varargs
feature of Java 5.
You have used varargs in C when you have used fprintf or similar
functions.
You use the ellipsis to represent a list of arguments and you can iterate
through the list: (From p 189 of Core Java I)
public static double max(double... values) {
double largest = Double.MIN_VALUE;
for (double v : values)
if (v > largest)
largest = v;
return largest;
}
You can call this as follows:
double n = max*3.1, 40.4, -5);
Look at the Java documentation for Class, Constructor, and
Method
How I used reflection in the editor project
Problem:
- We want to have one list of commands that is shared by all sessions.
- Each command needs to know what CommandExecuter class to use to execute it.
- An instance of CommandExectuer corresponds to a given session
- When we execute a command, we need to get the appropriate instance of the
CommandExecuter class stored int he command.
- In addition, each CommandExecute class creates a set of commands through
a static method.
Solution:
- We keep a list of CommandExecuter instances for each session.
- This list is in the same order for each session.
- The command keeps the index of the appropriate command executer.
- The static method in CommandExecuter classes is called to create the
commands and set the order id (order in the list) of the CommandExecuter.
Original Implementation:
- The Editor called a static method of each class that extends
CommandExecuter to create the commands.
- This static method was passed an id which would be the index in the
list of the corresponding instance in the list kept by each session.
- In the Commands class, the instances of the CommandExecuters were
created and put in a list.
- The result was that there were two classes (Editor and Commands) that
had to have the same list of CommandCreators in the same order.
Current Implementation:
- The Editor class has a list of names (strings) of the CommandExecuter
classes.
- The order in this list determines the ids of these classes.
- The Editor class has an ArrayList of type Class containing the
classes that extend ExecuterClass in order of their ids.
- The Editor class uses reflection to execute the setId
static method of each of these classes, passing in the id.
- The addExecuterClass(String s) method in Editor, takes the
name of a class, creates a Class, puts it in the ArrayList,
and executes the setId static method of this class.
- An instance of the Commands class (one for each session) calls
method in Editor (setupExecuters) to create the instances of the
CommandExecuter classes for the given session.
- This creates an ArrayList of CommandExecuter instances in Commands
by finding the constructor of each class and using the
newInstance method of the constructor, passing the session
as a parameter to the constructor.