SlideShare a Scribd company logo
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Lecture 10 :

Advanced Reflection in Java
LSINF 2335
Programming Paradigms
Prof. Kim Mens
UCL / EPL / INGI


(Slides partly based on chapter 5 of
the book “Java Reflection in Action”
and on slides by Pr. Walter Cazzola)
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Advanced reflective features
■ Dynamic proxies
– Proxy
– InvocationHandler
■ Call stack introspection
– Throwable
– StackTraceElement
■ Instrumentation
– java.lang.instrument
■ Conclusion
2
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Proxy Design Pattern
3
■ The proxy pattern defines a proxy as a surrogate
for another object to control access to it
– a proxy keeps a reference to the real object,
– is substitutable with it (identical interface)
– and delegates requests to it
■ Typical examples of usage of proxies:
– local representation of remote objects
– delay of expensive operations
– access protection for secure objects
– ...
Client
action()
Subject
action()
Proxy
action()
RealSubject
delegate
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Dynamic proxies
■ To easily implement proxies, Java 1.3 introduced
the Dynamic Proxy API
■ A dynamic proxy requires
– an instance of the Proxy class
– a proxy interface
• this is the interface that is implemented by the proxy class
• interestingly, one proxy can implement multiple interfaces
– each proxy instance has an InvocationHandler
4Proxy
InvocationHandler
delegates to
IProxy
implements
ProxyHandler
implements
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
InvocationHandler
■ Each proxy instance has an InvocationHandler
– The invocation handler determines how to treat messages
that are sent to the proxy instance
– When a method is invoked on a proxy instance, the
method invocation is encoded and dispatched to the
invoke() method of its invocation handler



Object invoke(Object proxy, Method m, Object[] args)
5
Proxy
invoke(Object, Method, Object[]) : Object
<<interface>>
InvocationHandlerdelegates to
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Example: an InvocationHandler
to trace method calls
6
public class TraceHandler implements InvocationHandler {
private Object baseObject; // the real object the proxy delegates to
public TraceHandler(Object base) {
baseObject = base;
}
public Object invoke(Object proxy, Method m, Object[] args) {
Object result = null; // result to be returned by the method
try {
System.out.println("before " + m.getName());
result = m.invoke(baseObject, args);
System.out.println("after " + m.getName());
}
catch (Exception e) {

e.printStackTrace();
}
return result;
}

}
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Proxy class
■ Provides static methods for creating dynamic proxy classes and
instances
■ Is also the superclass of all dynamic proxy classes created by those
methods
■ To create a proxy for some class Foo

InvocationHandler handler = new MyInvocationHandler(...);
Class proxyClass = Proxy.getProxyClass(
Foo.class.getClassLoader(), new Class[] { Foo.class });
Foo f = (Foo) proxyClass.
getConstructor(new Class[] { InvocationHandler.class }).
newInstance(new Object[] { handler });
■ or more simply:

Foo f = (Foo) Proxy.

