• Like
  • Save
No dark magic - Byte code engineering in the real world
Upcoming SlideShare
Loading in...5
×
 

No dark magic - Byte code engineering in the real world

on

  • 4,864 views

ften byte code engineering is perceived as "black magic" and considered too low level. This session will not bore you with all the details of the JVM specification but provide you with a practical ...

ften byte code engineering is perceived as "black magic" and considered too low level. This session will not bore you with all the details of the JVM specification but provide you with a practical overview of how this byte code "swizzling" can be used in the real world instead. Several projects have sucessfully leveraged this technique to achieve some amazing things (e.g. AOP). This session will go through some of these examples and try to outline the differences between the two major libraries (BCEL and ASM). Maybe your next project can then benefit from some of this "magic".

Statistics

Views

Total Views
4,864
Views on SlideShare
4,851
Embed Views
13

Actions

Likes
2
Downloads
103
Comments
0

3 Embeds 13

http://www.slideshare.net 9
http://www.linkedin.com 3
http://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    No dark magic - Byte code engineering in the real world No dark magic - Byte code engineering in the real world Presentation Transcript

    • Torsten Curdt Java Byte Code Engineering No black magic
    • Virtual CPU Physical Machine Java Virtual Machine
    • .cstring .align 2 Native Assembler LC0: .ascii quot;Hello, world .text .align 2 .globl _main #include <stdio.h> _main: mflr r0 stmw r30,-8(r1) int main(char** args) { r0,8(r1) stw stwu r1,-80(r1) printf(quot;Hello, worldquot;); mr r30,r1 bcl 20,31,quot;L000000000 return 0; quot;L00000000001$pbquot;: } mflr r31 stw r3,104(r30) gcc -S addis r2,r31,ha16(LC0 HelloWorld.c la r3,lo16(LC0-quot;L0000 bl L_printf$LDBLStub$
    • Native Assembler #include <stdio.h> int main(char** args) mflr r0 printf(quot;Hello, worl return 0; stmw r30,-8(r1) } stw r0,8(r1) stwu r1,-80(r1) mr r30,r1 bcl 20,31,quot;L00000000001$pbquot; ... gcc -S HelloWorld.c
    • Compiled from quot;HelloW Java Assembler public class HelloWor public HelloWorld(); Code: 0: aload_0 1: invokespecial public class HelloWorld { Object.quot;<init>quot;:()V 4: return public static void main(String[] args) { public static void ma System.out.println(quot;Hello, world!quot;); Code: } 0: getstatic System.out:Ljava/io/P 3: ldc #3; / } 5: invokevirtual //Method java javap -c HelloWorld lang/String;)V 8: return
    • Java Assembler public class HelloWo public static void main(java.lang.String[]) public static void System.out.print Code: } 0: getstatic #2; //Field java/lang/ 3: ldc #3; //String Hello, world! } 5: invokevirtual #4; //Method java/io/ 8: return } javap -c HelloWorld
    • Jasmin Syntax public class HelloWo public static void .method public static main([Ljava/lang System.out.print .limit stack 2 } .limit locals 1 } .line 4 getstatic java/lang/System/out Ljava/ ldc quot;Hello, world!quot; invokevirtual java/io/PrintStream/println .line 5 return .end method
    • Tools • Disassembler: Jasper java -jar jasper.jar HelloWorld.class • Assembler: Jasmin java -jar jasmin.jar HelloWorld.j
    • Roundtrip jasmin HelloWorld.j HelloWorld.class jasper
    • Libraries • ASM (BSD-style) • BCEL (ASL) • CGLIB (ASL) • Javassist (MPL/LGPL)
    • Libraries AOP Javassist ASM CGLIB BCEL
    • BCEL • Object model based • JDK 1.4 support • Verifier • “DOM”
    • ASM • Event driven • DOM available • JDK 1.6 support • “SAX”
    • BCEL vs ASM • Community • JDK support • “DOM” vs “SAX” • API • Tool support
    • BCEL vs ASM • BCEL: xalan, findbugs, aspectj, javaflow, clirr, beanshell, just4log, ... • ASM: jardiff, javaflow, groovy, cobertura, aspectwerkz, beanshell, retroweaver, ...
    • demo
    • Generation
    • XSLTC • XSLT to java compiler • Creates a translet from xslt • Translet as JAXP transformer
    • XSLTC *.xsl SAX Parser XPath Parser XSLT Parser JAXP / TrAX API native API *.xml XSLT Compiler BCEL class
    • XSLTC Speed gregor msxml jd.xslt xsltc saxon xalan-j
    • Groovy groovy Groovy Parser source ASM ClassVisitor class
    • Analysis
    • Findbugs • Static analysis for bug patterns • False positives
    • Findbugs
    • Findbugs
    • Clirr / Jardiff • Diff for jars or class files • Detecting API changes
    • Jardiff
    • Jardiff
    • Dependency • Analyses class dependencies • Finding unused classes • Renaming classes http://vafer.org/projects/dependency
    • Dependency Set dependencies = DependencyUtils .getDependenciesOfClass( Class1.class );
    • Dependency new ResourceRenamer() { String getNewNameFor(String oldName) { if (oldName.startsWith( quot;java.util.HashMapquot;)) { return quot;my.quot; + oldName; } return oldName; }
    • Modification
    • Cobertura / Clover • Testcase coverage • Recording the execution path • Maven plugin
    • Cobertura
    • Cobertura
    • Throttling • Wrap Socket to deliver throughput according to settings • Cannot rewrite java.* classes
    • Retroweaver • Compile with jdk 1.5 features, run on 1.4 • Static conversion • Runtime conversion
    • Just4log for(int i=0; i<500000; i++) { for(int i=0; i<500000; i++) { if(log.isDebugEnabled()) { if(log.isDebugEnabled()) { log.debug(quot;messagequot; log.debug(quot;messagequot; + someLongTaskToExecute()); } + someLongTaskToExecute()); }normalCodeToExecute(); } normalCodeToExecute(); }
    • AspectJ public aspect LogAspect { declare precedence : LogAspect, *; pointcut voidCalls() : !within(LogAspect) && execution(void *.*(..)) );
    • AspectJ Object around() : voidCalls() { Signature sig = thisJoinPoint.getSignature(); log.debug('<' + getFullSig(thisJoinPoint)); Object result = proceed(); log.debug('>' + sig.getName()); return result;
    • Javaflow class MyRunnable implements Runnable { public void run() { for(int i=0; i<10; i++ ) Continuation.suspend(); }} Continuation c = Continuation.startWith( new MyRunnable()); Continuation d = Continuation.continueWith(c); ...
    • Javaflow
    • What you can do • reflection without j.l.reflection • generate classes on the fly • implement interface on the fly
    • What you can do • change visibility • extend classes that are final • modify vs forking • ...
    • Thanks!