Top Banner
Retroactive API Extensions Through Bytecode Weaving Jevgeni Kabanov PhD student, University of Tartu
20

Retroactive API Extensions Through Bytecode Weaving

Feb 22, 2016

Download

Documents

holly

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 Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Retroactive API Extensions Through  Bytecode  Weaving

Retroactive API Extensions

Through Bytecode Weaving Jevgeni Kabanov

PhD student, University of Tartu

Page 2: Retroactive API Extensions Through  Bytecode  Weaving

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

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

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
Page 6: Retroactive API Extensions Through  Bytecode  Weaving

BEHIND THE SCENES

MyObject.class

MyObject.classMyClass_

3MyClass’MyClass

Page 7: Retroactive API Extensions Through  Bytecode  Weaving

IDEs Servers Frameworks

Open-Source API

Page 8: Retroactive API Extensions Through  Bytecode  Weaving

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

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

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

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

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

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

REWRITING IS EASYASM

Visitor APILow level

JavassistString-embedded Java DSL High-level, but adjustable

Page 15: Retroactive API Extensions Through  Bytecode  Weaving

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

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

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

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

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

Questions?