![]() |
COMP 202: Principles of Object-Oriented Programming II← Dynamic Class Loading → |
Home — Fall 2008 | | | Information | | | Resources | | | Exam Resources | | | OwlSpace | | | COMP 201 |
In the design of the Temperature Calculator
assignment, we want the program to have the ability to convert between
temperature scales that are not known at compile time. Since you cannot know
the class names of classes not known at compile time, the model cannot simply
create an object of that class by doing new Celsius()
or something
similar that uses the class name directly in the source code.
When the "Add..." button is pressed, the user can enter the
fully qualified class name, i.e. the class name with all the package names
in front of the class (e.g. java.lang.Object
as opposed to just
Object
The starting point of most reflection concerns is a
class named Class.
In most cases, we will need to import a package named
java.lang.reflect to make use of the classes
pertaining to reflection.
- Suppose s is a String
that stands for the fully qualified name of some class (or interface, or
primitive type). For example, s
is "model.temperature.Celsius".
Then Class.forName(s) will return the
Class object associated with the class
whose name is s. Its signature is
Class<?> forName(String s).
- If we know that a given class has a default
constructor, i.e. a constructor with no parameter, then we can create an
instance of this class via the method
Class.newInstance(). For example,
Class.forName("java.lang.String").newInstance()
will return the empty string: ""
- If we do not have any a priori knowledge on the
constructors of a given class, we can query the class for an array of its
Constructor objects. Suppose someClass is an object of type Class:
Class someClass;
Then there is a unique class X that is
associated with someClass, and someClass.getConstructors()
will return an array of Constructor
objects that correspond to all the public constructors for X.
Its signature is
Constructor[] getConstructor().
- We can use any of these Constructor
objects to instantiate an object of type X
(without knowing what X is) by invoking
its newInstance(...) method. newInstance(...)
requires an array of Object parameters
corresponding to the list of parameters of the associated
constructor. Its signature is
T newInstance(Object... args).
- The Constructor class has a method
called getParameterTypes() that returns
the array of Class objects corresponding
to the parameter types of the associated constructor. Its signature
is
Class<?>[] getParameterTypes().
Below is a method that makes use of the above class loading capability to
add a new temperature unit to the list of units, after asking the user to enter
the fully qualified class name and storing that name in the className
string:
Here is some source code that uses reflection
to perform operations on lists.
←
Dynamic Class Loading
→
URL: http://www.cs.rice.edu/teaching/202/08-fall/lectures/classLoading/index.shtml
Copyright © 2008-2010 Mathias Ricken and Stephen Wong