SlideShare ist ein Scribd-Unternehmen logo
1 von 162
Downloaden Sie, um offline zu lesen
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;
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

JavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with JavassistJavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with JavassistAnton Arhipov
 
Google Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG NantesGoogle Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG Nantesmikaelbarbero
 
Jdk 7 4-forkjoin
Jdk 7 4-forkjoinJdk 7 4-forkjoin
Jdk 7 4-forkjoinknight1128
 
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 JavassistAnton 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 missAndres 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 JavassistAnton 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 Sydneyjulien.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 JavassistAnton Arhipov
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and UtilitiesPramod 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 ProgrammersDavid 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 2016Danny Preussler
 
Kotlin 101 for Java Developers
Kotlin 101 for Java DevelopersKotlin 101 for Java Developers
Kotlin 101 for Java DevelopersChristoph Pickl
 

Was ist angesagt? (20)

JavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with JavassistJavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with Javassist
 
Google Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG NantesGoogle Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG Nantes
 
Jdk 7 4-forkjoin
Jdk 7 4-forkjoinJdk 7 4-forkjoin
Jdk 7 4-forkjoin
 
Migrating to JUnit 5
Migrating to JUnit 5Migrating to JUnit 5
Migrating to JUnit 5
 
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
 
Mastering Java ByteCode
Mastering Java ByteCodeMastering Java ByteCode
Mastering Java ByteCode
 
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
 
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
 
Sailing with Java 8 Streams
Sailing with Java 8 StreamsSailing with Java 8 Streams
Sailing with Java 8 Streams
 
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
 
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
 
Use of Apache Commons and Utilities
Use of Apache Commons and UtilitiesUse of Apache Commons and Utilities
Use of Apache Commons and Utilities
 
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.
 
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...
 
Java 7 LavaJUG
Java 7 LavaJUGJava 7 LavaJUG
Java 7 LavaJUG
 
모던자바의 역습
모던자바의 역습모던자바의 역습
모던자바의 역습
 
Jdk(java) 7 - 6 기타기능
Jdk(java) 7 - 6 기타기능Jdk(java) 7 - 6 기타기능
Jdk(java) 7 - 6 기타기능
 
Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS Programmers
 
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
 
Kotlin 101 for Java Developers
Kotlin 101 for Java DevelopersKotlin 101 for Java Developers
Kotlin 101 for Java Developers
 

Ähnlich wie Java Bytecode for Discriminating Developers - JavaZone 2011

Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012Anton 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 worldtcurdt
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software DevelopmentZeeshan MIrza
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesCharles Nutter
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesGanesh Samarthyam
 
Java Basics
Java BasicsJava Basics
Java BasicsSunil 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 JavaCharles 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 systemGuardSquare
 
Advanced debugging  techniques in different environments
Advanced debugging  techniques in different environmentsAdvanced debugging  techniques in different environments
Advanced debugging  techniques in different environmentsAndrii Soldatenko
 
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 LaneAndres 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 machineChun-Yu Wang
 
2015-10-07 PPDC HTTP Adapters
2015-10-07 PPDC HTTP Adapters2015-10-07 PPDC HTTP Adapters
2015-10-07 PPDC HTTP Adaptersawwaiid
 

Ähnlich wie 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
 
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
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
 
Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for Dummies
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 
Java Basics
Java BasicsJava Basics
Java Basics
 
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
 
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
 
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
 
Advanced debugging  techniques in different environments
Advanced debugging  techniques in different environmentsAdvanced debugging  techniques in different environments
Advanced debugging  techniques in different environments
 
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
 
Play!ng with scala
Play!ng with scalaPlay!ng with scala
Play!ng with scala
 
Presentation to java
Presentation  to  javaPresentation  to  java
Presentation to java
 
Introduction to-java
Introduction to-javaIntroduction to-java
Introduction to-java
 
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
 
Hello java
Hello java   Hello java
Hello java
 
Hello Java-First Level
Hello Java-First LevelHello Java-First Level
Hello Java-First Level
 
Hello java
Hello java  Hello java
Hello java
 
2015-10-07 PPDC HTTP Adapters
2015-10-07 PPDC HTTP Adapters2015-10-07 PPDC HTTP Adapters
2015-10-07 PPDC HTTP Adapters
 

Mehr von Anton Arhipov

JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfJavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfAnton 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 TeamCityAnton Arhipov
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCityAnton 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 hourAnton 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 hourAnton 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 DSLAnton Arhipov
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCityAnton 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 TestContainersAnton Arhipov
 
GeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainersGeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainersAnton 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 reloadingAnton 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 hassleAnton 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 reloadingAnton 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 reloadingAnton Arhipov
 
JUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentationJUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentationAnton 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 reloadingAnton 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 101Anton 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 profilerAnton Arhipov
 

Mehr von 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
 
Idiomatic kotlin
Idiomatic kotlinIdiomatic kotlin
Idiomatic kotlin
 
TechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервьюTechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервью
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
 
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
 
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
 
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
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
 
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
 
GeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainersGeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainers
 
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
 
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
 
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
 
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
 
JUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentationJUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentation
 
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
 
Devclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервьюDevclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервью
 
Joker 2016 - Bytecode 101
Joker 2016 - Bytecode 101Joker 2016 - Bytecode 101
Joker 2016 - Bytecode 101
 
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
 

Kürzlich hochgeladen

Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 

Kürzlich hochgeladen (20)

Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 

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;
  • 63.
  • 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
  • 114.
  • 115.
  • 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.
  • 162. http://arhipov.blogspot.com ant.arhipov@gmail.com @antonarhipov @javarebel