Weaving technologies Comparison of different weaving approaches - - PowerPoint PPT Presentation

weaving technologies
SMART_READER_LITE
LIVE PREVIEW

Weaving technologies Comparison of different weaving approaches - - PowerPoint PPT Presentation

Weaving technologies Comparison of different weaving approaches http://techblog.karsten-becker.de 1 Bio Professional Studies Siemens CT SE2 TUHH Eclipse RCP General engineering IBM Rational science HAW Hamburg


slide-1
SLIDE 1

http://techblog.karsten-becker.de 1

Weaving technologies

Comparison of different weaving approaches

slide-2
SLIDE 2

http://techblog.karsten-becker.de 2

Bio

Professional

  • Siemens CT SE2

– Eclipse RCP

  • IBM Rational

Reasearch

– Eclipse JDT

Refactoring

Studies

  • TUHH

– General engineering

science

  • HAW Hamburg

– Computer

Engineering

slide-3
SLIDE 3

http://techblog.karsten-becker.de 3

Overview

  • Used Terminology
  • Offline weaving (static weaving)
  • Load-time weaving
  • Online weaving (dynamic weaving)
slide-4
SLIDE 4

http://techblog.karsten-becker.de 4

Terminology

  • Advice

– Code that will be inserted at a joinpoint – Often defined as „regular“ Java code

  • Joinpoint

– Point in control flow of a application

  • before/after/around method/field access/constructor
  • Pointcut

– Collection of joinpoints – Often matched via regular expressions

slide-5
SLIDE 5

http://techblog.karsten-becker.de 5

Terminology

  • Aspect

– Collection of advices that are assigned to

joinpoints using pointcuts

  • Weaving

– Process of applying an Aspect to an existing

application

– Usually involves modification of bytecode

slide-6
SLIDE 6

http://techblog.karsten-becker.de 6

Offline weaving

Offline weaving

  • Bytecode information
  • Bytecode manipulation tools
  • General weaving process
slide-7
SLIDE 7

http://techblog.karsten-becker.de 7

Bytecode information

Example Application:

public class Hello { public static void main(String[] args) { System.out.println("Hello Welt"); } }

slide-8
SLIDE 8

http://techblog.karsten-becker.de 8

Bytecode Information

public static main(String[]) : void L0 (0) LINENUMBER 5 L0 GETSTATIC System.out : PrintStream LDC "Hello Welt" INVOKEVIRTUAL PrintStream.println(String) : void L1 (4) LINENUMBER 6 L1 RETURN L2 (6) LOCALVARIABLE args String[] L0 L2 0 MAXSTACK = 2 MAXLOCALS = 1

slide-9
SLIDE 9

http://techblog.karsten-becker.de 9

Java Virtual Machine

  • Stack driven

– Invocation

  • Pushes parameters
  • (Pops result)

– Execution

  • Pops parameters
  • Pushs result
  • Execution Modes

– Interpreted – Compiled

  • Baseline compiled
  • Optimised compiled

Demo with bytecode outline

slide-10
SLIDE 10

http://techblog.karsten-becker.de 10

Bytecode Modification

Structures that need updates

  • Constant pool

– String references to classes – Literals

  • References to Labels
  • MaxStack
  • MaxLocals
  • Exceptions table
  • Attributes
slide-11
SLIDE 11

http://techblog.karsten-becker.de 11

BCEL Example

  • Insert „before“ advice
  • Insert „after“ advice

public class TestClass { public static void main(String[] args) { if (args.length == 0) return; System.out.print("World"); } }

slide-12
SLIDE 12

http://techblog.karsten-becker.de 12

BCEL Example

JavaClass testClass = Repository.lookupClass("TestClass"); Method[] methods = testClass.getMethods(); ClassGen gen = new ClassGen(testClass); InstructionFactory factory = new InstructionFactory(gen);

  • Loading Class into special representation
  • Can not modify already loaded classes!
slide-13
SLIDE 13

http://techblog.karsten-becker.de 13

BCEL Code print

Calling getCode on Method object

Code(max_stack = 2 , max_locals = 1 , code_length = 15) 0: aload_0 1: array length 2: ifne #6 5: return 6: getstatic java.lang.System.out Ljava/io/PrintStream ; (22) 9: ldc "World" (24) 11: invokevirtual java.io.PrintStream.print(Ljava/lang/String;)V (30) 14: return

slide-14
SLIDE 14

http://techblog.karsten-becker.de 14

BCEL Insert before

MethodGen mg = new MethodGen(method,TestClass.getClassName(), gen .getConstantPool()); InstructionList original = mg.getInstructionList(); InvokeInstruction before = factory.createInvoke(Aspect.class.getName(), "before", Type.VOID, new Type[0], Constants.INVOKESTATIC);

  • riginal.insert(before);

mg.setMaxLocals(); mg.setMaxStack(); Method newMethod = mg.getMethod();

slide-15
SLIDE 15

http://techblog.karsten-becker.de 15

BCEL Code print

Calling getCode again

Code(max_stack = 2, max_locals = 1, code_length = 18) 0: invokestatic offline.general.bcel.Aspect.before ()V (39) 3: aload_0 4: arraylength 5: ifne #9 8: return 9: getstatic java.lang.System.out Ljava/io/PrintStream; (22) 12: ldc "World" (24) 14: invokevirtual java.io.PrintStream.print (Ljava/lang/String;)V (30) 17: return

slide-16
SLIDE 16

http://techblog.karsten-becker.de 16

BCEL Insert after

