retroactive api extensions through bytecode weaving
DESCRIPTION
Retroactive API Extensions Through Bytecode Weaving . Jevgeni Kabanov PhD student, University of Tartu. WHAT?. API Extensions Application programming interface New APIs, changed APIs Bytecode Weaving Bytecode : stack-based Java-like language Weaving: runtime program rewriting - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/1.jpg)
Retroactive API Extensions
Through Bytecode Weaving Jevgeni Kabanov
PhD student, University of Tartu
![Page 2: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/2.jpg)
WHAT?API Extensions
Application programming interfaceNew APIs, changed APIs
Bytecode WeavingBytecode: stack-based Java-like language Weaving: runtime program rewriting
RetroactiveAbstraction after the factWithout access to source code
![Page 3: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/3.jpg)
WHY?Java EE is implemented by application containers each with specific extensions
Frameworks and applications need a unified access to container-specific features
![Page 4: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/4.jpg)
EXAMPLEJavaScript, DOM, abstraction libraries
GWTPrototypejQueryExt JS
Spring (Java framework)Abstraction over some common Java EE features for applications
![Page 5: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/5.jpg)
![Page 6: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/6.jpg)
BEHIND THE SCENES
MyObject.class
MyObject.classMyClass_
3MyClass’MyClass
![Page 7: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/7.jpg)
IDEs Servers Frameworks
Open-Source API
![Page 8: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/8.jpg)
Class loader APIpublic abstract class ClassLoader { public Class loadClass(String name); protected Class defineClass(byte[] b);
public URL getResource(String name); public Enumeration getResources(String
name);
public ClassLoader getParent()}
![Page 9: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/9.jpg)
API ExtensionsChange loadClass() to include the classes we provide to it
Add a method getChildResources() that returns the resources hierarchically
![Page 10: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/10.jpg)
Java Class Definition
Modifiers, name, super class, interfacesEnclosing class referenceAnnotation*Inner class* NameField* Modifiers, name, type
Annotation*Method* Modifiers, name, return and parameter types
Annotation*Compiled code
![Page 11: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/11.jpg)
Java Execution Model
Method B
Method A
MainL0 L1 L2 L3O1 O2
L0 L1 L2O1 O2 O3
L0 L1O1
Local variables
Operand stack
Execution stackFrame
Calli
ng a
met
hod
Thro
win
g an
ex
cept
ion
![Page 12: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/12.jpg)
Instruction Example
INVOKEINTERFACE java/util/List get (I)LObject;
opcode arguments
... java.util.List int ... java.util.List.get(int)apply
Operands stack when applying the instruction:
Instruction:
![Page 13: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/13.jpg)
Hello, World! in Bytecodepublic class HelloWorld { public <init>()V ALOAD 0 INVOKESPECIAL Object.<init>()V RETURN public static main([LString;)V GETSTATIC System.out : LPrintStream; LDC "Hello, World!" INVOKEVIRTUAL PrintStream.println(LString;)V RETURN}
![Page 14: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/14.jpg)
REWRITING IS EASYASM
Visitor APILow level
JavassistString-embedded Java DSL High-level, but adjustable
![Page 15: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/15.jpg)
PROBLEM 1: TRANSPARENCYIf the result of an API call is altered, how do we access the original from our framework?
E.g. ClassLoader.loadClass()
![Page 16: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/16.jpg)
PROBLEM 2: OPTIONALWhat if the features we want to support are optional or implemented differently among implementers?
E.g. ClassLoader.getChildResources()
![Page 17: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/17.jpg)
PROBLEM 3: VERSIONINGWhat if we have several versions of an implementation?
NB! In Java the class files are not versioned and detecting versions may be challenging
![Page 18: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/18.jpg)
PROBLEM 4: GLOBAL STATEWhat if implementer state dictates that calls are legal in some states and illegal in others?
E.g. ClassLoader classpath is often constructed incrementally
![Page 19: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/19.jpg)
PROBLEM 5: SYNCHRONIZATION
What if implementer does synchronization?
Synchronized in Java is a reentrant mutexClassLoader.loadClass() is synchronized, ClassLoader.getResource() usually not
![Page 20: Retroactive API Extensions Through Bytecode Weaving](https://reader036.vdocuments.mx/reader036/viewer/2022062222/5681602f550346895dcf48f6/html5/thumbnails/20.jpg)
Questions?