newProxyInstance( Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler );
7
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Example: a Proxy that traces
method calls
8
public class Component1 implements IComponent {
Color myColor;
public Color getColor() {
System.out.println("Inside the getColor() method of Component1.");
return myColor;
}
public void setColor(Color color) {
myColor = color;
System.out.println("The color of component 1 has been set.");
}
}
public interface IComponent {
public Color getColor();
public void setColor(Color color);
}
interface
shared by real
object and proxy
real object to
which proxy will
delegate
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Example: a Proxy that traces
method calls
9
IComponent c1 = new Component1(new Color(0));
InvocationHandler th = new TraceHandler(c1);
IComponent proxy = (IComponent) Proxy.newProxyInstance(
c1.getClass().getClassLoader(),

c1.getClass().getInterfaces(),
th);
/* standard call */
c1.getColor();
/* traced call */
proxy.getColor();
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
How Java creates a proxy
10
Bibliography Examples: Tracing and Proxy Chaining.
Java’s Dynamic Proxy.
How Java Creates a Proxy.
extends
implements
implements
implements
$IPoint
InvocationHandlerIPoint Proxy
Point TraceHandler
Classes
Interfaces
p thpth
- int x
- int y
+ getX(): int
+ getY(): int
+ getX(): int
+ getY(): int
+ getX(): int
+ getY(): int
- Object base
+ invoke(): Object
- InvocationHandler i
+ invoke(): Object
Generated
on-the-fly
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Advanced reflective features
■ Dynamic proxies
– Proxy
– InvocationHandler
■ Call stack introspection
– Throwable
– StackTraceElement
■ Instrumentation
– java.lang.instrument
■ Conclusion
11
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Call Stack Introspection
■ State introspection
■ Call stack
■ Reifying the call stack
– Throwable
– StackTraceElement
■ Examples:
– printing the call stack
– show warnings for unimplemented methods
12
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
State Introspection
13
■ Introspection is not only application structure
introspection
■ Information about the program execution can be
introspected as well
– the execution state
– the call stack
■ Each thread has a call stack consisting of stack
frames
■ Call stack introspection allows a thread to examine
its context
– the execution trace and the current frame
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Call Stack
1. package reflectionexample;
2.
3. public class Example {
4.
5. public static void m1() {
6. m2();
7. }
8.
9. public static void m2() {
10. m3();
11. }
12.
13. public static void m3() {
14. // do something
15. }
16.
17. public static void main(String[] args) {
18. m1();
19. }
20.
21. }
class: Example
method: main
line: 18
class: Example
method: m1
line: 6
Call Stack:
class: Example
method: m2
line: 10
class: Example
method: m3
line: 14
(m3)
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Reifying the call stack :
Throwable
15
■ In Java there is no accessible CallStack meta-object
■ But...
– When an instance of Throwable is created, the call stack is
saved as an array of StackTraceElement
– By writing: new Throwable().getStackTrace()
– This gives us access to a representation of the call stack at
the moment when Throwable was created.
– The getStackTrace() method returns the current call stack
as an array of StackTraceElement.
• The first frame (position 0) is the current frame
■ Only introspection
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Example 1: print the call stack
16
public class CallStackExample1 {
public static void m1() {
m2();
}
public static void m2() {
m3();
}
public static void m3() {
StackTraceElement[] stack = new Throwable().getStackTrace();
for (int i = 0; i < stack.length; i++) {
System.out.println(stack[i]);
}
}
public static void main(String[] args) {
m1();
}
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Example 2: show warnings for
unimplemented methods
■ How to create an auxiliary method to be used as
placeholder for unimplemented methods?
– I have a method todo() remaining to be implemented
– I provide the following dummy implementation
– When calling that method todo() I want to get a warning:
– as well as a warning message on the Console:
Warning:
reflectionexample.CallStackExample2.todo(CallStackExample2.java:8)
() : this method remains to be implemented
public static void todo() {
toBeImplemented();
}
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
18
Example 2: show warnings for
methods to be implemented
public class CallStackExample2 {
public static void todo() {
toBeImplemented();
}
public static void toBeImplemented() {
// Get the stack element referring to the method calling this one
StackTraceElement el = new Throwable().getStackTrace()[1];
// Show a dialog box with information on the method remaining to be
implemented
String msg = el.toString() + "() : this method remains to be implemented";
// Show this message in a dialog box
JOptionPane.showMessageDialog(null, msg, "Erreur", JOptionPane.ERROR_MESSAGE);
// Print this warning on the console
System.err.println("Warning: " + msg);
}
public static void main(String[] args) {
todo();
}
}
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Reifying the call stack :
StackTraceElement
■ From a frame we can get:
– getFileName() : the filename containing the execution point
– getLineNumber() : the line number where the call occurs
– getClassName() : the name of the class containing the

execution point
– getMethodName() : the name of the method containing the 

execution point
19
“myPackage.Example.m3(Example.java:14)”
StackTraceElement:
class: Example
method: m3
line: 14
filename: Example.java
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Advanced reflective features
■ Dynamic proxies
– Proxy
– InvocationHandler
■ Call stack introspection
– Throwable
– StackTraceElement
■ Instrumentation
– java.lang.instrument
■ Conclusion
20
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Suppose we want to...
1. Instrument a Java program to print a message on
the console whenever a class is loaded by the JVM
2. Instrument a Java program to print all messages
being sent while the program is running
3. Replace the definition of a class at runtime
– even for classes with currently active instances
– for example, to change the behaviour of some messages
understood by those instances
21
... then Java instrumentation may be your answer
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Java Instrumentation
(goal)
22
■ java.lang.instrument
– Provides services that allow Java programming language
agents to instruments programs running on the JVM.
– This instrumentation is achieved by modifying byte-code.
– Allows you to create agents, that run embedded in a JVM
and intercept the classloading process, to
• monitor the classloading process
• modify the bytecode of classes
■ Can be combined with dedicated libraries for Java
bytecode manipulation, like Javassist or BCEL.
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Java Instrumentation
(mechanics)
23
To implement an agent that intercepts classloading
you need to define
– a class implementing the premain method



public static void premain(String agentArguments, 

Instrumentation instrumentation) { ... }
– a class implementing a transformer (which describes how the
bytecode should be transformed)



public class SomeTransformer implements ClassFileTransformer

public byte[] transform(ClassLoader loader, 

String className, Class redefiningClass,

ProtectionDomain domain, byte[] bytes)

throws IllegalClassFormatException { ... }
– you can put both methods in a single class
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Java Instrumentation
(mechanics)
24
– The transform method receives information on the class to be
loaded and can modify its bytecode.



public byte[] transform(ClassLoader loader, 

String className, Class redefiningClass,

ProtectionDomain domain, byte[] bytes)

• returns null if there's no modification, else returns the new
bytecode
• to modify the bytecode you can use a specialised library like
Javassist

– The premain method should add the transformer to the agent:



public static void premain(String agentArguments, 

Instrumentation instrumentation) {

instrumentation.addTransformer(new SomeTransformer()); }

SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Java Instrumentation
(mechanics)
25
– To plug the agent into the JVM and execute it, you need to put
it inside a jar:

jar cvfm MyAgent.jar ManifestFile.txt

MyAgent.class SomeTransformer.class
– The manifest file for this jar has to declare the premain class:

Premain-Class: MyAgent

If the agent modifies the bytecode, this also has to be
declared in the manifest file:

Can-Redefine-Classes: true
– Finally, to instrument a Java program you need to add the
javaagent parameter when you execute the JVM:
> java -javaagent:MyAgent.jar MyProgram
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Examples
1. Instrument a Java program to print a message on
the console whenever a class is loaded by the JVM
2. Instrument a Java program to print all messages
being sent while the program is running
3. Replace the definition of a class at runtime
– even for classes with currently active instances
– for example, to change the behaviour of some messages
understood by those instances
26
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Example 1: Instrument Java code
27
/**
* Just prints a message to stdout before each Class is loaded
* > java -javaagent:LogClassLoad.jar <the_Main_Class_to_execute>
*/
public class LogClassLoad implements ClassFileTransformer {
public static void premain(String options, Instrumentation ins) {
ins.addTransformer(new LogClassLoad());
}
public byte[] transform(ClassLoader loader, String className, Class cBR,
java.security.ProtectionDomain pD, byte[] classfileBuffer)
throws IllegalClassFormatException {
try {
System.out.println("LOADING: " + className);
}
catch (Throwable exc) {
System.err.println(exc);
}
return null; // For this first example no transformation is required :
// we are only logging when classes are loaded
}
}
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Example 1: Instrument Java code
28
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Examples
1. Instrument a Java program to print a message on
the console whenever a class is loaded by the JVM
2. Instrument a Java program to print all messages
being sent while the program is running
3. Replace the definition of a class at runtime
– even for classes with currently active instances
– for example, to change the behaviour of some messages
understood by those instances
29
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Example 2: Print all messages
30
public class TraceMethodCall implements ClassFileTransformer {
public static void premain(String options, Instrumentation ins) {
ins.addTransformer(new TraceMethodCall());
}
public byte[] transform(ClassLoader loader, String className, Class cBR,
java.security.ProtectionDomain pD, byte[] classfileBuffer)
throws IllegalClassFormatException {
try {
if(isSystemClass(className)) return null;
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get(className);
CtMethod[] allmethods = cc.getMethods();
for(CtMethod method : allmethods) {
try {
method.insertBefore("TraceMethodCall.trace();");
} catch (Exception e) { }
}
return cc.toBytecode();
}
catch (Throwable exc) { }
return null; // No transformation required
}
for(CtMethod method : allmethods) {
try {
method.insertBefore("TraceMethodCall.trace();");
} catch (Exception e) { }
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Example 2: Print all messages
31
public class TraceMethodCall implements ClassFileTransformer {
...
/** Check if a class is a system class, based on the package name. **/
public static boolean isSystemClass(String className) { ... }
public static boolean isDottedSystemClass(String className) { ... }
...
public static void trace() {
Throwable thw = new Throwable();
if(thw.getStackTrace().length > 2) {
StackTraceElement stackTo = thw.getStackTrace()[1];
StackTraceElement stackFrom = thw.getStackTrace()[2];
if(!isDottedSystemClass(stackFrom.getClassName())) {
System.out.println(""" + stackFrom.getClassName() + "." 

+ stackFrom.getMethodName() + "" -> " + """ 

+ stackTo.getClassName() + "." + stackTo.getMethodName() + """);
}
}
}
}
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Example 2: Print all messages
32
package reflectionexample;
public class CallStackExample0 {
public static void m1() { m2(); }
public static void m2() { m3(); }
public static void m3() { }
public static void main(String[] args) { m1(); }
}
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Examples
1. Instrument a Java program to print a message on
the console whenever a class is loaded by the JVM
2. Instrument a Java program to print all messages
being sent while the program is running
3. Replace the definition of a class at runtime
– even for classes with currently active instances
– for example, to change the behaviour of some messages
understood by those instances
33
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Example 2: Replacing a class
(goal)
34
/**
* This modified class replaces the original class at run-time.
* The modified class has the same name but is located in a subdirectory.
* It is a clone of the original class where one method was changed.
* Instances of this class will react differently to those messages.
*/
public class SwappedClass {
public void message() {
System.out.println("I am the MODIFIED SwapedClass");
}
}
/**
* This is the original class that will be replaced by a modified copy at run-time.
* The modified copy of this class is located in the "modif" subdirectory.
*/
public class SwappedClass {
public void message() {
System.out.println("I am the original SwapedClass");
}
}
original
modifie
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Example 2: Replacing a class
(mechanics)
■ to dynamically replace a class execute this code:
35
import java.lang.instrument.Instrumentation;
public class SwapClass {
private static Instrumentation instCopy;
public static void premain(String options, Instrumentation inst) {
instCopy = inst;
}
public static Instrumentation getInstrumentation() {
return instCopy;
}
}
SwapClass.getInstrumentation().redefineClasses(...)
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Example 2: Replacing a class
(code)
36
public class SwapTest {
public static void main (String[] args) {
try {
// Create an instance of the class that will be modified
SwappedClass swapped = new SwappedClass();
// Send a message to the instance; the original message should be displayed
swapped.message();
// Now replace the class by a modified version
// 1. read the class definition from file
FileChannel fc =
new FileInputStream(new File("modif/SwappedClass.class")).getChannel();
ByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int)fc.size());
byte[] classBuffer = new byte[buf.capacity()];
buf.get(classBuffer, 0, classBuffer.length);
// 2. use instrumentation API to replace the old class’s definition by the new one
SwapClass.getInstrumentation().redefineClasses(
new ClassDefinition[] {
new ClassDefinition(swapped.getClass(), classBuffer)});
// Send a message to the old instance; the MODIFIED message will be displayed
swapped.message();
}
catch (Exception e){
System.out.println(e);
}
}
}
SwapClass.getInstrumentation().redefineClasses(
new ClassDefinition[] {
new ClassDefinition(swapped.getClass(), classBuffer)});
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Example 2: Replacing a class
(at work)
37
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Reflection in Java: Conclusions
38
■ Benefits
– reflection in Java opens up the structure and the
execution trace of the program;
– the reflection API is (relatively) simple and quite
complete.
■ Drawbacks
– reflection in Java is (mostly) limited to introspection;
– there isn't a clear separation between base and meta-
level;
– reflection in Java has been proved inefficient
Ad

More Related Content

What's hot (20)

Discover Quarkus and GraalVM
Discover Quarkus and GraalVMDiscover Quarkus and GraalVM
Discover Quarkus and GraalVM
Romain Schlick
 
Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19
José Paumard
 
What's new in Java 11
What's new in Java 11What's new in Java 11
What's new in Java 11
Michel Schudel
 
Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)
Marcos García
 
What CloudStackers Need To Know About LINSTOR/DRBD
What CloudStackers Need To Know About LINSTOR/DRBDWhat CloudStackers Need To Know About LINSTOR/DRBD
What CloudStackers Need To Know About LINSTOR/DRBD
ShapeBlue
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVM
Sylvain Wallez
 
Exploring the power of Gradle in android studio - Basics & Beyond
Exploring the power of Gradle in android studio - Basics & BeyondExploring the power of Gradle in android studio - Basics & Beyond
Exploring the power of Gradle in android studio - Basics & Beyond
Kaushal Dhruw
 
GraalVM: Run Programs Faster Everywhere
GraalVM: Run Programs Faster EverywhereGraalVM: Run Programs Faster Everywhere
GraalVM: Run Programs Faster Everywhere
J On The Beach
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage Collection
Azul Systems Inc.
 
Intrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VMIntrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VM
Kris Mok
 
JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013
Vladimir Ivanov
 
Quarkus - a next-generation Kubernetes Native Java framework
Quarkus - a next-generation Kubernetes Native Java frameworkQuarkus - a next-generation Kubernetes Native Java framework
Quarkus - a next-generation Kubernetes Native Java framework
SVDevOps
 
Low level java programming
Low level java programmingLow level java programming
Low level java programming
Peter Lawrey
 
Reliability Guarantees for Apache Kafka
Reliability Guarantees for Apache KafkaReliability Guarantees for Apache Kafka
Reliability Guarantees for Apache Kafka
confluent
 
Java 9 Features
Java 9 FeaturesJava 9 Features
Java 9 Features
NexThoughts Technologies
 
Eventually, Scylla Chooses Consistency
Eventually, Scylla Chooses ConsistencyEventually, Scylla Chooses Consistency
Eventually, Scylla Chooses Consistency
ScyllaDB
 
Gradle
GradleGradle
Gradle
Jadson Santos
 
JVM++: The Graal VM
JVM++: The Graal VMJVM++: The Graal VM
JVM++: The Graal VM
Martin Toshev
 
Quarkus Denmark 2019
Quarkus Denmark 2019Quarkus Denmark 2019
Quarkus Denmark 2019
Max Andersen
 
Disaster Recovery with MirrorMaker 2.0 (Ryanne Dolan, Cloudera) Kafka Summit ...
Disaster Recovery with MirrorMaker 2.0 (Ryanne Dolan, Cloudera) Kafka Summit ...Disaster Recovery with MirrorMaker 2.0 (Ryanne Dolan, Cloudera) Kafka Summit ...
Disaster Recovery with MirrorMaker 2.0 (Ryanne Dolan, Cloudera) Kafka Summit ...
confluent
 
Discover Quarkus and GraalVM
Discover Quarkus and GraalVMDiscover Quarkus and GraalVM
Discover Quarkus and GraalVM
Romain Schlick
 
Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19
José Paumard
 
Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)
Marcos García
 
What CloudStackers Need To Know About LINSTOR/DRBD
What CloudStackers Need To Know About LINSTOR/DRBDWhat CloudStackers Need To Know About LINSTOR/DRBD
What CloudStackers Need To Know About LINSTOR/DRBD
ShapeBlue
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVM
Sylvain Wallez
 
Exploring the power of Gradle in android studio - Basics & Beyond
Exploring the power of Gradle in android studio - Basics & BeyondExploring the power of Gradle in android studio - Basics & Beyond
Exploring the power of Gradle in android studio - Basics & Beyond
Kaushal Dhruw
 
GraalVM: Run Programs Faster Everywhere
GraalVM: Run Programs Faster EverywhereGraalVM: Run Programs Faster Everywhere
GraalVM: Run Programs Faster Everywhere
J On The Beach
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage Collection
Azul Systems Inc.
 
Intrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VMIntrinsic Methods in HotSpot VM
Intrinsic Methods in HotSpot VM
Kris Mok
 
JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013
Vladimir Ivanov
 
Quarkus - a next-generation Kubernetes Native Java framework
Quarkus - a next-generation Kubernetes Native Java frameworkQuarkus - a next-generation Kubernetes Native Java framework
Quarkus - a next-generation Kubernetes Native Java framework
SVDevOps
 
Low level java programming
Low level java programmingLow level java programming
Low level java programming
Peter Lawrey
 
Reliability Guarantees for Apache Kafka
Reliability Guarantees for Apache KafkaReliability Guarantees for Apache Kafka
Reliability Guarantees for Apache Kafka
confluent
 
Eventually, Scylla Chooses Consistency
Eventually, Scylla Chooses ConsistencyEventually, Scylla Chooses Consistency
Eventually, Scylla Chooses Consistency
ScyllaDB
 
Quarkus Denmark 2019
Quarkus Denmark 2019Quarkus Denmark 2019
Quarkus Denmark 2019
Max Andersen
 
Disaster Recovery with MirrorMaker 2.0 (Ryanne Dolan, Cloudera) Kafka Summit ...
Disaster Recovery with MirrorMaker 2.0 (Ryanne Dolan, Cloudera) Kafka Summit ...Disaster Recovery with MirrorMaker 2.0 (Ryanne Dolan, Cloudera) Kafka Summit ...
Disaster Recovery with MirrorMaker 2.0 (Ryanne Dolan, Cloudera) Kafka Summit ...
confluent
 

Similar to Advanced Reflection in Java (20)

Java reflection
Java reflectionJava reflection
Java reflection
Ranjith Chaz
 
Jersey Guice AOP
Jersey Guice AOPJersey Guice AOP
Jersey Guice AOP
Domenico Briganti
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
Shawn Brito
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
PavanKumar823345
 
Week 8,9,10 of lab of data communication .pdf
Week 8,9,10 of lab of data communication .pdfWeek 8,9,10 of lab of data communication .pdf
Week 8,9,10 of lab of data communication .pdf
nimrastorage123
 
Nhibernate Part 2
Nhibernate   Part 2Nhibernate   Part 2
Nhibernate Part 2
guest075fec
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docx
ssuser1c8ca21
 
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
Sudarshan Dhondaley
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
Pramod Kumar
 
Introduction+To+Java+Concurrency
Introduction+To+Java+ConcurrencyIntroduction+To+Java+Concurrency
Introduction+To+Java+Concurrency
King's College London
 
Inheritance
InheritanceInheritance
Inheritance
Mavoori Soshmitha
 
المحاضرة الثامنة: تراكيب البيانات الطابور
المحاضرة الثامنة: تراكيب البيانات الطابورالمحاضرة الثامنة: تراكيب البيانات الطابور
المحاضرة الثامنة: تراكيب البيانات الطابور
Mahmoud Alfarra
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
OUM SAOKOSAL
 
DBI-Assisted Android Application Reverse Engineering
DBI-Assisted Android Application Reverse EngineeringDBI-Assisted Android Application Reverse Engineering
DBI-Assisted Android Application Reverse Engineering
Sahil Dhar
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
rohit_gupta_mrt
 
Exploring lambdas and invokedynamic for embedded systems
Exploring lambdas and invokedynamic for embedded systemsExploring lambdas and invokedynamic for embedded systems
Exploring lambdas and invokedynamic for embedded systems
InfinIT - Innovationsnetværket for it
 
Maf3 - Part 1
Maf3 - Part 1Maf3 - Part 1
Maf3 - Part 1
Paolo Quadrani
 
Design patterns(red)
Design patterns(red)Design patterns(red)
Design patterns(red)
Fahad A. Shaikh
 
04 threads
04 threads04 threads
04 threads
ambar khetan
 
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock Tutorial
Sbin m
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
Shawn Brito
 
Week 8,9,10 of lab of data communication .pdf
Week 8,9,10 of lab of data communication .pdfWeek 8,9,10 of lab of data communication .pdf
Week 8,9,10 of lab of data communication .pdf
nimrastorage123
 
Nhibernate Part 2
Nhibernate   Part 2Nhibernate   Part 2
Nhibernate Part 2
guest075fec
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docx
ssuser1c8ca21
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
Pramod Kumar
 
المحاضرة الثامنة: تراكيب البيانات الطابور
المحاضرة الثامنة: تراكيب البيانات الطابورالمحاضرة الثامنة: تراكيب البيانات الطابور
المحاضرة الثامنة: تراكيب البيانات الطابور
Mahmoud Alfarra
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
OUM SAOKOSAL
 
DBI-Assisted Android Application Reverse Engineering
DBI-Assisted Android Application Reverse EngineeringDBI-Assisted Android Application Reverse Engineering
DBI-Assisted Android Application Reverse Engineering
Sahil Dhar
 
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock Tutorial
Sbin m
 
Ad

More from kim.mens (20)

Software Maintenance and Evolution
Software Maintenance and EvolutionSoftware Maintenance and Evolution
Software Maintenance and Evolution
kim.mens
 
Context-Oriented Programming
Context-Oriented ProgrammingContext-Oriented Programming
Context-Oriented Programming
kim.mens
 
Software Reuse and Object-Oriented Programming
Software Reuse and Object-Oriented ProgrammingSoftware Reuse and Object-Oriented Programming
Software Reuse and Object-Oriented Programming
kim.mens
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smells
kim.mens
 
Object-Oriented Design Heuristics
Object-Oriented Design HeuristicsObject-Oriented Design Heuristics
Object-Oriented Design Heuristics
kim.mens
 
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
kim.mens
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
kim.mens
 
Domain Modelling
Domain ModellingDomain Modelling
Domain Modelling
kim.mens
 
Object-Oriented Application Frameworks
Object-Oriented Application FrameworksObject-Oriented Application Frameworks
Object-Oriented Application Frameworks
kim.mens
 
Towards a Context-Oriented Software Implementation Framework
Towards a Context-Oriented Software Implementation FrameworkTowards a Context-Oriented Software Implementation Framework
Towards a Context-Oriented Software Implementation Framework
kim.mens
 
Towards a Taxonomy of Context-Aware Software Variabilty Approaches
Towards a Taxonomy of Context-Aware Software Variabilty ApproachesTowards a Taxonomy of Context-Aware Software Variabilty Approaches
Towards a Taxonomy of Context-Aware Software Variabilty Approaches
kim.mens
 
Breaking the Walls: A Unified Vision on Context-Oriented Software Engineering
Breaking the Walls: A Unified Vision on Context-Oriented Software EngineeringBreaking the Walls: A Unified Vision on Context-Oriented Software Engineering
Breaking the Walls: A Unified Vision on Context-Oriented Software Engineering
kim.mens
 
Context-oriented programming
Context-oriented programmingContext-oriented programming
Context-oriented programming
kim.mens
 
Basics of reflection
Basics of reflectionBasics of reflection
Basics of reflection
kim.mens
 
Reflection in Ruby
Reflection in RubyReflection in Ruby
Reflection in Ruby
kim.mens
 
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
kim.mens
 
Introduction to Smalltalk
Introduction to SmalltalkIntroduction to Smalltalk
Introduction to Smalltalk
kim.mens
 
A gentle introduction to reflection
A gentle introduction to reflectionA gentle introduction to reflection
A gentle introduction to reflection
kim.mens
 
Managing the Evolution of Information Systems with Intensional Views and Rela...
Managing the Evolution of Information Systems with Intensional Views and Rela...Managing the Evolution of Information Systems with Intensional Views and Rela...
Managing the Evolution of Information Systems with Intensional Views and Rela...
kim.mens
 
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
kim.mens
 
Software Maintenance and Evolution
Software Maintenance and EvolutionSoftware Maintenance and Evolution
Software Maintenance and Evolution
kim.mens
 
Context-Oriented Programming
Context-Oriented ProgrammingContext-Oriented Programming
Context-Oriented Programming
kim.mens
 
Software Reuse and Object-Oriented Programming
Software Reuse and Object-Oriented ProgrammingSoftware Reuse and Object-Oriented Programming
Software Reuse and Object-Oriented Programming
kim.mens
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smells
kim.mens
 
Object-Oriented Design Heuristics
Object-Oriented Design HeuristicsObject-Oriented Design Heuristics
Object-Oriented Design Heuristics
kim.mens
 
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
kim.mens
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
kim.mens
 
Domain Modelling
Domain ModellingDomain Modelling
Domain Modelling
kim.mens
 
Object-Oriented Application Frameworks
Object-Oriented Application FrameworksObject-Oriented Application Frameworks
Object-Oriented Application Frameworks
kim.mens
 
Towards a Context-Oriented Software Implementation Framework
Towards a Context-Oriented Software Implementation FrameworkTowards a Context-Oriented Software Implementation Framework
Towards a Context-Oriented Software Implementation Framework
kim.mens
 
Towards a Taxonomy of Context-Aware Software Variabilty Approaches
Towards a Taxonomy of Context-Aware Software Variabilty ApproachesTowards a Taxonomy of Context-Aware Software Variabilty Approaches
Towards a Taxonomy of Context-Aware Software Variabilty Approaches
kim.mens
 
Breaking the Walls: A Unified Vision on Context-Oriented Software Engineering
Breaking the Walls: A Unified Vision on Context-Oriented Software EngineeringBreaking the Walls: A Unified Vision on Context-Oriented Software Engineering
Breaking the Walls: A Unified Vision on Context-Oriented Software Engineering
kim.mens
 
Context-oriented programming
Context-oriented programmingContext-oriented programming
Context-oriented programming
kim.mens
 
Basics of reflection
Basics of reflectionBasics of reflection
Basics of reflection
kim.mens
 
Reflection in Ruby
Reflection in RubyReflection in Ruby
Reflection in Ruby
kim.mens
 
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
kim.mens
 
Introduction to Smalltalk
Introduction to SmalltalkIntroduction to Smalltalk
Introduction to Smalltalk
kim.mens
 
A gentle introduction to reflection
A gentle introduction to reflectionA gentle introduction to reflection
A gentle introduction to reflection
kim.mens
 
Managing the Evolution of Information Systems with Intensional Views and Rela...
Managing the Evolution of Information Systems with Intensional Views and Rela...Managing the Evolution of Information Systems with Intensional Views and Rela...
Managing the Evolution of Information Systems with Intensional Views and Rela...
kim.mens
 
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
kim.mens
 
Ad

Recently uploaded (20)

Discrete choice experiments: Environmental Improvements to Airthrey Loch Lake...
Discrete choice experiments: Environmental Improvements to Airthrey Loch Lake...Discrete choice experiments: Environmental Improvements to Airthrey Loch Lake...
Discrete choice experiments: Environmental Improvements to Airthrey Loch Lake...
Professional Content Writing's
 
Introduction to Black Hole and how its formed
Introduction to Black Hole and how its formedIntroduction to Black Hole and how its formed
Introduction to Black Hole and how its formed
MSafiullahALawi
 
Astrobiological implications of the stability andreactivity of peptide nuclei...
Astrobiological implications of the stability andreactivity of peptide nuclei...Astrobiological implications of the stability andreactivity of peptide nuclei...
Astrobiological implications of the stability andreactivity of peptide nuclei...
Sérgio Sacani
 
Eric Schott- Environment, Animal and Human Health (3).pptx
Eric Schott- Environment, Animal and Human Health (3).pptxEric Schott- Environment, Animal and Human Health (3).pptx
Eric Schott- Environment, Animal and Human Health (3).pptx
ttalbert1
 
TOI-421 b: A Hot Sub-Neptune with a Haze-free, Low Mean Molecular Weight Atmo...
TOI-421 b: A Hot Sub-Neptune with a Haze-free, Low Mean Molecular Weight Atmo...TOI-421 b: A Hot Sub-Neptune with a Haze-free, Low Mean Molecular Weight Atmo...
TOI-421 b: A Hot Sub-Neptune with a Haze-free, Low Mean Molecular Weight Atmo...
Sérgio Sacani
 
Pharmacologically active constituents.pdf
Pharmacologically active constituents.pdfPharmacologically active constituents.pdf
Pharmacologically active constituents.pdf
Nistarini College, Purulia (W.B) India
 
Somato_Sensory _ somatomotor_Nervous_System.pptx
Somato_Sensory _ somatomotor_Nervous_System.pptxSomato_Sensory _ somatomotor_Nervous_System.pptx
Somato_Sensory _ somatomotor_Nervous_System.pptx
klynct
 
Carboxylic-Acid-Derivatives.lecture.presentation
Carboxylic-Acid-Derivatives.lecture.presentationCarboxylic-Acid-Derivatives.lecture.presentation
Carboxylic-Acid-Derivatives.lecture.presentation
GLAEXISAJULGA
 
Secondary metabolite ,Plants and Health Care
Secondary metabolite ,Plants and Health CareSecondary metabolite ,Plants and Health Care
Secondary metabolite ,Plants and Health Care
Nistarini College, Purulia (W.B) India
 
An upper limit to the lifetime of stellar remnants from gravitational pair pr...
An upper limit to the lifetime of stellar remnants from gravitational pair pr...An upper limit to the lifetime of stellar remnants from gravitational pair pr...
An upper limit to the lifetime of stellar remnants from gravitational pair pr...
Sérgio Sacani
 
ICAI OpenGov Lab: A Quick Introduction | AI for Open Government
ICAI OpenGov Lab: A Quick Introduction | AI for Open GovernmentICAI OpenGov Lab: A Quick Introduction | AI for Open Government
ICAI OpenGov Lab: A Quick Introduction | AI for Open Government
David Graus
 
A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...
A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...
A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...
Sérgio Sacani
 
Subject name: Introduction to psychology
Subject name: Introduction to psychologySubject name: Introduction to psychology
Subject name: Introduction to psychology
beebussy155
 
Batteries and fuel cells for btech first year
Batteries and fuel cells for btech first yearBatteries and fuel cells for btech first year
Batteries and fuel cells for btech first year
MithilPillai1
 
Controls over genes.ppt. Gene Expression
Controls over genes.ppt. Gene ExpressionControls over genes.ppt. Gene Expression
Controls over genes.ppt. Gene Expression
NABIHANAEEM2
 
Study in Pink (forensic case study of Death)
Study in Pink (forensic case study of Death)Study in Pink (forensic case study of Death)
Study in Pink (forensic case study of Death)
memesologiesxd
 
Reticular formation_groups_organization_
Reticular formation_groups_organization_Reticular formation_groups_organization_
Reticular formation_groups_organization_
klynct
 
Siver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptx
Siver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptxSiver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptx
Siver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptx
PriyaAntil3
 
Hypothalamus_structure_nuclei_ functions.pptx
Hypothalamus_structure_nuclei_ functions.pptxHypothalamus_structure_nuclei_ functions.pptx
Hypothalamus_structure_nuclei_ functions.pptx
klynct
 
Preclinical Advances in Nuclear Neurology.pptx
Preclinical Advances in Nuclear Neurology.pptxPreclinical Advances in Nuclear Neurology.pptx
Preclinical Advances in Nuclear Neurology.pptx
MahitaLaveti
 
Discrete choice experiments: Environmental Improvements to Airthrey Loch Lake...
Discrete choice experiments: Environmental Improvements to Airthrey Loch Lake...Discrete choice experiments: Environmental Improvements to Airthrey Loch Lake...
Discrete choice experiments: Environmental Improvements to Airthrey Loch Lake...
Professional Content Writing's
 
Introduction to Black Hole and how its formed
Introduction to Black Hole and how its formedIntroduction to Black Hole and how its formed
Introduction to Black Hole and how its formed
MSafiullahALawi
 
Astrobiological implications of the stability andreactivity of peptide nuclei...
Astrobiological implications of the stability andreactivity of peptide nuclei...Astrobiological implications of the stability andreactivity of peptide nuclei...
Astrobiological implications of the stability andreactivity of peptide nuclei...
Sérgio Sacani
 
Eric Schott- Environment, Animal and Human Health (3).pptx
Eric Schott- Environment, Animal and Human Health (3).pptxEric Schott- Environment, Animal and Human Health (3).pptx
Eric Schott- Environment, Animal and Human Health (3).pptx
ttalbert1
 
TOI-421 b: A Hot Sub-Neptune with a Haze-free, Low Mean Molecular Weight Atmo...
TOI-421 b: A Hot Sub-Neptune with a Haze-free, Low Mean Molecular Weight Atmo...TOI-421 b: A Hot Sub-Neptune with a Haze-free, Low Mean Molecular Weight Atmo...
TOI-421 b: A Hot Sub-Neptune with a Haze-free, Low Mean Molecular Weight Atmo...
Sérgio Sacani
 
Somato_Sensory _ somatomotor_Nervous_System.pptx
Somato_Sensory _ somatomotor_Nervous_System.pptxSomato_Sensory _ somatomotor_Nervous_System.pptx
Somato_Sensory _ somatomotor_Nervous_System.pptx
klynct
 
Carboxylic-Acid-Derivatives.lecture.presentation
Carboxylic-Acid-Derivatives.lecture.presentationCarboxylic-Acid-Derivatives.lecture.presentation
Carboxylic-Acid-Derivatives.lecture.presentation
GLAEXISAJULGA
 
An upper limit to the lifetime of stellar remnants from gravitational pair pr...
An upper limit to the lifetime of stellar remnants from gravitational pair pr...An upper limit to the lifetime of stellar remnants from gravitational pair pr...
An upper limit to the lifetime of stellar remnants from gravitational pair pr...
Sérgio Sacani
 
ICAI OpenGov Lab: A Quick Introduction | AI for Open Government
ICAI OpenGov Lab: A Quick Introduction | AI for Open GovernmentICAI OpenGov Lab: A Quick Introduction | AI for Open Government
ICAI OpenGov Lab: A Quick Introduction | AI for Open Government
David Graus
 
A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...
A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...
A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...
Sérgio Sacani
 
Subject name: Introduction to psychology
Subject name: Introduction to psychologySubject name: Introduction to psychology
Subject name: Introduction to psychology
beebussy155
 
Batteries and fuel cells for btech first year
Batteries and fuel cells for btech first yearBatteries and fuel cells for btech first year
Batteries and fuel cells for btech first year
MithilPillai1
 
Controls over genes.ppt. Gene Expression
Controls over genes.ppt. Gene ExpressionControls over genes.ppt. Gene Expression
Controls over genes.ppt. Gene Expression
NABIHANAEEM2
 
Study in Pink (forensic case study of Death)
Study in Pink (forensic case study of Death)Study in Pink (forensic case study of Death)
Study in Pink (forensic case study of Death)
memesologiesxd
 
Reticular formation_groups_organization_
Reticular formation_groups_organization_Reticular formation_groups_organization_
Reticular formation_groups_organization_
klynct
 
Siver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptx
Siver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptxSiver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptx
Siver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptx
PriyaAntil3
 
Hypothalamus_structure_nuclei_ functions.pptx
Hypothalamus_structure_nuclei_ functions.pptxHypothalamus_structure_nuclei_ functions.pptx
Hypothalamus_structure_nuclei_ functions.pptx
klynct
 
Preclinical Advances in Nuclear Neurology.pptx
Preclinical Advances in Nuclear Neurology.pptxPreclinical Advances in Nuclear Neurology.pptx
Preclinical Advances in Nuclear Neurology.pptx
MahitaLaveti
 

Advanced Reflection in Java

  • 1. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Lecture 10 :
 Advanced Reflection in Java LSINF 2335 Programming Paradigms Prof. Kim Mens UCL / EPL / INGI 
 (Slides partly based on chapter 5 of the book “Java Reflection in Action” and on slides by Pr. Walter Cazzola)
  • 2. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Advanced reflective features ■ Dynamic proxies – Proxy – InvocationHandler ■ Call stack introspection – Throwable – StackTraceElement ■ Instrumentation – java.lang.instrument ■ Conclusion 2
  • 3. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Proxy Design Pattern 3 ■ The proxy pattern defines a proxy as a surrogate for another object to control access to it – a proxy keeps a reference to the real object, – is substitutable with it (identical interface) – and delegates requests to it ■ Typical examples of usage of proxies: – local representation of remote objects – delay of expensive operations – access protection for secure objects – ... Client action() Subject action() Proxy action() RealSubject delegate
  • 4. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Dynamic proxies ■ To easily implement proxies, Java 1.3 introduced the Dynamic Proxy API ■ A dynamic proxy requires – an instance of the Proxy class – a proxy interface • this is the interface that is implemented by the proxy class • interestingly, one proxy can implement multiple interfaces – each proxy instance has an InvocationHandler 4Proxy InvocationHandler delegates to IProxy implements ProxyHandler implements
  • 5. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. InvocationHandler ■ Each proxy instance has an InvocationHandler – The invocation handler determines how to treat messages that are sent to the proxy instance – When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke() method of its invocation handler
 
 Object invoke(Object proxy, Method m, Object[] args) 5 Proxy invoke(Object, Method, Object[]) : Object <<interface>> InvocationHandlerdelegates to
  • 6. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example: an InvocationHandler to trace method calls 6 public class TraceHandler implements InvocationHandler { private Object baseObject; // the real object the proxy delegates to public TraceHandler(Object base) { baseObject = base; } public Object invoke(Object proxy, Method m, Object[] args) { Object result = null; // result to be returned by the method try { System.out.println("before " + m.getName()); result = m.invoke(baseObject, args); System.out.println("after " + m.getName()); } catch (Exception e) {
 e.printStackTrace(); } return result; }
 }
  • 7. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Proxy class ■ Provides static methods for creating dynamic proxy classes and instances ■ Is also the superclass of all dynamic proxy classes created by those methods ■ To create a proxy for some class Foo
 InvocationHandler handler = new MyInvocationHandler(...); Class proxyClass = Proxy.getProxyClass( Foo.class.getClassLoader(), new Class[] { Foo.class }); Foo f = (Foo) proxyClass. getConstructor(new Class[] { InvocationHandler.class }). newInstance(new Object[] { handler }); ■ or more simply:
 Foo f = (Foo) Proxy.
 newProxyInstance( Foo.class.getClassLoader(), new Class[] { Foo.class }, handler ); 7
  • 8. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example: a Proxy that traces method calls 8 public class Component1 implements IComponent { Color myColor; public Color getColor() { System.out.println("Inside the getColor() method of Component1."); return myColor; } public void setColor(Color color) { myColor = color; System.out.println("The color of component 1 has been set."); } } public interface IComponent { public Color getColor(); public void setColor(Color color); } interface shared by real object and proxy real object to which proxy will delegate
  • 9. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example: a Proxy that traces method calls 9 IComponent c1 = new Component1(new Color(0)); InvocationHandler th = new TraceHandler(c1); IComponent proxy = (IComponent) Proxy.newProxyInstance( c1.getClass().getClassLoader(),
 c1.getClass().getInterfaces(), th); /* standard call */ c1.getColor(); /* traced call */ proxy.getColor();
  • 10. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. How Java creates a proxy 10 Bibliography Examples: Tracing and Proxy Chaining. Java’s Dynamic Proxy. How Java Creates a Proxy. extends implements implements implements $IPoint InvocationHandlerIPoint Proxy Point TraceHandler Classes Interfaces p thpth - int x - int y + getX(): int + getY(): int + getX(): int + getY(): int + getX(): int + getY(): int - Object base + invoke(): Object - InvocationHandler i + invoke(): Object Generated on-the-fly
  • 11. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Advanced reflective features ■ Dynamic proxies – Proxy – InvocationHandler ■ Call stack introspection – Throwable – StackTraceElement ■ Instrumentation – java.lang.instrument ■ Conclusion 11
  • 12. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Call Stack Introspection ■ State introspection ■ Call stack ■ Reifying the call stack – Throwable – StackTraceElement ■ Examples: – printing the call stack – show warnings for unimplemented methods 12
  • 13. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. State Introspection 13 ■ Introspection is not only application structure introspection ■ Information about the program execution can be introspected as well – the execution state – the call stack ■ Each thread has a call stack consisting of stack frames ■ Call stack introspection allows a thread to examine its context – the execution trace and the current frame
  • 14. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Call Stack 1. package reflectionexample; 2. 3. public class Example { 4. 5. public static void m1() { 6. m2(); 7. } 8. 9. public static void m2() { 10. m3(); 11. } 12. 13. public static void m3() { 14. // do something 15. } 16. 17. public static void main(String[] args) { 18. m1(); 19. } 20. 21. } class: Example method: main line: 18 class: Example method: m1 line: 6 Call Stack: class: Example method: m2 line: 10 class: Example method: m3 line: 14 (m3)
  • 15. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Reifying the call stack : Throwable 15 ■ In Java there is no accessible CallStack meta-object ■ But... – When an instance of Throwable is created, the call stack is saved as an array of StackTraceElement – By writing: new Throwable().getStackTrace() – This gives us access to a representation of the call stack at the moment when Throwable was created. – The getStackTrace() method returns the current call stack as an array of StackTraceElement. • The first frame (position 0) is the current frame ■ Only introspection
  • 16. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example 1: print the call stack 16 public class CallStackExample1 { public static void m1() { m2(); } public static void m2() { m3(); } public static void m3() { StackTraceElement[] stack = new Throwable().getStackTrace(); for (int i = 0; i < stack.length; i++) { System.out.println(stack[i]); } } public static void main(String[] args) { m1(); }
  • 17. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example 2: show warnings for unimplemented methods ■ How to create an auxiliary method to be used as placeholder for unimplemented methods? – I have a method todo() remaining to be implemented – I provide the following dummy implementation – When calling that method todo() I want to get a warning: – as well as a warning message on the Console: Warning: reflectionexample.CallStackExample2.todo(CallStackExample2.java:8) () : this method remains to be implemented public static void todo() { toBeImplemented(); }
  • 18. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. 18 Example 2: show warnings for methods to be implemented public class CallStackExample2 { public static void todo() { toBeImplemented(); } public static void toBeImplemented() { // Get the stack element referring to the method calling this one StackTraceElement el = new Throwable().getStackTrace()[1]; // Show a dialog box with information on the method remaining to be implemented String msg = el.toString() + "() : this method remains to be implemented"; // Show this message in a dialog box JOptionPane.showMessageDialog(null, msg, "Erreur", JOptionPane.ERROR_MESSAGE); // Print this warning on the console System.err.println("Warning: " + msg); } public static void main(String[] args) { todo(); } }
  • 19. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Reifying the call stack : StackTraceElement ■ From a frame we can get: – getFileName() : the filename containing the execution point – getLineNumber() : the line number where the call occurs – getClassName() : the name of the class containing the
 execution point – getMethodName() : the name of the method containing the 
 execution point 19 “myPackage.Example.m3(Example.java:14)” StackTraceElement: class: Example method: m3 line: 14 filename: Example.java
  • 20. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Advanced reflective features ■ Dynamic proxies – Proxy – InvocationHandler ■ Call stack introspection – Throwable – StackTraceElement ■ Instrumentation – java.lang.instrument ■ Conclusion 20
  • 21. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Suppose we want to... 1. Instrument a Java program to print a message on the console whenever a class is loaded by the JVM 2. Instrument a Java program to print all messages being sent while the program is running 3. Replace the definition of a class at runtime – even for classes with currently active instances – for example, to change the behaviour of some messages understood by those instances 21 ... then Java instrumentation may be your answer
  • 22. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Java Instrumentation (goal) 22 ■ java.lang.instrument – Provides services that allow Java programming language agents to instruments programs running on the JVM. – This instrumentation is achieved by modifying byte-code. – Allows you to create agents, that run embedded in a JVM and intercept the classloading process, to • monitor the classloading process • modify the bytecode of classes ■ Can be combined with dedicated libraries for Java bytecode manipulation, like Javassist or BCEL.
  • 23. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Java Instrumentation (mechanics) 23 To implement an agent that intercepts classloading you need to define – a class implementing the premain method
 
 public static void premain(String agentArguments, 
 Instrumentation instrumentation) { ... } – a class implementing a transformer (which describes how the bytecode should be transformed)
 
 public class SomeTransformer implements ClassFileTransformer
 public byte[] transform(ClassLoader loader, 
 String className, Class redefiningClass,
 ProtectionDomain domain, byte[] bytes)
 throws IllegalClassFormatException { ... } – you can put both methods in a single class
  • 24. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Java Instrumentation (mechanics) 24 – The transform method receives information on the class to be loaded and can modify its bytecode.
 
 public byte[] transform(ClassLoader loader, 
 String className, Class redefiningClass,
 ProtectionDomain domain, byte[] bytes)
 • returns null if there's no modification, else returns the new bytecode • to modify the bytecode you can use a specialised library like Javassist
 – The premain method should add the transformer to the agent:
 
 public static void premain(String agentArguments, 
 Instrumentation instrumentation) {
 instrumentation.addTransformer(new SomeTransformer()); }

  • 25. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Java Instrumentation (mechanics) 25 – To plug the agent into the JVM and execute it, you need to put it inside a jar:
 jar cvfm MyAgent.jar ManifestFile.txt
 MyAgent.class SomeTransformer.class – The manifest file for this jar has to declare the premain class:
 Premain-Class: MyAgent
 If the agent modifies the bytecode, this also has to be declared in the manifest file:
 Can-Redefine-Classes: true – Finally, to instrument a Java program you need to add the javaagent parameter when you execute the JVM: > java -javaagent:MyAgent.jar MyProgram
  • 26. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Examples 1. Instrument a Java program to print a message on the console whenever a class is loaded by the JVM 2. Instrument a Java program to print all messages being sent while the program is running 3. Replace the definition of a class at runtime – even for classes with currently active instances – for example, to change the behaviour of some messages understood by those instances 26
  • 27. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example 1: Instrument Java code 27 /** * Just prints a message to stdout before each Class is loaded * > java -javaagent:LogClassLoad.jar <the_Main_Class_to_execute> */ public class LogClassLoad implements ClassFileTransformer { public static void premain(String options, Instrumentation ins) { ins.addTransformer(new LogClassLoad()); } public byte[] transform(ClassLoader loader, String className, Class cBR, java.security.ProtectionDomain pD, byte[] classfileBuffer) throws IllegalClassFormatException { try { System.out.println("LOADING: " + className); } catch (Throwable exc) { System.err.println(exc); } return null; // For this first example no transformation is required : // we are only logging when classes are loaded } }
  • 29. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Examples 1. Instrument a Java program to print a message on the console whenever a class is loaded by the JVM 2. Instrument a Java program to print all messages being sent while the program is running 3. Replace the definition of a class at runtime – even for classes with currently active instances – for example, to change the behaviour of some messages understood by those instances 29
  • 30. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example 2: Print all messages 30 public class TraceMethodCall implements ClassFileTransformer { public static void premain(String options, Instrumentation ins) { ins.addTransformer(new TraceMethodCall()); } public byte[] transform(ClassLoader loader, String className, Class cBR, java.security.ProtectionDomain pD, byte[] classfileBuffer) throws IllegalClassFormatException { try { if(isSystemClass(className)) return null; ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get(className); CtMethod[] allmethods = cc.getMethods(); for(CtMethod method : allmethods) { try { method.insertBefore("TraceMethodCall.trace();"); } catch (Exception e) { } } return cc.toBytecode(); } catch (Throwable exc) { } return null; // No transformation required } for(CtMethod method : allmethods) { try { method.insertBefore("TraceMethodCall.trace();"); } catch (Exception e) { } import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod;
  • 31. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example 2: Print all messages 31 public class TraceMethodCall implements ClassFileTransformer { ... /** Check if a class is a system class, based on the package name. **/ public static boolean isSystemClass(String className) { ... } public static boolean isDottedSystemClass(String className) { ... } ... public static void trace() { Throwable thw = new Throwable(); if(thw.getStackTrace().length > 2) { StackTraceElement stackTo = thw.getStackTrace()[1]; StackTraceElement stackFrom = thw.getStackTrace()[2]; if(!isDottedSystemClass(stackFrom.getClassName())) { System.out.println(""" + stackFrom.getClassName() + "." 
 + stackFrom.getMethodName() + "" -> " + """ 
 + stackTo.getClassName() + "." + stackTo.getMethodName() + """); } } } }
  • 32. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example 2: Print all messages 32 package reflectionexample; public class CallStackExample0 { public static void m1() { m2(); } public static void m2() { m3(); } public static void m3() { } public static void main(String[] args) { m1(); } }
  • 33. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Examples 1. Instrument a Java program to print a message on the console whenever a class is loaded by the JVM 2. Instrument a Java program to print all messages being sent while the program is running 3. Replace the definition of a class at runtime – even for classes with currently active instances – for example, to change the behaviour of some messages understood by those instances 33
  • 34. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example 2: Replacing a class (goal) 34 /** * This modified class replaces the original class at run-time. * The modified class has the same name but is located in a subdirectory. * It is a clone of the original class where one method was changed. * Instances of this class will react differently to those messages. */ public class SwappedClass { public void message() { System.out.println("I am the MODIFIED SwapedClass"); } } /** * This is the original class that will be replaced by a modified copy at run-time. * The modified copy of this class is located in the "modif" subdirectory. */ public class SwappedClass { public void message() { System.out.println("I am the original SwapedClass"); } } original modifie
  • 35. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example 2: Replacing a class (mechanics) ■ to dynamically replace a class execute this code: 35 import java.lang.instrument.Instrumentation; public class SwapClass { private static Instrumentation instCopy; public static void premain(String options, Instrumentation inst) { instCopy = inst; } public static Instrumentation getInstrumentation() { return instCopy; } } SwapClass.getInstrumentation().redefineClasses(...)
  • 36. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example 2: Replacing a class (code) 36 public class SwapTest { public static void main (String[] args) { try { // Create an instance of the class that will be modified SwappedClass swapped = new SwappedClass(); // Send a message to the instance; the original message should be displayed swapped.message(); // Now replace the class by a modified version // 1. read the class definition from file FileChannel fc = new FileInputStream(new File("modif/SwappedClass.class")).getChannel(); ByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int)fc.size()); byte[] classBuffer = new byte[buf.capacity()]; buf.get(classBuffer, 0, classBuffer.length); // 2. use instrumentation API to replace the old class’s definition by the new one SwapClass.getInstrumentation().redefineClasses( new ClassDefinition[] { new ClassDefinition(swapped.getClass(), classBuffer)}); // Send a message to the old instance; the MODIFIED message will be displayed swapped.message(); } catch (Exception e){ System.out.println(e); } } } SwapClass.getInstrumentation().redefineClasses( new ClassDefinition[] { new ClassDefinition(swapped.getClass(), classBuffer)});
  • 38. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Reflection in Java: Conclusions 38 ■ Benefits – reflection in Java opens up the structure and the execution trace of the program; – the reflection API is (relatively) simple and quite complete. ■ Drawbacks – reflection in Java is (mostly) limited to introspection; – there isn't a clear separation between base and meta- level; – reflection in Java has been proved inefficient
  翻译: