Jav Assist X java compile X class load

  • Slides: 28
Download presentation
Jav. Assist X. java compile X. class load Class Loader My Loader X Jav.

Jav. Assist X. java compile X. class load Class Loader My Loader X Jav. Assist enables preload byte code modification

? What is Reflection If a program can inspect itself while running, it’s said

? What is Reflection If a program can inspect itself while running, it’s said that the runtime system is reflective with respect to that program.

Levels of Reflection • Introspection. – No alteration, read only. • Behavioral Reflection. –

Levels of Reflection • Introspection. – No alteration, read only. • Behavioral Reflection. – Limited Alteration possible, such as method invocation. – In general : behavior of operations is changeable. • Structural Reflection. – The ability to change data structures used by the runtime system, such as Class for example.

Java, Aspect. J, and Jav. Assist • Java provides Introspection: – get. Name. •

Java, Aspect. J, and Jav. Assist • Java provides Introspection: – get. Name. • Aspect. J provides Behavioral Reflection: – Aspects + Point cuts are used. – Compile time Weaving process integrates behavioral changes to precompiled base code. • Javassist provides Structural Reflection: – Javassist API is in Java. – Customized runtime loader changes preloaded byte code.

? How is Javassist implemented Ct. Class c = new Ct. Class(“X. class”); //There’s

? How is Javassist implemented Ct. Class c = new Ct. Class(“X. class”); //There’s nothing like Class X in the JVM so far! c. not. Final(); c. add. Field(…) c. add. Method(…) //c contains the altered byte code of X. c. load(); //Altered X is finally loaded into the JVM. c ’X My Loader X. class

Design Goals of Javassist 1. Source Level Abstraction. 2. No knowledge of byte code

Design Goals of Javassist 1. Source Level Abstraction. 2. No knowledge of byte code is needed. 3. API is purely in Java. 2. Efficient Structural Reflection. 3. Reduce runtime overheads due to class reconstruction. 3. Safe Structural Reflection. 4. Protect programmers from defected byte code composition.

Pursuing Javassist Design Goals. 1. Source Level Abstraction. 2. Class rename. 2. Efficient Structural

Pursuing Javassist Design Goals. 1. Source Level Abstraction. 2. Class rename. 2. Efficient Structural Reflection. 3. Runtime compilation with add. Method. 3. Safe Structural Reflection. 4. Class member removal.

