SlideShare a Scribd company logo
Java Bytecode for
Discriminating Developers
      JavaZone’11, Oslo
whoami


Anton Arhipov
Java dev. / Product Lead

JRebel

                           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       2
12+
+
1+2
      1       2
12+
+
1+2
           1           2
12+   PUSH 1
                   1
+
1+2
           1           2
12+   PUSH 1
      PUSH 2
                   2
                   1
+
1+2
           1           2
12+   PUSH 1
      PUSH 2
                   3
      ADD
+
1+2
           1             2
12+   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, a
Operations with constant values (ldc, iconst_1)
TYPE OPERATION

<TYPE> ::= b, s, c, i, l, f, d, a
Operations 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;
STACK
MACHINE
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 variables
0 1 2      … N
Operand 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: areturn
LocalVariableTable:
 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: areturn
LocalVariableTable:
 Start Length Slot Name Signature
 0    5    0 this       LGet;
0                     1             2         3   4


   aload_0 getfield 00 02 areturn

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: areturn
LocalVariableTable:
 Start Length Slot Name Signature
 0    5    0 this       LGet;
0                     1             2         3   4


         2A                   B4             00 02          B0

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: areturn
LocalVariableTable:
 Start Length Slot Name Signature
 0    5    0 this       LGet;
Java Bytecode for Discriminating Developers - JavaZone 2011
STACK
JUGGLING
dup       A
pop       B
swap
dup_x1
dup2_x1
dup       A
pop       A
swap      B
dup_x1
dup2_x1
dup       A
pop       B
swap
dup_x1
dup2_x1
dup       B
pop       A
swap
dup_x1
dup2_x1
dup       B
pop       A
swap      B
dup_x1
dup2_x1
dup       B
pop       A
swap      B
dup_x1    B
dup2_x1   A
How do you
swap 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! 
LOCAL
VARIABLES
Local Variables

public int calculate(int value) {
  return value + 42;
}
Local Variables

public 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 Variables

public 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 Variables

public 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 Variables

public int calculate(int value) {
  return value + 42;
}
                                              The table
public 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 Variables

public 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                         Stack

var        value                      depth     value
                        ldc "Hello"
0                                      0
                        astore_0
1                       iconst_1       1
                        astore_1
2                       aload_0        2

3                                      3

4                                      4
Local Variables                         Stack

var        value                      depth     value
                        ldc "Hello"
0                                      0       "Hello"
                        astore_0
1                       iconst_1       1
                        astore_1
2                       aload_0        2

3                                      3

4                                      4
Local Variables                         Stack

var        value                      depth     value
                        ldc "Hello"
0         "Hello"                      0
                        astore_0
1                       iconst_1       1
                        astore_1
2                       aload_0        2

3                                      3

4                                      4
Local Variables                         Stack

var        value                      depth     value
                        ldc "Hello"
0         "Hello"                      0              1
                        astore_0
1                       iconst_1       1
                        astore_1
2                       aload_0        2

3                                      3

4                                      4
Local Variables                         Stack

var        value                      depth     value
                        ldc "Hello"
0         "Hello"                      0
                        astore_0
1             1         iconst_1       1
                        astore_1
2                       aload_0        2

3                                      3

4                                      4
Local Variables                         Stack

var        value                      depth     value
                        ldc "Hello"
0         "Hello"                      0       "Hello"
                        astore_0
1             1         iconst_1       1
                        astore_1
2                       aload_0        2

3                                      3

4                                      4
load
  Local
Variables           Stack
  Table

            store
OBJECTS
Object Initialization



new
 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>":()V
12: 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>":()V
12: 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>":()V
12: 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
Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011
There’s no initializer
METHOD INVOCATION
 &
 PARAMETER PASSING



METHOD
INVOCATION
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
  invokedynamic


Efficient Implementation of Java Interfaces:
Invokeinterface Considered Harmless, Bowen
Alpern, Anthony Cocchi, Stephen Fink, David
Grove, and Derek Lieber, OOPSLA’01
Method Invocation



obj.method(param1, param2);
Method Invocation



obj.method(param1, param2);

      push obj
      push param1
      push param2
      call method
Method Invocation



obj.method(param1, param2);
                              obj
      push obj
      push param1
      push param2
      call method
Method Invocation



obj.method(param1, param2);
                              param1
      push obj
                               obj
      push param1
      push param2
      call method
Method Invocation



