No dark magic - Byte code engineering in the real world

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

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

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

    + tcurdttcurdt, 3 years ago

    custom

    1451 views, 0 favs, 0 embeds more stats

    ften byte code engineering is perceived as "black m more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1451
      • 1451 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 56
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories