SlideShare une entreprise Scribd logo
1  sur  38
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump AnalysisDmitry Buzdin
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questionsGradeup
 
The JVM is your friend
The JVM is your friendThe JVM is your friend
The JVM is your friendKai Koenig
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz SAurabh PRajapati
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 
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 & BeyondKaushal Dhruw
 
Implementing a JavaScript Engine
Implementing a JavaScript EngineImplementing a JavaScript Engine
Implementing a JavaScript EngineKris Mok
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobGaruda Trainings
 
Graal and Truffle: One VM to Rule Them All
Graal and Truffle: One VM to Rule Them AllGraal and Truffle: One VM to Rule Them All
Graal and Truffle: One VM to Rule Them AllThomas Wuerthinger
 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programmingElizabeth Thomas
 

Tendances (20)

Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump Analysis
 
Java.util
Java.utilJava.util
Java.util
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questions
 
The JVM is your friend
The JVM is your friendThe JVM is your friend
The JVM is your friend
 
JVM
JVMJVM
JVM
 
Threading in C#
Threading in C#Threading in C#
Threading in C#
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Inside the jvm
Inside the jvmInside the jvm
Inside the jvm
 
Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz
 
Evolution Of Java
Evolution Of JavaEvolution Of Java
Evolution Of Java
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Final keyword
Final keywordFinal keyword
Final keyword
 
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
 
Implementing a JavaScript Engine
Implementing a JavaScript EngineImplementing a JavaScript Engine
Implementing a JavaScript Engine
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
 
Java exception
Java exception Java exception
Java exception
 
Graal and Truffle: One VM to Rule Them All
Graal and Truffle: One VM to Rule Them AllGraal and Truffle: One VM to Rule Them All
Graal and Truffle: One VM to Rule Them All
 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programming
 

Similaire à JavaProgReflectAdvFeat

Java design patterns
Java design patternsJava design patterns
Java design patternsShawn Brito
 
Nhibernate Part 2
Nhibernate   Part 2Nhibernate   Part 2
Nhibernate Part 2guest075fec
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docxssuser1c8ca21
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and UtilitiesPramod Kumar
 
المحاضرة الثامنة: تراكيب البيانات الطابور
المحاضرة الثامنة: تراكيب البيانات الطابورالمحاضرة الثامنة: تراكيب البيانات الطابور
المحاضرة الثامنة: تراكيب البيانات الطابورMahmoud Alfarra
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 InheritanceOUM SAOKOSAL
 
DBI-Assisted Android Application Reverse Engineering
DBI-Assisted Android Application Reverse EngineeringDBI-Assisted Android Application Reverse Engineering
DBI-Assisted Android Application Reverse EngineeringSahil Dhar
 
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock TutorialSbin m
 

Similaire à JavaProgReflectAdvFeat (20)

Java reflection
Java reflectionJava reflection
Java reflection
 
Jersey Guice AOP
Jersey Guice AOPJersey Guice AOP
Jersey Guice AOP
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 
Nhibernate Part 2
Nhibernate   Part 2Nhibernate   Part 2
Nhibernate Part 2
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docx
 
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
 
Introduction+To+Java+Concurrency
Introduction+To+Java+ConcurrencyIntroduction+To+Java+Concurrency
Introduction+To+Java+Concurrency
 
Inheritance
InheritanceInheritance
Inheritance
 
المحاضرة الثامنة: تراكيب البيانات الطابور
المحاضرة الثامنة: تراكيب البيانات الطابورالمحاضرة الثامنة: تراكيب البيانات الطابور
المحاضرة الثامنة: تراكيب البيانات الطابور
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
 
DBI-Assisted Android Application Reverse Engineering
DBI-Assisted Android Application Reverse EngineeringDBI-Assisted Android Application Reverse Engineering
DBI-Assisted Android Application Reverse Engineering
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
 
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
 
Maf3 - Part 1
Maf3 - Part 1Maf3 - Part 1
Maf3 - Part 1
 
Design patterns(red)
Design patterns(red)Design patterns(red)
Design patterns(red)
 
04 threads
04 threads04 threads
04 threads
 
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock Tutorial
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 

Plus de kim.mens

Software Maintenance and Evolution
Software Maintenance and EvolutionSoftware Maintenance and Evolution
Software Maintenance and Evolutionkim.mens
 
Context-Oriented Programming
Context-Oriented ProgrammingContext-Oriented Programming
Context-Oriented Programmingkim.mens
 
