Java Bytecode for Discriminating Developers - JavaZone 2011
Upcoming SlideShare
Loading in...5
×
 

Java Bytecode for Discriminating Developers - JavaZone 2011

on

  • 7,335 views

 

Statistics

Views

Total Views
7,335
Views on SlideShare
2,813
Embed Views
4,522

Actions

Likes
6
Downloads
96
Comments
0

51 Embeds 4,522

http://arhipov.blogspot.com 3584
http://arhipov.blogspot.in 193
http://arhipov.blogspot.de 136
http://feeds.feedburner.com 90
http://arhipov.blogspot.co.uk 90
http://arhipov.blogspot.fr 43
http://arhipov.blogspot.ru 42
http://arhipov.blogspot.ca 32
http://arhipov.blogspot.com.es 28
http://lanyrd.com 25
http://arhipov.blogspot.com.au 24
http://arhipov.blogspot.it 23
http://arhipov.blogspot.se 19
http://arhipov.blogspot.com.br 18
http://arhipov.blogspot.jp 14
http://arhipov.blogspot.cz 13
http://paper.li 11
http://arhipov.blogspot.co.at 10
http://arhipov.blogspot.co.il 10
http://arhipov.blogspot.ch 9
http://twitter.com 8
http://arhipov.blogspot.dk 8
http://arhipov.blogspot.pt 7
http://arhipov.blogspot.co.nz 7
http://arhipov.blogspot.be 7
http://arhipov.blogspot.ro 7
http://arhipov.blogspot.nl 7
http://arhipov.blogspot.mx 5
http://arhipov.blogspot.com.ar 5
http://arhipov.blogspot.hk 5
http://arhipov.blogspot.kr 4
http://arhipov.blogspot.hu 4
http://www.arhipov.blogspot.ru 3
http://arhipov.blogspot.fi 3
http://arhipov.blogspot.tw 3
http://arhipov.blogspot.sk 2
http://arhipov.blogspot.no 2
http://arhipov.blogspot.sg 2
http://www.arhipov.blogspot.com 2
http://arhipov.blogspot.com.tr 2
http://us-w1.rockmelt.com 2
http://www.hanrss.com 2
http://webcache.googleusercontent.com 2
http://arhipov.blogspot.ie 2
http://reader.aol.com 1
http://feedproxy.google.com 1
http://hghltd.yandex.net 1
http://tweetedtimes.com 1
https://twitter.com 1
http://localhost 1
More...

