Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Reflection In Java
•
•
•
For every loaded class, the Java Runtime Environment
(JRE) maintains an associated Class object
The Class object “reflects” the class it represents
•
The primitive Java types are also represented as Class objects
Reflection can be used to:
Determine the class of an object.
Get information about a class's modifiers, fields, methods, interfaces,
constructors, and superclasses.
Create an instance of a class whose name is not known until runtime.
Get and set the value of an object's field.
Invoke a method on an object.
03G-1
Main Java reflection Classes
Class ( java.lang.Class )
Instances of the class Class represent classes and interfaces in a
running Java application, every object is represented by a Class
object.
Member
An Interface that reflects identifying information about a single
member (a field or a method) or a constructor.
Method ( java.lang.reflect.Method )
Implements Member Interface
provides information about, and access to, a single method on a
class or interface.
Represents instance methods and class methods ( static )
Field
Implements Member Interface
provides information about, and dynamic access to, a single field (
also for static fields )
Provides access and modification ( set, get ) methods.
03G-2
Main Java reflection Classes
Constructor
provides information about, and access to, a single
constructor for a class.
Package
Package objects contain version information about the
implementation and specification of a Java package
Modifier
provides static methods and constants to decode class and
member access modifiers. The sets of modifiers are
represented as integers with distinct bit positions
representing different modifiers
03G-3
Accessing the Class object
To get the Class object for an object mystery:
Class c = mystery.getClass();
Or, using the class name:
Class c = Class.forName(“mysteryClass”);
Getting the superclass of MysteryClass:
Class s = c.getSuperclass();
Getting the class name:
String s = c.getName();
Discovering the interfaces implemented by a class:
Class[] interfaces = c.getInterfaces();
Discovering the fields of a class:
Field[] fields = c.getFields();
Discovering the methods of a class:
Method[] methods = c.getMethods();
03G-4
Example
static void showMethods(Object o) {
Class c = o.getClass();
Method[] theMethods = c.getMethods();
for (int i = 0; i < theMethods.length; i++) {
String methodString = theMethods[i].getName();
System.out.println("Name: " + methodString);
String returnString = theMethods[i].getReturnType().getName();
System.out.println(" Return Type: " + returnString);
Class[] parameterTypes = theMethods[i].getParameterTypes();
System.out.print(" Parameter Types:");
for (int k = 0; k < parameterTypes.length; k ++) {
String parameterString = parameterTypes[k].getName();
System.out.print(" " + parameterString);
}
System.out.println();
}
}
03G-5
Example – Cont.
Output for a call of the form:
Polygon p = new Polygon();
showMethods(p);
Name: equals
Return Type: boolean
Parameter Types: java.lang.Object
Name: getClass
Return Type: java.lang.Class
Parameter Types:
Name: intersects
Return Type: boolean
Parameter Types: double double double double
.
.
03G-6
A Java Reflection Example
Illustrates Four Issues:
Runtime type Information (RTTI)
Introspection
Invoking Method Objects
Dynamic Instantiation
03G-7
Back to our Employee Example…
Employee
Only
number
partial
view of the
classes…
level
print()
MonthlyEmployee
HourlyEmployee
print()
print()
public final class MonthlyEmployee extends Employee {
public void print() {
System.out.println(“I’m a Monthly Employee");
}
}
public final class HourlyEmployee extends Employee {
public void print() {
System.out.println(“I’m a Hourly Employee");
}
}
03G-8
Reflection and Dynamic Binding
What is the output of the following test code:
Employee e;
e = new MonthlyEmployee();
Class c = e.getClass();
System.out.println("class of e = " + c.getName());
e = new HourlyEmployee();
c = e.getClass();
System.out.println("class of e = " + c.getName());
class of e = MonthlyEmployee
class of e = HourlyEmployee
03G-9
SuperClass
What is the output of the following test code:
c = c.getSuperclass();
System.out.println("base class of e = " +
c.getName());
c = c.getSuperclass();
System.out.println("base of base class of e = "
+ c.getName());
base class of e = Employee
base of base class of e = java.lang.Object
03G-10
Getting all fields
Field fields[] = c.getFields();
for(int i = 0; i < fields.length; i++) {
System.out.print(fields[i].getName() + "= ");
System.out.println(fields[i].getInt(e));
}
e is an instance of
Employee or its
subtype !
The output produced:
number= 111
level= 12
Non public
fields are not
printed!
03G-11
Setting a Field
Let’s promote an employee…
Field fields[] = c.getFields();
for(int i = 0; i < fields.length; i++) {
if(fields[i].getName() == “Level”)
fields[i].setInt(e, (fields[i].getInt(e))++);
}
03G-12
Examining Modifiers
What is the output of the following test code:
int m = c.getModifiers();
if (Modifier.isPublic(m))
System.out.println("public");
if (Modifier.isAbstract(m))
System.out.println("abstract");
if (Modifier.isFinal(m))
System.out.println("final");
public final
03G-13
Invoking Method Objects
•
•
We can ask a method object to invoke the method
it represents
we must provide it with the implicit and explicit
arguments
Employee e = new HourlyEmployee();
Class c = e.getClass();
Method m = c.getMethod(“print”, null);
m.invoke(e, null);
the output produced:
I’m a Hourly Employee
03G-14
Dynamic Instantiation
The universal printer gets the employee type and invokes the print method..
class UniversalPrinter {
public void print(String empType) {
Class c = Class.forName(empType);
Employee emp = (Employee ) c.newInstance();
emp.print();
}
}
-for calling other constructors( with arguments ), use the
Constructor class
- Constructor c = ...
- Object newObject = c.newInstance( Object[] initArguments )
What is the output of the following code?
UniversalPrinter p = new UniversalPrinter();
String empType;
empType = “HourlyEmployee";
p.print(empType);
empType = “MonthlyEmployee";
p.print(empType);
03G-15
Reflection - What’s missing
Reflection is ‘read only’
Cant add / modify fields, methods
Non public members (fields, methods) are not
accessible
Implementation is not available
Program logic is not reflected
Major performance impact
Much slower then doing the same operations directly…
Complex code
03G-16