Reflection
Madhavan Mukund
[Link]
Programming Concepts using Java
Week 5
Reflection
Wikipedia
Reflective programming or reflection is the ability of a process to examine, introspect,
and modify its own structure and behaviour.
Madhavan Mukund Reflection Programming Concepts using Java 2 / 14
Reflection
Wikipedia
Reflective programming or reflection is the ability of a process to examine, introspect,
and modify its own structure and behaviour.
Two components involved in reflection
Madhavan Mukund Reflection Programming Concepts using Java 2 / 14
Reflection
Wikipedia
Reflective programming or reflection is the ability of a process to examine, introspect,
and modify its own structure and behaviour.
Two components involved in reflection
Introspection
A program can observe, and therefore reason about its own state.
Madhavan Mukund Reflection Programming Concepts using Java 2 / 14
Reflection
Wikipedia
Reflective programming or reflection is the ability of a process to examine, introspect,
and modify its own structure and behaviour.
Two components involved in reflection
Introspection
A program can observe, and therefore reason about its own state.
Intercession
A program can modify its execution state or alter its own interpretation or meaning.
Madhavan Mukund Reflection Programming Concepts using Java 2 / 14
Reflection in Java
Simple example of introspection
Employee e = new Manager(...);
...
if (e instanceof Manager){
...
}
Madhavan Mukund Reflection Programming Concepts using Java 3 / 14
Reflection in Java
Simple example of introspection
Employee e = new Manager(...);
...
if (e instanceof Manager){
...
}
What if we don’t know the type that we want to check in advance?
Madhavan Mukund Reflection Programming Concepts using Java 3 / 14
Reflection in Java
Simple example of introspection
Employee e = new Manager(...);
...
if (e instanceof Manager){
...
}
What if we don’t know the type that we want to check in advance?
Suppose we want to write a function to check if two different objects are both
instances of the same class?
public static boolean classequal(Object o1, Object o2){
...
// return true iff o1 and o2 point to objects of same type
...
}
Madhavan Mukund Reflection Programming Concepts using Java 3 / 14
Reflection in Java . . .
public static boolean classequal(Object o1, Object o2){...}
Madhavan Mukund Reflection Programming Concepts using Java 4 / 14
Reflection in Java . . .
public static boolean classequal(Object o1, Object o2){...}
Can’t use instanceof
Will have to check across all defined classes
This is not even a fixed set!
Madhavan Mukund Reflection Programming Concepts using Java 4 / 14
Reflection in Java . . .
public static boolean classequal(Object o1, Object o2){...}
Can’t use instanceof
Will have to check across all defined classes
This is not even a fixed set!
Can’t use generic type variables
The following code is syntactically disallowed
if (o1 instance of T) { ...}
Madhavan Mukund Reflection Programming Concepts using Java 4 / 14
Introspection in Java
Can extract the class of an object using getClass()
Madhavan Mukund Reflection Programming Concepts using Java 5 / 14
Introspection in Java
Can extract the class of an object using getClass()
Import package [Link]
import [Link].*;
class MyReflectionClass{
...
public static boolean classequal(Object o1, Object o2){
return ([Link]() == [Link]());
}
}
Madhavan Mukund Reflection Programming Concepts using Java 5 / 14
Introspection in Java
Can extract the class of an object using getClass()
Import package [Link]
import [Link].*;
class MyReflectionClass{
...
public static boolean classequal(Object o1, Object o2){
return ([Link]() == [Link]());
}
}
What does getClass() return?
Madhavan Mukund Reflection Programming Concepts using Java 5 / 14
Introspection in Java
Can extract the class of an object using getClass()
Import package [Link]
import [Link].*;
class MyReflectionClass{
...
public static boolean classequal(Object o1, Object o2){
return ([Link]() == [Link]());
}
}
What does getClass() return?
An object of type Class that encodes class information
Madhavan Mukund Reflection Programming Concepts using Java 5 / 14
The class Class
A version of classequal the explicitly uses this fact
import [Link].*;
class MyReflectionClass{
...
public static boolean classequal(Object o1, Object o2){
Class c1, c2;
c1 = [Link]();
c2 = [Link]();
return (c1 == c2);
}
}
Madhavan Mukund Reflection Programming Concepts using Java 6 / 14
The class Class
A version of classequal the explicitly uses this fact
import [Link].*;
class MyReflectionClass{
...
public static boolean classequal(Object o1, Object o2){
Class c1, c2;
c1 = [Link]();
c2 = [Link]();
return (c1 == c2);
}
}
For each currently loaded class C, Java creates an object of type Class with
information about C
Madhavan Mukund Reflection Programming Concepts using Java 6 / 14
The class Class
A version of classequal the explicitly uses this fact
import [Link].*;
class MyReflectionClass{
...
public static boolean classequal(Object o1, Object o2){
Class c1, c2;
c1 = [Link]();
c2 = [Link]();
return (c1 == c2);
}
}
For each currently loaded class C, Java creates an object of type Class with
information about C
Encoding execution state as data — reification
Representing an abstract idea in a concrete form
Madhavan Mukund Reflection Programming Concepts using Java 6 / 14
Using the Class object
Can create new instances of a class at runtime
...
Class c = [Link]();
Object o = [Link]();
// Create a new object of same type as obj
...
Madhavan Mukund Reflection Programming Concepts using Java 7 / 14
Using the Class object
Can create new instances of a class at runtime
...
Class c = [Link]();
Object o = [Link]();
// Create a new object of same type as obj
...
Can also get hold of the class object using the name of the class
...
String s = "Manager".
Class c = [Link](s);
Object o = [Link]();
...
Madhavan Mukund Reflection Programming Concepts using Java 7 / 14
Using the Class object
Can create new instances of a class at runtime
...
Class c = [Link]();
Object o = [Link]();
// Create a new object of same type as obj
...
Can also get hold of the class object using the name of the class
...
String s = "Manager".
Class c = [Link](s);
Object o = [Link]();
...
. . . , or, more compactly
...
Object o = [Link]("Manager").newInstance();
...
Madhavan Mukund Reflection Programming Concepts using Java 7 / 14
The class Class . . .
From the Class object for class C, we can extract details about constructors,
methods and fields of C
Madhavan Mukund Reflection Programming Concepts using Java 8 / 14
The class Class . . .
From the Class object for class C, we can extract details about constructors,
methods and fields of C
Constructors, methods and fields themselves have structure
Constructors: arguments
Methods : arguments and return type
All three: modifiers static, private etc
Madhavan Mukund Reflection Programming Concepts using Java 8 / 14
The class Class . . .
From the Class object for class C, we can extract details about constructors,
methods and fields of C
Constructors, methods and fields themselves have structure
Constructors: arguments
Methods : arguments and return type
All three: modifiers static, private etc
Additional classes Constructor, Method, Field
Madhavan Mukund Reflection Programming Concepts using Java 8 / 14
The class Class . . .
From the Class object for class C, we can extract details about constructors,
methods and fields of C
Constructors, methods and fields themselves have structure
Constructors: arguments
Methods : arguments and return type
All three: modifiers static, private etc
Additional classes Constructor, Method, Field
Use getConstructors(), getMethods() and getFields() to obtain
constructors, methods and fields of C in an array.
Madhavan Mukund Reflection Programming Concepts using Java 8 / 14
The class Class . . .
Extracting information about constructors, methods and fields
...
Class c = [Link]();
Constructor[] constructors = [Link]();
Method[] methods = [Link]();
Field[] fields = [Link]();
...
Madhavan Mukund Reflection Programming Concepts using Java 9 / 14
The class Class . . .
Extracting information about constructors, methods and fields
...
Class c = [Link]();
Constructor[] constructors = [Link]();
Method[] methods = [Link]();
Field[] fields = [Link]();
...
Constructor, Method, Field in turn have functions to get further details
Madhavan Mukund Reflection Programming Concepts using Java 9 / 14
The class Class . . .
Example: Get the list of parameters for each constructor
...
Class c = [Link]();
Constructor[] constructors = [Link]();
for (int i = 0; i < [Link]; i++){
Class params[] = constructors[i].getParameterTypes();
..
}
Madhavan Mukund Reflection Programming Concepts using Java 10 / 14
The class Class . . .
Example: Get the list of parameters for each constructor
...
Class c = [Link]();
Constructor[] constructors = [Link]();
for (int i = 0; i < [Link]; i++){
Class params[] = constructors[i].getParameterTypes();
..
}
Each parameter list is a list of types
Return value is an array of type Class[]
Madhavan Mukund Reflection Programming Concepts using Java 10 / 14
The class Class . . .
We can also invoke methods and examine/set values of fields.
...
Class c = [Link]();
..
Method[] methods = [Link]();
Object[] args = { ... }
// construct an array of arguments
methods[3].invoke(obj,args);
// invoke methods[3] on obj with arguments args
...
Madhavan Mukund Reflection Programming Concepts using Java 11 / 14
The class Class . . .
We can also invoke methods and examine/set values of fields.
...
Class c = [Link]();
..
Method[] methods = [Link]();
Object[] args = { ... }
// construct an array of arguments
methods[3].invoke(obj,args);
// invoke methods[3] on obj with arguments args
...
Field[] fields = [Link]();
Object o = fields[2].get(obj);
// get the value of fields[2] from obj
...
fields[3].set(obj,value);
// set the value of fields[3] in obj to value
...
Madhavan Mukund Reflection Programming Concepts using Java 11 / 14
Reflection and security
Can we extract information about private methods, fields, . . . ?
Madhavan Mukund Reflection Programming Concepts using Java 12 / 14
Reflection and security
Can we extract information about private methods, fields, . . . ?
getConstructors(), . . . only return publicly defined values
Madhavan Mukund Reflection Programming Concepts using Java 12 / 14
Reflection and security
Can we extract information about private methods, fields, . . . ?
getConstructors(), . . . only return publicly defined values
Separate functions to also include private components
getDeclaredConstructors()
getDeclaredMethods()
getDeclaredFields()
Madhavan Mukund Reflection Programming Concepts using Java 12 / 14
Reflection and security
Can we extract information about private methods, fields, . . . ?
getConstructors(), . . . only return publicly defined values
Separate functions to also include private components
getDeclaredConstructors()
getDeclaredMethods()
getDeclaredFields()
Should this be allowed to all programs?
Madhavan Mukund Reflection Programming Concepts using Java 12 / 14
Reflection and security
Can we extract information about private methods, fields, . . . ?
getConstructors(), . . . only return publicly defined values
Separate functions to also include private components
getDeclaredConstructors()
getDeclaredMethods()
getDeclaredFields()
Should this be allowed to all programs?
Security issue!
Madhavan Mukund Reflection Programming Concepts using Java 12 / 14
Reflection and security
Can we extract information about private methods, fields, . . . ?
getConstructors(), . . . only return publicly defined values
Separate functions to also include private components
getDeclaredConstructors()
getDeclaredMethods()
getDeclaredFields()
Should this be allowed to all programs?
Security issue!
Access to private components may be restricted through external security policies
Madhavan Mukund Reflection Programming Concepts using Java 12 / 14
Using reflection
BlueJ, a programming environment to learn Java
Madhavan Mukund Reflection Programming Concepts using Java 13 / 14
Using reflection
BlueJ, a programming environment to learn Java
Can define and compile Java classes
Madhavan Mukund Reflection Programming Concepts using Java 13 / 14
Using reflection
BlueJ, a programming environment to learn Java
Can define and compile Java classes
For compiled code, create object, invoke methods, examine state
Madhavan Mukund Reflection Programming Concepts using Java 13 / 14
Using reflection
BlueJ, a programming environment to learn Java
Can define and compile Java classes
For compiled code, create object, invoke methods, examine state
Uses reflective capabilities of Java — BlueJ need not internally maintain
“debugging” information about each class
Madhavan Mukund Reflection Programming Concepts using Java 13 / 14
Using reflection
BlueJ, a programming environment to learn Java
Can define and compile Java classes
For compiled code, create object, invoke methods, examine state
Uses reflective capabilities of Java — BlueJ need not internally maintain
“debugging” information about each class
See [Link]
Madhavan Mukund Reflection Programming Concepts using Java 13 / 14
Limitations of Java reflection
Cannot create or modify classes at run time
The following is not possible
Class c = new Class(....);
An environment like BlueJ must invoke Java compiler before you can use a new class
Madhavan Mukund Reflection Programming Concepts using Java 14 / 14
Limitations of Java reflection
Cannot create or modify classes at run time
The following is not possible
Class c = new Class(....);
An environment like BlueJ must invoke Java compiler before you can use a new class
Contrast with Python
class XYZ: can be executed at runtime in Python
Madhavan Mukund Reflection Programming Concepts using Java 14 / 14
Limitations of Java reflection
Cannot create or modify classes at run time
The following is not possible
Class c = new Class(....);
An environment like BlueJ must invoke Java compiler before you can use a new class
Contrast with Python
class XYZ: can be executed at runtime in Python
Other OO languages like Smalltalk allow redefining methods at run time
Madhavan Mukund Reflection Programming Concepts using Java 14 / 14