Accessibility

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

    Java Bytecode for Discriminating Developers - JavaZone 2011 Java Bytecode for Discriminating Developers - JavaZone 2011 Presentation Transcript

    • Java Bytecode forDiscriminating Developers JavaZone’11, Oslo
    • whoamiAnton ArhipovJava dev. / Product LeadJRebel http://arhipov.blogspot.com @antonarhipov @javarebel
    • Java… Do you speak it!?
    • Java… Do you speak it!?
    • The Master Plan Examples Bytecode 101
    • THE INTRO
    • 1+2
    • +1+2 1 2
    • +1+2 1 212+
    • +1+2 1 212+
    • +1+2 1 212+ PUSH 1 1
    • +1+2 1 212+ PUSH 1 PUSH 2 2 1
    • +1+2 1 212+ PUSH 1 PUSH 2 3 ADD
    • +1+2 1 212+ ICONST_1 ICONST_2 3 IADD
    • ?=1+2
    • TAXONOMY
    • Bytecode One-byte instructions 256 possible opcodes 200+ in use
    • Bytecode One-byte instructions 256 possible opcodes 200+ in use
    • TYPE OPERATION
    • TYPE OPERATION<TYPE> ::= b, s, c, i, l, f, d, a
    • TYPE OPERATION<TYPE> ::= b, s, c, i, l, f, d, aOperations with constant values (ldc, iconst_1)
    • TYPE OPERATION<TYPE> ::= b, s, c, i, l, f, d, aOperations with constant values (ldc, iconst_1)Local variables and stack interaction (load/store)Array operations (aload, astore)Math (add, sub, mul, div)Boolean/bitwise operations (iand, ixor)Comparisons (cmpg, cmpl, ifne, ifeq)Conversions (l2d, i2l)
    • Bytecode Taxonomy Stack Manipulation
    • Bytecode Taxonomy Stack Flow Manipulation Control
    • Bytecode Taxonomy Stack Flow Manipulation Control Object Model
    • Bytecode Taxonomy Stack Flow Manipulation Control Object Arithmetics Model
    • Bytecode Taxonomy Stack Flow Manipulation Control monitorenter monitorexit Object Arithmetics Model
    • TOOLING
    • javap -c -verbose Clazz
    • HELLO, WORLD!
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); }}
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c}
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: the default constructor 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: push this to stack 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return invoke <init> on this
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello, World! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return get static field public static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello, World! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello, World! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V load string to the stack
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello, World! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V invoke method with parameter
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello, World! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c What’s #1,#2, etc ?} Compiled from "Hello.java" public class Hello extends java.lang.Object{ public Hello(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3; //String Hello, World! 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose}
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose}
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose} Compiled from "Hello.java“ public class Hello extends java.lang.Object SourceFile: "Hello.java" minor version: 0 major version: 50 Constant pool: const #1 = Method #6.#20; // java/lang/Object."<init>":()V const #2 = Field #21.#22; // java/lang/System.out:Ljava/io/PrintStream; const #3 = String #23; // Hello, World! const #4 = Method #24.#25; // java/io/PrintStream.println:(Ljava/lang/String;)V const #5 = class #26; // Hello const #6 = class #27; // java/lang/Object const #7 = Asciz <init>; const #8 = Asciz ()V;
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose} Compiled from "Hello.java“ public class Hello extends java.lang.Object SourceFile: "Hello.java" minor version: 0 major version: 50 Constant pool: const #1 = Method #6.#20; // java/lang/Object."<init>":()V const #2 = Field #21.#22; // java/lang/System.out:Ljava/io/PrintStream; const #3 = String #23; // Hello, World! const #4 = Method #24.#25; // java/io/PrintStream.println:(Ljava/lang/String;)V const #5 = class #26; // Hello const #6 = class #27; // java/lang/Object const #7 = Asciz <init>; const #8 = Asciz ()V;
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose} … public Hello(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 1: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LHello;
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose} … public Hello(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 1: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LHello;
    • public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose} … public Hello(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 1: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LHello;
    • STACKMACHINE
    • Stack Machine
    • Stack Machine JVM is a stack-based machine
    • Stack Machine JVM is a stack-based machine Each thread has a stack
    • Stack Machine JVM is a stack-based machine Each thread has a stack Stack stores frames
    • Stack Machine JVM is a stack-based machine Each thread has a stack Stack stores frames Frame is created on method invocation
    • Stack Machine JVM is a stack-based machine Each thread has a stack Stack stores frames Frame is created on method invocation Frame consists of: Operand stack Array of local variables
    • Local variables0 1 2 … NOperand stack #1 Constant Pool
    • public class Get { String name; public String getName() { return name; }}public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
    • public class Get { String name; public String getName() { return name; }}public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
    • 0 1 2 3 4 aload_0 getfield 00 02 areturnpublic java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
    • 0 1 2 3 4 2A B4 00 02 B0public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
    • STACKJUGGLING
    • dup Apop Bswapdup_x1dup2_x1
    • dup Apop Aswap Bdup_x1dup2_x1
    • dup Apop Bswapdup_x1dup2_x1
    • dup Bpop Aswapdup_x1dup2_x1
    • dup Bpop Aswap Bdup_x1dup2_x1
    • dup Bpop Aswap Bdup_x1 Bdup2_x1 A
    • How do youswap doubles?
    • dup2_x2
    • dup2_x2 dconst_0 0.0
    • dup2_x2 dconst_0 dconst_1 1.0 0.0
    • dup2_x2 dconst_0 dconst_1 1.0 swap 0.0
    • dup2_x2 dconst_0 dconst_1 1.0 swap not allowed! 0.0
    • dup2_x2 dconst_0 dconst_1 1.0 swap2 0.0
    • dup2_x2 dconst_0 dconst_1 1.0 doesn’t swap2 exist 0.0
    • dup2_x2 dconst_0 dconst_1 1.0 dup2_x2 0.0 1.0
    • dup2_x2 dconst_0 dconst_1 0.0 dup2_x2 pop2 1.0
    • dup2_x2 dconst_0 dconst_1 0.0 dup2_x2 pop2 1.0 profit! 
    • LOCALVARIABLES
    • Local Variablespublic int calculate(int value) { return value + 42;}
    • Local Variablespublic int calculate(int value) { return value + 42;}public int calculate(int); Code: Stack=2, Locals=2, Args_size=2 … LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
    • Local Variablespublic int calculate(int value) { return value + 42;}public int calculate(int); Code: Stack=2, Locals=2, Args_size=2 … LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
    • Local Variablespublic int calculate(int value) { return value + 42;}public int calculate(int); Code: Stack=2, Locals=2, Args_size=2 … LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
    • Local Variablespublic int calculate(int value) { return value + 42;} The tablepublic int calculate(int); Code: maps Stack=2, Locals=2, Args_size=2 numbers to … names LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
    • Local Variablespublic int calculate(int value) { return value + 42;}public int calculate(int); Code: Stack=2, Locals=2, Args_size=2 Sized explicitly … LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
    • Local Variables Stackvar value depth value ldc "Hello"0 0 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
    • Local Variables Stackvar value depth value ldc "Hello"0 0 "Hello" astore_01 iconst_1 1 astore_12 aload_0 23 34 4
    • Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
    • Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 1 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
    • Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 astore_01 1 iconst_1 1 astore_12 aload_0 23 34 4
    • Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 "Hello" astore_01 1 iconst_1 1 astore_12 aload_0 23 34 4
    • load LocalVariables Stack Table store
    • OBJECTS
    • Object Initializationnew 0xBB <init> Instance initialization method <clinit> Class and interface initialization method
    • Object Initialization: static {}public class Initializer { static int a; static int b; static { a = 1; } static { b = 2; } static {}; Code:} 0: iconst_1 1: putstatic #2; //Field a:I 4: iconst_2 5: putstatic #3; //Field b:I 8: return
    • Object Initialization: static {}public class Initializer { static int a; static int b; <clinit> static { a = 1; } static { b = 2; } static {}; Code:} 0: iconst_1 1: putstatic #2; //Field a:I 4: iconst_2 5: putstatic #3; //Field b:I 8: return
    • Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object(); } }
    • Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: }
    • Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: 0: aload_0 }
    • Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: 0: aload_0 } 1: invokespecial #1; //Method java/lang/Object."<init>":()V
    • Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: 0: aload_0 } 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0
    • Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: 0: aload_0 } 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class java/lang/Object 8: dup
    • Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: 0: aload_0 } 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class java/lang/Object 8: dup 9: invokespecial #1; //Method java/lang/Object."<init>":()V12: putfield #3; //Field o:Ljava/lang/Object;
    • Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: 0: aload_0 } 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class java/lang/Object 8: dup 9: invokespecial #1; //Method java/lang/Object."<init>":()V12: putfield #3; //Field o:Ljava/lang/Object;15: return
    • Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object();public Initializer(); } Code: 0: aload_0 } 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class java/lang/Object 8: dup 9: invokespecial #1; //Method java/lang/Object."<init>":()V12: putfield #3; //Field o:Ljava/lang/Object;15: return
    • Object Initialization: {}public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; }}
    • Object Initialization: {}public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; }}
    • Object Initialization: {}public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; }}
    • Object Initialization: {}public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; }}
    • Object Initialization: {}public class Initializer { int a; public Initializer(int); int b; Code: int c; 0: aload_0 1: invokespecial #1; // ..<init> 4: aload_0 { a = 1; } 5: iconst_1 6: putfield #2; //Field a:I public Initializer(int b) { 9: aload_0 this.b = b; 10: iconst_2 } 11: putfield #3; //Field c:I 14: aload_0 { c = 2; } 15: iload_1} 16: putfield #4; //Field b:I 19: return
    • There’s no initializer
    • METHOD INVOCATION & PARAMETER PASSINGMETHODINVOCATION
    • Java Method Invocation
    • Java Method Invocation invokestatic invokespecial invokevirtual invokeinterface invokedynamic
    • Java Method Invocation invokestatic invokespecial invokevirtual invokeinterface Integer.valueOf(“42”) invokedynamic
    • Java Method Invocation invokestatic invokespecial <init> invokevirtual invokeinterface invokedynamic private void foo(); super.method();
    • Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface invokedynamic
    • Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B invokedynamic
    • Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B A/method1 invokedynamic B/method2 B/method3
    • Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B impl X A/method1 invokedynamic B/method2 B/method3 X/methodX
    • Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B impl X A/method1 invokedynamic B/method2 B/method3 X/methodX class D impl X D/method1 X/methodX
    • Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B impl X A/method1 invokedynamic B/method2 B/method3 X/methodX class D impl X D/method1 X/methodX
    • Java Method Invocation invokestatic invokespecial invokevirtual invokeinterface invokedynamicEfficient Implementation of Java Interfaces:Invokeinterface Considered Harmless, BowenAlpern, Anthony Cocchi, Stephen Fink, DavidGrove, and Derek Lieber, OOPSLA’01
    • Method Invocationobj.method(param1, param2);
    • Method Invocationobj.method(param1, param2); push obj push param1 push param2 call method
    • Method Invocationobj.method(param1, param2); obj push obj push param1 push param2 call method
    • Method Invocationobj.method(param1, param2); param1 push obj obj push param1 push param2 call method
    • Method Invocationobj.method(param1, param2); param2 push obj param1 push param1 obj push param2 call method
    • Method Invocationobj.method(param1, param2); obj? push obj push param1 push param2 call method
    • INNERCLASSES
    • Inner Classes public class Car { class Engine { public void start() { move(); } } private void move() { } }
    • Inner Classesclass Car$Engine extends j.l.Object{final Car this$0;Car$Engine(Car);public void start(); Code: 0: aload_0 1: getfield #1; //Field this$0:LCar; 4: invokestatic #3; // Car.access$000:(LCar;)V 7: return}
    • Inner Classes public class Car extends j.l.Object{ public Car(); private void move();class Car$Engine extends j.l.Object{ static void access$000(Car);final Car this$0; Code: 0: aload_0Car$Engine(Car); 1: invokespecial #1; // move: ()V; 4: returnpublic void start(); } Code: 0: aload_0 1: getfield #1; //Field this$0:LCar; 4: invokestatic #3; // Car.access$000:(LCar;)V 7: return}
    • Inner Classes public class Car extends j.l.Object{ public Car(); private void move();class Car$Engine extends j.l.Object{ static void access$000(Car);final Car this$0; Code: 0: aload_0Car$Engine(Car); 1: invokespecial #1; // move: ()V; 4: returnpublic void start(); } Code: 0: aload_0 1: getfield #1; //Field this$0:LCar; 4: invokestatic #3; // Car.access$000:(LCar;)V 7: return}
    • “HOW DO THEY DO THAT?”
    • object Singleton { def test={}}
    • object Singleton { def test={} } $> scalac Singleton.scalaSingleton.class Singleton$.class
    • public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}
    • public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}
    • public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}
    • public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}public final class Singleton$ extends java.lang.Object implementsscala.ScalaObject {public static final Singleton$ MODULE$;public static {}; Code: 0: new #9; //class Singleton$ 3: invokespecial #12; //Method "<init>":()V 6: returnpublic void test();private Singleton$();}
    • public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}public final class Singleton$ extends java.lang.Object implementsscala.ScalaObject {public static final Singleton$ MODULE$;public static {}; Code: 0: new #9; //class Singleton$ 3: invokespecial #12; //Method "<init>":()V 6: returnpublic void test();private Singleton$();}
    • public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}public final class Singleton$ extends java.lang.Object implementsscala.ScalaObject {public static final Singleton$ MODULE$;public static {}; Code: 0: new #9; //class Singleton$ 3: invokespecial #12; //Method "<init>":()V 6: returnpublic void test();private Singleton$();}
    • public final class Singleton extends java.lang.Object {public static final void test(); Code: 0: getstatic #11; //Field Singleton$.MODULE$:LSingleton$; 3: invokevirtual #13; //Method Singleton$.test:()V 6: return}public final class Singleton$ extends java.lang.Object implementsscala.ScalaObject {public static final Singleton$ MODULE$;public static {};public void test();private Singleton$();Code: 0: aload_0 1: invokespecial #17; //Method java/lang/Object."<init>":()V 4: aload_0 5: putstatic #19; //Field MODULE$:LSingleton$; 8: return
    • object Singleton { def test={}}
    • public class Singleton { public void test(){ Singleton$.MODULE$.test(); } }object Singleton { def test={}}
    • public class Singleton { public void test(){ Singleton$.MODULE$.test(); } } public final class Singleton$object Singleton { implements scala.ScalaObject { def test={} public static final Singleton$ MODULE$;} static { new Singleton$(); } private Singleton$(){ MODULE$ = this; } public void test() { } }
    • CRASH!BOOM!BANG!
    • Javassist Bytecode manipulation made easy Source-level and bytecode-level API Uses the vocabulary of Java language On-the-fly compilation of the injected code http://www.jboss.org/javassist
    • for(int i = 0; i < 100; i++){ int a = 0; try { while (true) { a++; foo(a); if(a > 1) break; } } catch (Exception e) { }}
    • JavassistCtMethod method = …method.setBody(“ for(int i = 0; i < 100; i++){ int a = 0; try { while (true) { a++; foo(a); if(a > 1) break; } } catch (Exception e) { } }”);
    • -Xverify:allException in thread "main" java.lang.VerifyError:(class: zt/javassist/My, method: test signature:()V) Inconsistent stack height 0 != 1
    • public void test(); Code: Stack=2, Locals=4, Args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 42 8: iconst_0 9: istore_2 10: goto 29 13: iinc 2, 1 16: aload_0 17: iload_2 18: invokevirtual #23; //Method zt/javassist/My.foo:(I)V 21: iload_2 22: iconst_1 23: if_icmple 29 26: goto 32 29: goto 13 32: astore_3 33: goto 36 36: iinc 1, 1 39: goto 2 42: return Exception table: from to target type 10 32 32 Class java/lang/Exception
    • public void test(); Code: Stack=2, Locals=4, Args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 42 8: iconst_0 9: istore_2 10: goto 29 13: iinc 2, 1 16: aload_0 17: iload_2 18: invokevirtual #23; //Method zt/javassist/My.foo:(I)V 21: iload_2 No local 22: iconst_1 23: if_icmple 29 variables table 26: goto 32 29: goto 13 32: astore_3 33: goto 36 36: iinc 1, 1 39: goto 2 42: return Exception table: from to target type 10 32 32 Class java/lang/Exception
    • public void test(); Code: Stack=2, Locals=4, Args_size=1 0: iconst_0 1: istore_1 2: iload_1 3: bipush 100 5: if_icmpge 42 8: iconst_0 9: istore_2 10: goto 29 13: iinc 2, 1 16: aload_0 17: iload_2 18: invokevirtual #23; //Method zt/javassist/My.foo:(I)V 21: iload_2 No local 22: iconst_1 23: if_icmple 29 variables table 26: goto 32 29: goto 13 32: astore_3 33: goto 36 36: iinc 1, 1 39: goto 2 42: return Exception table: from to target type 10 32 32 Class java/lang/Exception
    • Bytecode is fun! Know your platform! Maybe, you can build your own language? You may need to read bytecode someday.
    • http://arhipov.blogspot.com ant.arhipov@gmail.com @antonarhipov @javarebel