Software Reuse and Object-Oriented Programming
Software Reuse and Object-Oriented ProgrammingSoftware Reuse and Object-Oriented Programming
Software Reuse and Object-Oriented Programmingkim.mens
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smellskim.mens
 
Object-Oriented Design Heuristics
Object-Oriented Design HeuristicsObject-Oriented Design Heuristics
Object-Oriented Design Heuristicskim.mens
 
Software Patterns
Software PatternsSoftware Patterns
Software Patternskim.mens
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoringkim.mens
 
Domain Modelling
Domain ModellingDomain Modelling
Domain Modellingkim.mens
 
Object-Oriented Application Frameworks
Object-Oriented Application FrameworksObject-Oriented Application Frameworks
Object-Oriented Application Frameworkskim.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 Frameworkkim.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 Approacheskim.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 Engineeringkim.mens
 
Context-oriented programming
Context-oriented programmingContext-oriented programming
Context-oriented programmingkim.mens
 
Basics of reflection
Basics of reflectionBasics of reflection
Basics of reflectionkim.mens
 
Reflection in Ruby
Reflection in RubyReflection in Ruby
Reflection in Rubykim.mens
 
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Rubykim.mens
 
Introduction to Smalltalk
Introduction to SmalltalkIntroduction to Smalltalk
Introduction to Smalltalkkim.mens
 
A gentle introduction to reflection
A gentle introduction to reflectionA gentle introduction to reflection
A gentle introduction to reflectionkim.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
 

Plus de kim.mens (20)

Software Maintenance and Evolution
Software Maintenance and EvolutionSoftware Maintenance and Evolution
Software Maintenance and Evolution
 
Context-Oriented Programming
Context-Oriented ProgrammingContext-Oriented Programming
Context-Oriented Programming
 
Software Reuse and Object-Oriented Programming
Software Reuse and Object-Oriented ProgrammingSoftware Reuse and Object-Oriented Programming
Software Reuse and Object-Oriented Programming
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smells
 
Object-Oriented Design Heuristics
Object-Oriented Design HeuristicsObject-Oriented Design Heuristics
Object-Oriented Design Heuristics
 
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
Domain Modelling
Domain ModellingDomain Modelling
Domain Modelling
 
Object-Oriented Application Frameworks
Object-Oriented Application FrameworksObject-Oriented Application Frameworks
Object-Oriented Application Frameworks
 
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
 
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
 
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
 
Context-oriented programming
Context-oriented programmingContext-oriented programming
Context-oriented programming
 
Basics of reflection
Basics of reflectionBasics of reflection
Basics of reflection
 
Reflection in Ruby
Reflection in RubyReflection in Ruby
Reflection in Ruby
 
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
 
Introduction to Smalltalk
Introduction to SmalltalkIntroduction to Smalltalk
Introduction to Smalltalk
 
A gentle introduction to reflection
A gentle introduction to reflectionA gentle introduction to reflection
A gentle introduction to reflection
 
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...
 
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)
 

Dernier

PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...Sérgio Sacani
 
GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)Areesha Ahmad
 
Forensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfForensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfrohankumarsinghrore1
 
Pests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPirithiRaju
 
Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )aarthirajkumar25
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxUmerFayaz5
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfSumit Kumar yadav
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PPRINCE C P
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bSérgio Sacani
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfmuntazimhurra
 
DIFFERENCE IN BACK CROSS AND TEST CROSS
DIFFERENCE IN  BACK CROSS AND TEST CROSSDIFFERENCE IN  BACK CROSS AND TEST CROSS
DIFFERENCE IN BACK CROSS AND TEST CROSSLeenakshiTyagi
 
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdfPests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdfPirithiRaju
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSarthak Sekhar Mondal
 
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticsPulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticssakshisoni2385
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisDiwakar Mishra
 
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡anilsa9823
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...anilsa9823
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000Sapana Sha
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsAArockiyaNisha
 

Dernier (20)

PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
 
GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)
 
Forensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfForensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdf
 
Pests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdf
 
Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptx
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdf
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C P
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdf
 
DIFFERENCE IN BACK CROSS AND TEST CROSS
DIFFERENCE IN  BACK CROSS AND TEST CROSSDIFFERENCE IN  BACK CROSS AND TEST CROSS
DIFFERENCE IN BACK CROSS AND TEST CROSS
 
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdfPests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
 
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticsPulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
 
CELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdfCELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdf
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
 
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based Nanomaterials
 

JavaProgReflectAdvFeat

  • 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