The Javassist API Ct. Class{ } Class{ Ct. Fields Ct. Methods Constructors Ct. Methods

The Javassist API Ct. Class{ } Class{ Ct. Fields Ct. Methods Constructors Ct. Methods } • Introspection • Alteration. • Adding a new Member. • Altering a Method Body. • Javassist Class Loader.

Ct. Class Introspection

Ct. Class Introspection

Ct. Field & Ct. Method Introspection

Ct. Field & Ct. Method Introspection

Alteration

Alteration

Example: add. Method public class Xvector extends java. util. vector{ public void add(X e){

Example: add. Method public class Xvector extends java. util. vector{ public void add(X e){ super. add. Element(e); } } Ct. Method m = // method add() in XVector Ct. Class c = // class String. Vector Classmap = new Class. Map(); map. put(“X” , ”java. lang. String”); c. add. Method(m, “add. String”, map); Public void add. String(java. lang. String e){ super. add. Element(e); }

Example: set. Body M( … ){ … Xclass xclass = new Xclass(3, 4); …

Example: set. Body M( … ){ … Xclass xclass = new Xclass(3, 4); … } Ct. Method m = // method which body is to be changed Code. Converter conv = new Code. Converter(); Conv. replace. New(Xclass, Yclass, “create” ); m. instrument(conv); M( … ){ … Yclass. create(3, 4); … } Must be Static

Reflective Class Loader • Jav. Assist default loader. • User defined loader. • Web

Reflective Class Loader • Jav. Assist default loader. • User defined loader. • Web Server. • Off line.

Examples: Binary Code. Adaptation class X implements Writable{ public void write(Printable. Stream s){. .

Examples: Binary Code. Adaptation class X implements Writable{ public void write(Printable. Stream s){. . } } class X implements Printable{ public void (Printable. Stream s){. . } public void print(){write(System. out); } }

Examples: Binary Code Adaptation. class Exemplar implements Printable { public void write(Print. Stream s)

Examples: Binary Code Adaptation. class Exemplar implements Printable { public void write(Print. Stream s) { /* dummy */ } public void print() { write(System. out); } } class Adaptor { // THE JOB IS DONE HERE public void adapt(Ct. Class c) { Ct. Method print. M = /* method print() in Exemplar */; Ct. Class[] interfaces = c. get. Interfaces(); for (int i = 0; i < interfaces. length; ++i) if (interfaces[i]. get. Name(). equals("Writable")) { interfaces[i] = Ct. Class. for. Name("Printable"); c. set. Interfaces(interfaces); c. add. Method(print. M, new Class. Map()); return; } } }

. Behavioral Reflection public class My. Meta. Object extends Meta. Object { public Object

. Behavioral Reflection public class My. Meta. Object extends Meta. Object { public Object trap. Method. Call(String method. Name, Object[] args) { /* called if a method call is intercepted. */ } public Object trap. Field. Read(String field. Name) { /* called if the value of a field is read. */ } public void trap. Field. Write(String field. Name, Object value) { /* called if a field is set. */ } }

Behavioral Reflection. public class C{ public int f; public int m(int x){ return x+f;

Behavioral Reflection. public class C{ public int f; public int m(int x){ return x+f; } } public class C implements Meta. Level { public int m(int x) { /* notify a Meta. Object. */ } public int f; private Meta. Object _Meta. Object = new My. Meta. Object(this); public Meta. Object _get. Meta. Object() { return _Meta. Object; } public int orig_m(int x) { return x + f; } public static int read_f(Object target) {/*notify a Meta. Object. */ } public static void write_f(Object target, int value) { /*notify a Meta. Object. */ } }

Behavioral Reflection. class Exemplar { private Meta. Object _Meta. Object; public Object trap(Object[] args,

Behavioral Reflection. class Exemplar { private Meta. Object _Meta. Object; public Object trap(Object[] args, String method. Name) { return _Meta. Object. trap. Methodcall(method. Name, args); } public static Object trap. Read(Object[] args, String name) { Metalevel target = (Metalevel)args[0]; return target. _get. Meta. Object(). trap. Field. Read(name); } public static Object trap. Write(Object[] args, String name) { Metalevel target = (Metalevel)args[0]; Object value = args[1]; target. _get. Meta. Object(). trap. Field. Write(name, value); } }

Related Work: . Reflection in Java • Meta. Xa and Kava enable behavioral reflection

Related Work: . Reflection in Java • Meta. Xa and Kava enable behavioral reflection in Java, whereas Jav. Assist enable structural reflection. • Jav. Assist can be used to achieve behavioral reflection, such as with Meta. Xa and Kava. • Kirby’s system, allows a class definition at load time. It doesn’t support class redefining.

Related Work: Compile-Time. Meta. Object Protocol • Another architecture enabling structural reflection without modifying

Related Work: Compile-Time. Meta. Object Protocol • Another architecture enabling structural reflection without modifying an existing runtime system is Open. Java. • Open. Java was designed mainly for off-line use at compile time.

Related Work: Compile-Time. Meta. Object Protocol • Open. Java is source-code based. • Open.

Related Work: Compile-Time. Meta. Object Protocol • Open. Java is source-code based. • Open. Java alterations are on source code level. • Open. Java involves a performance overhead due to source code manipulations. On the other hand it results with a finegrained source code.

Related Work: Compile-Time. Meta. Object Protocol • Jav. Assist is bytecode based and doesn’t

Related Work: Compile-Time. Meta. Object Protocol • Jav. Assist is bytecode based and doesn’t need source code( which is not always available). • No performance overhead: the code is already compiled, therefore Jav. Assist achieves shorter loading time. • Jav. Assist runs 10 times faster than Open. Java.

Related Work: Bytecode. Translators • JOIE and Java. Class API provide functionality similar to

Related Work: Bytecode. Translators • JOIE and Java. Class API provide functionality similar to Jav. Assist. • Using them, the programmer is required to have a deep understanding of the Java byte code. • Jav. Assist provides source-level abstraction for manipulating byte code in a safe manner.

. Related Work: Others • Open. JIT: allows a Java program to control how

. Related Work: Others • Open. JIT: allows a Java program to control how byte code is compiled into native code. It provides better flexibility with method body redefinition, but doesn’t allow to add a new method or field to the class.

. Conclusions • Jav. Assist is an extension to the Java reflection API. •

. Conclusions • Jav. Assist is an extension to the Java reflection API. • It enables structural reflection in Java, allows to alter a given class definition and to dynamically define a new class. • Language extensions are more easily implemented with structural reflection than with behavioral reflection.

(. Conclusions (Cont • Jav. Assist is portable. • Jav. Assist provides source-level abstraction

(. Conclusions (Cont • Jav. Assist is portable. • Jav. Assist provides source-level abstraction for manipulating byte code in a safe manner, while byte code translators, such as JOIE and the Java. Class API, provide no higherlevel abstraction. • Jav. Assist processes byte code. • Open. Java processes source code.

(. Conclusions (Cont • The architecture of Jav. Assist can be applied to other

(. Conclusions (Cont • The architecture of Jav. Assist can be applied to other object-oriented languages if a compiled binary program includes enough symbolic information to construct a class object. • API must be individually designed for each language to allow class alteration definition in a safe and meaningful manner.