InstructionList original = mg.getInstructionList(); InstructionHandle[] handles =

  • riginal.getInstructionHandles();

for (InstructionHandle handle : handles) { if (handle.getInstruction() .equals(InstructionConstants.RETURN)) { InvokeInstruction after = factory.createInvoke( Aspect.class.getName(), "after", Type.VOID, new Type[0], Constants.INVOKESTATIC);

  • riginal.insert(handle, after);

} }

slide-17
SLIDE 17

http://techblog.karsten-becker.de 17

Load-time weaving

Load-Time weaving

  • How it works
  • Possibilites
  • Restrictions
slide-18
SLIDE 18

http://techblog.karsten-becker.de 18

Load time weaving?!

  • Bytecode manipulation same as offline

– Often BCEL, ASM..

  • Configuration often through XML
slide-19
SLIDE 19

http://techblog.karsten-becker.de 19

LTW in AspectJ

public class TestClass { public static void main(String[] args) throws Exception { ClassLoader parent = ClassLoader.getSystemClassLoader(); ClassLoader cl = new WeavingURLClassLoader(parent); Class<?> loadClass = cl.loadClass("net.kbsvn.ltw.HelloClass"); ISayHello hc=(ISayHello) loadClass.newInstance(); hc.sayHello(); } } public interface ISayHello { public void sayHello(); }

slide-20
SLIDE 20

http://techblog.karsten-becker.de 20

Possibilites for ClassLoader

  • Redefine Systemclassloader through define

– -Djava.system.class.loader

  • Replace ClassLoader through hotswap

– JVM needs to run in debug modus

  • Replace ClassLoader in rt.jar

– -Xbootclasspath

slide-21
SLIDE 21

http://techblog.karsten-becker.de 21

Restrictions and side-effects

  • Only classes loaded through ClassLoader

– Due to parent first paradigma to be woven code

may not lie within ClassPath

  • Classes already loaded can not be woven
  • Only aspects that are loaded can be applied

– No loading of aspects during runtime

  • No classes from rt.jar

– Loaded through boostrap CL

slide-22
SLIDE 22

http://techblog.karsten-becker.de 22

Online Weaving

  • Through Proxies

– Spring – JBoss

  • Through JVMDI&JVMTI

– Prose – Spring (agent) – JBoss (agent)

  • Through deep JVM support

– Steamloom – JRockit?

slide-23
SLIDE 23

http://techblog.karsten-becker.de 23

Proxies

  • In Java since 1.3
  • During runtime created
  • Use interfaces for method signatures
  • Dispatch invocations to single method

– InvocationHandler#invoke()

slide-24
SLIDE 24

http://techblog.karsten-becker.de 24

Proxies advantages

  • Advice lookup during runtime

– Very easy to make dynamic

  • Supported in every JVM
  • Support before, after, around with

modification of result

slide-25
SLIDE 25

http://techblog.karsten-becker.de 25

Proxies disadvantage

  • Classes are not the same

– Problems with annotations – getClass() name differs

  • No support for field access
  • Small performance penalty
  • No Call support
slide-26
SLIDE 26

http://techblog.karsten-becker.de 26

JVMDI&JVMTI

  • Support through

– Breakpoint (JVMDI) – General invocation notification (JVMTI) – Bytecode redefinition (JVMTI & JVMDI)

  • Requires some support in JVM

– Most JVMs support both

  • Requires native Code

– Portability issues

  • Requires access to JVM commandline

– Not always easy in J2EE

  • Have access to all classes
slide-27
SLIDE 27

http://techblog.karsten-becker.de 27

JVMDI&JVMTI

  • Lowers overall performance

– JVMDI needs Debug mode of JVM

  • No penalty for client VM
  • Up to 20% penalty for server VM

– Redefinition of Class break optimisation level

  • Only partial support for Intertype definition
  • Not more than 1 supported

– In Java6 this restriction has been changed

slide-28
SLIDE 28

http://techblog.karsten-becker.de 28

Deep JVM support

  • Steamloom

– Works for jikes RVM on linux – Performance close to aspectJ static

  • Faster! for cflow
  • Faster Advice instance lookup

– No double book-keeping

  • Required information already included in Class objects
  • JRockit

– Not much known – Subscriber kind of weaving

slide-29
SLIDE 29

http://techblog.karsten-becker.de 29

Ready to go, or questions?

Any questions, or ready for deep JVM support?

slide-30
SLIDE 30

http://techblog.karsten-becker.de 30

Jikes RVM

  • Research Virtual Machine

– Written in Java

  • Selfhosted

– No interpreted mode – 2 JIT Compiler

  • Basline JIT
  • Adaptive Optimisation System
slide-31
SLIDE 31

http://techblog.karsten-becker.de 31

Jikes RVM

  • Method are replaced by lazy compilation

stubs

  • Lazy compilation stub triggers compilation of

real bytecode

– Done using Baseline compiler

  • Stupid but blazing fast
  • Optimised Compiler performs basic
  • ptimisation
  • Adaptive Compiler compiles profile based
  • ptimised code
slide-32
SLIDE 32

http://techblog.karsten-becker.de 32

Steamloom

  • Replaces Compilers

– Additional information about method

  • Inline places
  • BAT compatible Class structure
  • Advice deployment

– Weaves method bytecode using BAT – Flag method for recompilation

  • Advice undeployment

– Unweave method bytecode using BAT – Flag method for recompilation

slide-33
SLIDE 33

http://techblog.karsten-becker.de 33

Steamloom API

  • Interfaced using regular Java