obj.method(param1, param2);
                              param2
      push obj
                              param1
      push param1
                               obj
      push param2
      call method
Method Invocation



obj.method(param1, param2);
                              obj?
      push obj
      push param1
      push param2
      call method
INNER
CLASSES
Inner Classes

            public class Car {

                class Engine {
                  public void start() {
                    move();
                  }
                }

                private void move() {
                }
            }
Inner Classes




class 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_0
Car$Engine(Car);                          1: invokespecial #1; // move: ()V;
                                          4: return
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_0
Car$Engine(Car);                          1: invokespecial #1; // move: ()V;
                                          4: return
public 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.scala




Singleton.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 implements
scala.ScalaObject {
public static final Singleton$ MODULE$;
public static {};
  Code:
  0: new #9; //class Singleton$
  3: invokespecial #12; //Method "<init>":()V
  6: return
public 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 implements
scala.ScalaObject {
public static final Singleton$ MODULE$;
public static {};
  Code:
  0: new #9; //class Singleton$
  3: invokespecial #12; //Method "<init>":()V
  6: return
public 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 implements
scala.ScalaObject {
public static final Singleton$ MODULE$;
public static {};
  Code:
  0: new #9; //class Singleton$
  3: invokespecial #12; //Method "<init>":()V
  6: return
public 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 implements
scala.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) {
  }
}
Javassist

CtMethod 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:all


Exception 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
Ad

More Related Content

What's hot (19)

JavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with JavassistJavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with Javassist
Anton Arhipov
 
Google Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG NantesGoogle Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG Nantes
mikaelbarbero
 
Jdk 7 4-forkjoin
Jdk 7 4-forkjoinJdk 7 4-forkjoin
Jdk 7 4-forkjoin
knight1128
 
Migrating to JUnit 5
Migrating to JUnit 5Migrating to JUnit 5
Migrating to JUnit 5
Rafael Winterhalter
 
Riga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with JavassistRiga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with Javassist
Anton Arhipov
 
Mastering Java ByteCode
Mastering Java ByteCodeMastering Java ByteCode
Mastering Java ByteCode
Ecommerce Solution Provider SysIQ
 
Java libraries you can't afford to miss
Java libraries you can't afford to missJava libraries you can't afford to miss
Java libraries you can't afford to miss
Andres Almiray
 
Voxxed Days Vilnius 2015 - Having fun with Javassist
Voxxed Days Vilnius 2015 - Having fun with JavassistVoxxed Days Vilnius 2015 - Having fun with Javassist
Voxxed Days Vilnius 2015 - Having fun with Javassist
Anton Arhipov
 
Sailing with Java 8 Streams
Sailing with Java 8 StreamsSailing with Java 8 Streams
Sailing with Java 8 Streams
Ganesh Samarthyam
 
Software Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW SydneySoftware Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW Sydney
julien.ponge
 
Con-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With JavassistCon-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With Javassist
Anton Arhipov
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
Pramod Kumar
 
Kotlin is charming; The reasons Java engineers should start Kotlin.
Kotlin is charming; The reasons Java engineers should start Kotlin.Kotlin is charming; The reasons Java engineers should start Kotlin.
Kotlin is charming; The reasons Java engineers should start Kotlin.
JustSystems Corporation
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
julien.ponge
 
Java 7 LavaJUG
Java 7 LavaJUGJava 7 LavaJUG
Java 7 LavaJUG
julien.ponge
 
모던자바의 역습
모던자바의 역습모던자바의 역습
모던자바의 역습
DoHyun Jung
 
Jdk(java) 7 - 6 기타기능
Jdk(java) 7 - 6 기타기능Jdk(java) 7 - 6 기타기능
Jdk(java) 7 - 6 기타기능
knight1128
 
Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS Programmers
David Rodenas
 
Unit testing without Robolectric, Droidcon Berlin 2016
Unit testing without Robolectric, Droidcon Berlin 2016Unit testing without Robolectric, Droidcon Berlin 2016
Unit testing without Robolectric, Droidcon Berlin 2016
Danny Preussler
 
JavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with JavassistJavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with Javassist
Anton Arhipov
 
Google Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG NantesGoogle Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG Nantes
mikaelbarbero
 
Jdk 7 4-forkjoin
Jdk 7 4-forkjoinJdk 7 4-forkjoin
Jdk 7 4-forkjoin
knight1128
 
Riga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with JavassistRiga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with Javassist
Anton Arhipov
 
Java libraries you can't afford to miss
Java libraries you can't afford to missJava libraries you can't afford to miss
Java libraries you can't afford to miss
Andres Almiray
 
Voxxed Days Vilnius 2015 - Having fun with Javassist
Voxxed Days Vilnius 2015 - Having fun with JavassistVoxxed Days Vilnius 2015 - Having fun with Javassist
Voxxed Days Vilnius 2015 - Having fun with Javassist
Anton Arhipov
 
Software Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW SydneySoftware Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW Sydney
julien.ponge
 
Con-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With JavassistCon-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With Javassist
Anton Arhipov
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
Pramod Kumar
 
Kotlin is charming; The reasons Java engineers should start Kotlin.
Kotlin is charming; The reasons Java engineers should start Kotlin.Kotlin is charming; The reasons Java engineers should start Kotlin.
Kotlin is charming; The reasons Java engineers should start Kotlin.
JustSystems Corporation
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
julien.ponge
 
모던자바의 역습
모던자바의 역습모던자바의 역습
모던자바의 역습
DoHyun Jung
 
Jdk(java) 7 - 6 기타기능
Jdk(java) 7 - 6 기타기능Jdk(java) 7 - 6 기타기능
Jdk(java) 7 - 6 기타기능
knight1128
 
Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS Programmers
David Rodenas
 
Unit testing without Robolectric, Droidcon Berlin 2016
Unit testing without Robolectric, Droidcon Berlin 2016Unit testing without Robolectric, Droidcon Berlin 2016
Unit testing without Robolectric, Droidcon Berlin 2016
Danny Preussler
 

Similar to Java Bytecode for Discriminating Developers - JavaZone 2011 (20)

Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012
Anton Arhipov
 
No dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real worldNo dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real world
tcurdt
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
Zeeshan MIrza
 
Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
mrinalbhutani
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for Dummies
Charles Nutter
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
Ganesh Samarthyam
 
Java Basics
Java BasicsJava Basics
Java Basics
Sunil OS
 
Fast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible JavaFast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible Java
Charles Nutter
 
Why is Java relevant? New features of Java8
Why is Java relevant? New features of Java8 Why is Java relevant? New features of Java8
Why is Java relevant? New features of Java8
xshyamx
 
Eric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build systemEric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build system
GuardSquare
 
Advanced debugging  techniques in different environments
Advanced debugging  techniques in different environmentsAdvanced debugging  techniques in different environments
Advanced debugging  techniques in different environments
Andrii Soldatenko
 
Java Programming Fundamentals: Complete Guide for Beginners
Java Programming Fundamentals: Complete Guide for BeginnersJava Programming Fundamentals: Complete Guide for Beginners
Java Programming Fundamentals: Complete Guide for Beginners
Taranath Jaishy
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast Lane
Andres Almiray
 
Play!ng with scala
Play!ng with scalaPlay!ng with scala
Play!ng with scala
Siarzh Miadzvedzeu
 
Presentation to java
Presentation  to  javaPresentation  to  java
Presentation to java
Ganesh Chittalwar
 
Introduction to-java
Introduction to-javaIntroduction to-java
Introduction to-java
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH, SINDH, PAKISTAN
 
How to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineHow to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machine
Chun-Yu Wang
 
Hello java
Hello java  Hello java
Hello java
University of Babylon
 
Hello Java-First Level
Hello Java-First LevelHello Java-First Level
Hello Java-First Level
Dr. Raaid Alubady
 
Hello java
Hello java   Hello java
Hello java
University of Babylon
 
Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012
Anton Arhipov
 
No dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real worldNo dark magic - Byte code engineering in the real world
No dark magic - Byte code engineering in the real world
tcurdt
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
Zeeshan MIrza
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for Dummies
Charles Nutter
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
Ganesh Samarthyam
 
Java Basics
Java BasicsJava Basics
Java Basics
Sunil OS
 
Fast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible JavaFast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible Java
Charles Nutter
 
Why is Java relevant? New features of Java8
Why is Java relevant? New features of Java8 Why is Java relevant? New features of Java8
Why is Java relevant? New features of Java8
xshyamx
 
Eric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build systemEric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build system
GuardSquare
 
Advanced debugging  techniques in different environments
Advanced debugging  techniques in different environmentsAdvanced debugging  techniques in different environments
Advanced debugging  techniques in different environments
Andrii Soldatenko
 
Java Programming Fundamentals: Complete Guide for Beginners
Java Programming Fundamentals: Complete Guide for BeginnersJava Programming Fundamentals: Complete Guide for Beginners
Java Programming Fundamentals: Complete Guide for Beginners
Taranath Jaishy
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast Lane
Andres Almiray
 
How to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineHow to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machine
Chun-Yu Wang
 
Ad

More from Anton Arhipov (20)

JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfJavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdf
Anton Arhipov
 
Idiomatic kotlin
Idiomatic kotlinIdiomatic kotlin
Idiomatic kotlin
Anton Arhipov
 
TechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервьюTechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервью
Anton Arhipov
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
Anton Arhipov
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
Anton Arhipov
 
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
Devoxx Ukraine 2018 - Kotlin DSL in under an hourDevoxx Ukraine 2018 - Kotlin DSL in under an hour
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
Anton Arhipov
 
GeeCON Prague 2018 - Kotlin DSL in under an hour
GeeCON Prague 2018 - Kotlin DSL in under an hourGeeCON Prague 2018 - Kotlin DSL in under an hour
GeeCON Prague 2018 - Kotlin DSL in under an hour
Anton Arhipov
 
Build pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity and Kotlin DSLBuild pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity and Kotlin DSL
Anton Arhipov
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
Anton Arhipov
 
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainersJavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
Anton Arhipov
 
GeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainersGeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainers
Anton Arhipov
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
Anton Arhipov
 
JavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - TestContainers: integration testing without the hassleJavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - TestContainers: integration testing without the hassle
Anton Arhipov
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
Anton Arhipov
 
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloadingJavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
Anton Arhipov
 
JUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentationJUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentation
Anton Arhipov
 
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloadingRiga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Anton Arhipov
 
Devclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервьюDevclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервью
Anton Arhipov
 
Joker 2016 - Bytecode 101
Joker 2016 - Bytecode 101Joker 2016 - Bytecode 101
Joker 2016 - Bytecode 101
Anton Arhipov
 
JPoint 2016 - Etudes of DIY Java profiler
JPoint 2016 - Etudes of DIY Java profilerJPoint 2016 - Etudes of DIY Java profiler
JPoint 2016 - Etudes of DIY Java profiler
Anton Arhipov
 
JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfJavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdf
Anton Arhipov
 
TechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервьюTechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервью
Anton Arhipov
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
Anton Arhipov
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
Anton Arhipov
 
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
Devoxx Ukraine 2018 - Kotlin DSL in under an hourDevoxx Ukraine 2018 - Kotlin DSL in under an hour
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
Anton Arhipov
 
GeeCON Prague 2018 - Kotlin DSL in under an hour
GeeCON Prague 2018 - Kotlin DSL in under an hourGeeCON Prague 2018 - Kotlin DSL in under an hour
GeeCON Prague 2018 - Kotlin DSL in under an hour
Anton Arhipov
 
Build pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity and Kotlin DSLBuild pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity and Kotlin DSL
Anton Arhipov
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
Anton Arhipov
 
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainersJavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
Anton Arhipov
 
GeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainersGeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainers
Anton Arhipov
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
Anton Arhipov
 
JavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - TestContainers: integration testing without the hassleJavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - TestContainers: integration testing without the hassle
Anton Arhipov
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
Anton Arhipov
 
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloadingJavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
Anton Arhipov
 
JUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentationJUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentation
Anton Arhipov
 
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloadingRiga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Anton Arhipov
 
Devclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервьюDevclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервью
Anton Arhipov
 
Joker 2016 - Bytecode 101
Joker 2016 - Bytecode 101Joker 2016 - Bytecode 101
Joker 2016 - Bytecode 101
Anton Arhipov
 
JPoint 2016 - Etudes of DIY Java profiler
JPoint 2016 - Etudes of DIY Java profilerJPoint 2016 - Etudes of DIY Java profiler
JPoint 2016 - Etudes of DIY Java profiler
Anton Arhipov
 
Ad

Recently uploaded (20)

UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018
#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018
#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018
Lynda Kane
 
Buckeye Dreamin' 2023: De-fogging Debug Logs
Buckeye Dreamin' 2023: De-fogging Debug LogsBuckeye Dreamin' 2023: De-fogging Debug Logs
Buckeye Dreamin' 2023: De-fogging Debug Logs
Lynda Kane
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Network Security. Different aspects of Network Security.
Network Security. Different aspects of Network Security.Network Security. Different aspects of Network Security.
Network Security. Different aspects of Network Security.
gregtap1
 
Learn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step GuideLearn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step Guide
Marcel David
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
"Rebranding for Growth", Anna Velykoivanenko
"Rebranding for Growth", Anna Velykoivanenko"Rebranding for Growth", Anna Velykoivanenko
"Rebranding for Growth", Anna Velykoivanenko
Fwdays
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Asthma presentación en inglés abril 2025 pdf
Asthma presentación en inglés abril 2025 pdfAsthma presentación en inglés abril 2025 pdf
Asthma presentación en inglés abril 2025 pdf
VanessaRaudez
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Automation Dreamin' 2022: Sharing Some Gratitude with Your Users
Automation Dreamin' 2022: Sharing Some Gratitude with Your UsersAutomation Dreamin' 2022: Sharing Some Gratitude with Your Users
Automation Dreamin' 2022: Sharing Some Gratitude with Your Users
Lynda Kane
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018
#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018
#AdminHour presents: Hour of Code2018 slide deck from 12/6/2018
Lynda Kane
 
Buckeye Dreamin' 2023: De-fogging Debug Logs
Buckeye Dreamin' 2023: De-fogging Debug LogsBuckeye Dreamin' 2023: De-fogging Debug Logs
Buckeye Dreamin' 2023: De-fogging Debug Logs
Lynda Kane
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Network Security. Different aspects of Network Security.
Network Security. Different aspects of Network Security.Network Security. Different aspects of Network Security.
Network Security. Different aspects of Network Security.
gregtap1
 
Learn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step GuideLearn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step Guide
Marcel David
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
"Rebranding for Growth", Anna Velykoivanenko
"Rebranding for Growth", Anna Velykoivanenko"Rebranding for Growth", Anna Velykoivanenko
"Rebranding for Growth", Anna Velykoivanenko
Fwdays
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Asthma presentación en inglés abril 2025 pdf
Asthma presentación en inglés abril 2025 pdfAsthma presentación en inglés abril 2025 pdf
Asthma presentación en inglés abril 2025 pdf
VanessaRaudez
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Automation Dreamin' 2022: Sharing Some Gratitude with Your Users
Automation Dreamin' 2022: Sharing Some Gratitude with Your UsersAutomation Dreamin' 2022: Sharing Some Gratitude with Your Users
Automation Dreamin' 2022: Sharing Some Gratitude with Your Users
Lynda Kane
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 

Java Bytecode for Discriminating Developers - JavaZone 2011

  • 1. Java Bytecode for Discriminating Developers JavaZone’11, Oslo
  • 2. whoami Anton Arhipov Java dev. / Product Lead JRebel http://arhipov.blogspot.com @antonarhipov @javarebel
  • 3. Java… Do you speak it!?
  • 4. Java… Do you speak it!?
  • 5. The Master Plan Examples Bytecode 101
  • 7. 1+2
  • 8. + 1+2 1 2
  • 9. + 1+2 1 2 12+
  • 10. + 1+2 1 2 12+
  • 11. + 1+2 1 2 12+ PUSH 1 1
  • 12. + 1+2 1 2 12+ PUSH 1 PUSH 2 2 1
  • 13. + 1+2 1 2 12+ PUSH 1 PUSH 2 3 ADD
  • 14. + 1+2 1 2 12+ ICONST_1 ICONST_2 3 IADD
  • 15. ?=1+2
  • 17. Bytecode One-byte instructions 256 possible opcodes 200+ in use
  • 18. Bytecode One-byte instructions 256 possible opcodes 200+ in use
  • 20. TYPE OPERATION <TYPE> ::= b, s, c, i, l, f, d, a
  • 21. TYPE OPERATION <TYPE> ::= b, s, c, i, l, f, d, a Operations with constant values (ldc, iconst_1)
  • 22. TYPE OPERATION <TYPE> ::= b, s, c, i, l, f, d, a Operations 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)
  • 23. Bytecode Taxonomy Stack Manipulation
  • 24. Bytecode Taxonomy Stack Flow Manipulation Control
  • 25. Bytecode Taxonomy Stack Flow Manipulation Control Object Model
  • 26. Bytecode Taxonomy Stack Flow Manipulation Control Object Arithmetics Model
  • 27. Bytecode Taxonomy Stack Flow Manipulation Control monitorenter monitorexit Object Arithmetics Model
  • 31. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } }
  • 32. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c }
  • 33. 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
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. 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
  • 38. 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
  • 39. 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
  • 40. 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
  • 41. 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
  • 42. 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
  • 43. 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
  • 44. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose }
  • 45. public class Hello { public static void main(String[] args) { System.out.println ( “Hello, World!” ); } C:workgeeconclasses>javap Hello -c -verbose }
  • 46. 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;
  • 47. 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;
  • 48. 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;
  • 49. 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;
  • 50. 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;
  • 53. Stack Machine JVM is a stack-based machine
  • 54. Stack Machine JVM is a stack-based machine Each thread has a stack
  • 55. Stack Machine JVM is a stack-based machine Each thread has a stack Stack stores frames
  • 56. Stack Machine JVM is a stack-based machine Each thread has a stack Stack stores frames Frame is created on method invocation
  • 57. 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
  • 58. Local variables 0 1 2 … N Operand stack #1 Constant Pool
  • 59. 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: areturn LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  • 60. 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: areturn LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  • 61. 0 1 2 3 4 aload_0 getfield 00 02 areturn 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: areturn LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  • 62. 0 1 2 3 4 2A B4 00 02 B0 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: areturn LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  • 65. dup A pop B swap dup_x1 dup2_x1
  • 66. dup A pop A swap B dup_x1 dup2_x1
  • 67. dup A pop B swap dup_x1 dup2_x1
  • 68. dup B pop A swap dup_x1 dup2_x1
  • 69. dup B pop A swap B dup_x1 dup2_x1
  • 70. dup B pop A swap B dup_x1 B dup2_x1 A
  • 71. How do you swap doubles?
  • 76. dup2_x2 dconst_0 dconst_1 1.0 swap not allowed! 0.0
  • 77. dup2_x2 dconst_0 dconst_1 1.0 swap2 0.0
  • 78. dup2_x2 dconst_0 dconst_1 1.0 doesn’t swap2 exist 0.0
  • 79. dup2_x2 dconst_0 dconst_1 1.0 dup2_x2 0.0 1.0
  • 80. dup2_x2 dconst_0 dconst_1 0.0 dup2_x2 pop2 1.0
  • 81. dup2_x2 dconst_0 dconst_1 0.0 dup2_x2 pop2 1.0 profit! 
  • 83. Local Variables public int calculate(int value) { return value + 42; }
  • 84. Local Variables public 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
  • 85. Local Variables public 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
  • 86. Local Variables public 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
  • 87. Local Variables public int calculate(int value) { return value + 42; } The table public 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
  • 88. Local Variables public 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
  • 89. Local Variables Stack var value depth value ldc "Hello" 0 0 astore_0 1 iconst_1 1 astore_1 2 aload_0 2 3 3 4 4
  • 90. Local Variables Stack var value depth value ldc "Hello" 0 0 "Hello" astore_0 1 iconst_1 1 astore_1 2 aload_0 2 3 3 4 4
  • 91. Local Variables Stack var value depth value ldc "Hello" 0 "Hello" 0 astore_0 1 iconst_1 1 astore_1 2 aload_0 2 3 3 4 4
  • 92. Local Variables Stack var value depth value ldc "Hello" 0 "Hello" 0 1 astore_0 1 iconst_1 1 astore_1 2 aload_0 2 3 3 4 4
  • 93. Local Variables Stack var value depth value ldc "Hello" 0 "Hello" 0 astore_0 1 1 iconst_1 1 astore_1 2 aload_0 2 3 3 4 4
  • 94. Local Variables Stack var value depth value ldc "Hello" 0 "Hello" 0 "Hello" astore_0 1 1 iconst_1 1 astore_1 2 aload_0 2 3 3 4 4
  • 95. load Local Variables Stack Table store
  • 97. Object Initialization new 0xBB <init> Instance initialization method <clinit> Class and interface initialization method
  • 98. 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
  • 99. 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
  • 100. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object(); } }
  • 101. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object(); public Initializer(); } Code: }
  • 102. Object Initialization: new public class Initializer { Object o; public Initializer() { o = new Object(); public Initializer(); } Code: 0: aload_0 }
  • 103. 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
  • 104. 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
  • 105. 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
  • 106. 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>":()V 12: putfield #3; //Field o:Ljava/lang/Object;
  • 107. 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>":()V 12: putfield #3; //Field o:Ljava/lang/Object; 15: return
  • 108. 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>":()V 12: putfield #3; //Field o:Ljava/lang/Object; 15: return
  • 109. Object Initialization: {} public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; } }
  • 110. Object Initialization: {} public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; } }
  • 111. Object Initialization: {} public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; } }
  • 112. Object Initialization: {} public class Initializer { int a; int b; int c; { a = 1; } public Initializer(int b) { this.b = b; } { c = 2; } }
  • 113. 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
  • 117. METHOD INVOCATION & PARAMETER PASSING METHOD INVOCATION
  • 119. Java Method Invocation invokestatic invokespecial invokevirtual invokeinterface invokedynamic
  • 120. Java Method Invocation invokestatic invokespecial invokevirtual invokeinterface Integer.valueOf(“42”) invokedynamic
  • 121. Java Method Invocation invokestatic invokespecial <init> invokevirtual invokeinterface invokedynamic private void foo(); super.method();
  • 122. Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface invokedynamic
  • 123. Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B invokedynamic
  • 124. Java Method Invocation invokestatic class A invokespecial A/method1 A/method2 invokevirtual invokeinterface class B A/method1 invokedynamic B/method2 B/method3
  • 125. 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
  • 126. 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
  • 127. 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
  • 128. Java Method Invocation invokestatic invokespecial invokevirtual invokeinterface invokedynamic Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless, Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, and Derek Lieber, OOPSLA’01
  • 130. Method Invocation obj.method(param1, param2); push obj push param1 push param2 call method
  • 131. Method Invocation obj.method(param1, param2); obj push obj push param1 push param2 call method
  • 132. Method Invocation obj.method(param1, param2); param1 push obj obj push param1 push param2 call method
  • 133. Method Invocation obj.method(param1, param2); param2 push obj param1 push param1 obj push param2 call method
  • 134. Method Invocation obj.method(param1, param2); obj? push obj push param1 push param2 call method
  • 136. Inner Classes public class Car { class Engine { public void start() { move(); } } private void move() { } }
  • 137. Inner Classes class 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 }
  • 138. 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_0 Car$Engine(Car); 1: invokespecial #1; // move: ()V; 4: return public void start(); } Code: 0: aload_0 1: getfield #1; //Field this$0:LCar; 4: invokestatic #3; // Car.access$000:(LCar;)V 7: return }
  • 139. 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_0 Car$Engine(Car); 1: invokespecial #1; // move: ()V; 4: return public void start(); } Code: 0: aload_0 1: getfield #1; //Field this$0:LCar; 4: invokestatic #3; // Car.access$000:(LCar;)V 7: return }
  • 140. “HOW DO THEY DO THAT?”
  • 141. object Singleton { def test={} }
  • 142. object Singleton { def test={} } $> scalac Singleton.scala Singleton.class Singleton$.class
  • 143. 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 }
  • 144. 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 }
  • 145. 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 }
  • 146. 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 implements scala.ScalaObject { public static final Singleton$ MODULE$; public static {}; Code: 0: new #9; //class Singleton$ 3: invokespecial #12; //Method "<init>":()V 6: return public void test(); private Singleton$(); }
  • 147. 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 implements scala.ScalaObject { public static final Singleton$ MODULE$; public static {}; Code: 0: new #9; //class Singleton$ 3: invokespecial #12; //Method "<init>":()V 6: return public void test(); private Singleton$(); }
  • 148. 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 implements scala.ScalaObject { public static final Singleton$ MODULE$; public static {}; Code: 0: new #9; //class Singleton$ 3: invokespecial #12; //Method "<init>":()V 6: return public void test(); private Singleton$(); }
  • 149. 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 implements scala.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
  • 150. object Singleton { def test={} }
  • 151. public class Singleton { public void test(){ Singleton$.MODULE$.test(); } } object Singleton { def test={} }
  • 152. 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() { } }
  • 154. 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
  • 155. for(int i = 0; i < 100; i++){ int a = 0; try { while (true) { a++; foo(a); if(a > 1) break; } } catch (Exception e) { } }
  • 156. Javassist CtMethod 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) { } }”);
  • 157. -Xverify:all Exception in thread "main" java.lang.VerifyError: (class: zt/javassist/My, method: test signature: ()V) Inconsistent stack height 0 != 1
  • 158. 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
  • 159. 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
  • 160. 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
  • 161. Bytecode is fun! Know your platform! Maybe, you can build your own language? You may need to read bytecode someday.