0
Mastering Java BytecodeAnton Arhipov | ZeroTurnaround
whoami• Anton Arhipov• Java Dev / Product Lead• ZeroTurnaround, JRebel• @antonarhipov @javarebel
Today – Java Core• Mastering Java Bytecode• The Future of Java on Multi-  Core, Lambdas, Spliterators and  Methods• OpenJD...
Why Bytecode?•   Know your platform!•   Create your own compiler?•   Programming model (AOP, ORM)•   Awesome tools (JRebel...
THE INTRO
1+2
1+212+
1+212+
1+212+   PUSH 1               1
1+212+   PUSH 1      PUSH 2               2               1
1+212+   PUSH 1      PUSH 2               3      ADD
1+212+   ICONST_1      ICONST_2                 3      IADD
?=1+2
TAXONOMY
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• constant values (ldc, iconst_1)
TYPE OPERATION• <TYPE> ::= b, s, c, i, l, f, d, a• constant values (ldc, iconst_1)•   Local variables and stack interactio...
Bytecode Taxonomy
Bytecode Taxonomy  StackManipulation
Bytecode Taxonomy  Stack         FlowManipulation   Control
Bytecode Taxonomy  Stack         FlowManipulation   Control               Object               Model
Bytecode Taxonomy  Stack         FlowManipulation   Control               Object Arithmetics               Model
Bytecode Taxonomy  Stack                   FlowManipulation             Control          monitorenter          monitorexit...
TOOLING
javap• Java class file disassembler• Used with no options shows class structure  only   – Methods, superclass, interfaces,...
HELLO WORLD!
C:workgeeconclasses>javap   Hello -c
C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();  C...
C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();  C...
C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();  C...
C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();  C...
C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();  C...
C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();  C...
C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();  C...
C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();  C...
C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();  C...
C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();  C...
What’s #1,#2, etc ?C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object...
C:workgeeconclasses>javap   Hello -c -verbose
C:workgeeconclasses>javap   Hello -c -verbose
C:workgeeconclasses>javap Hello -c -verboseCompiled from "Hello.java“public class Hello extends java.lang.Object SourceFil...
C:workgeeconclasses>javap Hello -c -verboseCompiled from "Hello.java“public class Hello extends java.lang.Object SourceFil...
C:workgeeconclasses>javap Hello -c -verbose…public Hello(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespeci...
C:workgeeconclasses>javap Hello -c -verbose…public Hello(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespeci...
C:workgeeconclasses>javap Hello -c -verbose…public Hello(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespeci...
C:workgeeconclasses>javap Hello -c -verbose…public static void main(java.lang.String[]); Code: Stack=2, Locals=1, Args_siz...
MODEL OFCOMPUTATION
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 met...
Stack Machine•   JVM is a stack-based machine•   Each thread has a stack•   Stack stores frames•   Frame is created on met...
The FrameLocal variables0 1 2      … NOperand stack                  #1                       Constant                    ...
public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield      #2; //Field name:Ljava...
0          1         2       3         4    aload_0      getfield    00      02     areturnpublic java.lang.String getName...
0          1         2       3         4       2A          B4        00      02        B0public java.lang.String getName()...
public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield      #2; //Field name:Ljava...
STACK CRUNCHING
dup       Apop       Bswapdup_x1dup2_x1
dup       Apop       Aswap      Bdup_x1dup2_x1
dup       Apop       Bswapdup_x1dup2_x1
dup       Bpop       Aswapdup_x1dup2_x1
dup       Bpop       Aswap      Bdup_x1dup2_x1
dup       Bpop       Aswap      Bdup_x1    Bdup2_x1   A
dup2_x2 How do youswap doubles?
dup2_x2
dup2_x2dconst_0                     0.0
dup2_x2dconst_0dconst_1             1.0                     0.0
dup2_x2dconst_0dconst_1             1.0swap                     0.0
dup2_x2dconst_0dconst_1                  1.0swap       not allowed!                          0.0
dup2_x2dconst_0dconst_1             1.0swap2                     0.0
dup2_x2dconst_0dconst_1              1.0            doesn’tswap2        exist                      0.0
dup2_x2dconst_0dconst_1             1.0dup2_x2                     0.0                     1.0
dup2_x2dconst_0dconst_1             0.0dup2_x2pop2                 1.0
dup2_x2dconst_0dconst_1             0.0dup2_x2pop2                 1.0profit! 
LOCAL VARIABLES
Local Variables
Local Variablespublic int calculate(int); Code: Stack=2, Locals=2, Args_size=2  … LocalVariableTable: Start Length Slot Na...
Local Variablespublic int calculate(int); Code: Stack=2, Locals=2, Args_size=2  … LocalVariableTable: Start Length Slot Na...
Local Variablespublic int calculate(int); Code: Stack=2, Locals=2, Args_size=2  … LocalVariableTable: Start Length Slot Na...
Local Variables                                         The tablepublic int calculate(int); Code:                         ...
Local Variablespublic int calculate(int); Code: Stack=2, Locals=2, Args_size=2              Sized explicitly  … LocalVaria...
Local Variables                         Stackvar        value                      depth     value                        ...
Local Variables                         Stackvar        value                      depth     value                        ...
Local Variables                         Stackvar        value                      depth     value                        ...
Local Variables                         Stackvar        value                      depth     value                        ...
Local Variables                         Stackvar        value                      depth     value                        ...
Local Variables                         Stackvar        value                      depth     value                        ...
load  LocalVariables           Stack  Table            store
OBJECTS
Object Initializationnew 0xBB         <init>  Instance initialization method                         <clinit>             ...
Object Initialization: static {}                static {};                 Code:                  0: iconst_1             ...
Object Initialization: static {}                                 <clinit>                static {};                 Code: ...
Object Initialization: new
Object Initialization: new
Object Initialization: newpublic Initializer(); Code:
Object Initialization: newpublic Initializer(); Code: 0: aload_0
Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V
Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":(...
Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":(...
Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":(...
Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":(...
Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":(...
Object Initialization: {}
Object Initialization: {}
Object Initialization: {}             public Initializer(int);              Code:              0:aload_0              1:in...
There’s no initializer
METHODINVOCATION
invokeXXX•   invokestatic•   invokespecial•   invokevirtual•   invokeinterface•   invokedynamic
invokestatic•   invokestatic•   invokespecial•   invokevirtual     Integer.valueOf(“42”)•   invokeinterface•   invokedynamic
invokespecial•   invokestatic            <init>•   invokespecial•   invokevirtual     private void foo();•   invokeinterfa...
invokevirtual                        class A•   invokestatic         A/method1                         A/method2•   invoke...
invokevirtual                           class A•   invokestatic            A/method1                            A/method2•...
invokevirtual                             class A•   invokestatic               A/method1                               A/...
invokeinterface                             class A•   invokestatic               A/method1                               ...
invokeinterface                             class A•   invokestatic               A/method1                               ...
invokeinterface                             class A•   invokestatic               A/method1                               ...
invokeinterface                                             class A   •   invokestatic                             A/metho...
Method Invocation
Method Invocationobj.method(param1, param2);
Method Invocationobj.method(param1, param2);
Method Invocationobj.method(param1, param2);      push obj      push param1      push param2      call method
Method Invocationobj.method(param1, param2);                              obj      push obj      push param1      push par...
Method Invocationobj.method(param1, param2);                              param1      push obj                            ...
Method Invocationobj.method(param1, param2);                              param2      push obj                param1      ...
Method Invocationobj.method(param1, param2);                              obj?      push obj      push param1      push pa...
Method Invocationthis.add(1, 2);0:   aload_01:   iconst_12:   iconst_23:   invokevirtual #2; //Method add:(II)I
INNER CLASSES
Inner Classes
Inner Classes
Inner Classesclass Car$Engine extends j.l.Object{final Car this$0;Car$Engine(Car);public void start(); Code: 0: aload_0 1:...
Inner Classes                                      public class Car extends j.l.Object{                                   ...
Inner Classes                                      public class Car extends j.l.Object{                                   ...
“HOW DO THEY DO THAT?”
object Singleton {  def test={}}
object Singleton {        def test={}      } $> scalac Singleton.scalaSingleton.class       Singleton$.class
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
public final class Singleton extends java.lang.Object {public static final void test();  Code:  0: getstatic      #11; //F...
object Singleton {  def test={}}
public class Singleton {                       public void test(){                         Singleton$.MODULE$.test();     ...
public class Singleton {                       public void test(){                         Singleton$.MODULE$.test();     ...
class Groovy { }
class Groovy { }$> groovyc Groovy.groovy$> javap –c –p Groovy
class Groovy { }$> groovyc Groovy.groovy$> javap –c –p Groovypublic class Test extends java.lang.Object implements groovy....
OBJECTWEB ASM
SLIDESGOTO IDESLIDESIDE: DEMO
https://github.com/antonarhipov
@antonarhipovanton@zeroturnaround.com
Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012
Mastering Java Bytecode - JAX.de 2012
Upcoming SlideShare
Loading in...5
×

Mastering Java Bytecode - JAX.de 2012

6,709

Published on

Published in: Technology, Education
0 Comments
17 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,709
On Slideshare
0
From Embeds
0
Number of Embeds
35
Actions
Shares
0
Downloads
160
Comments
0
Likes
17
Embeds 0
No embeds

No notes for slide

Transcript of "Mastering Java Bytecode - JAX.de 2012"

  1. 1. Mastering Java BytecodeAnton Arhipov | ZeroTurnaround
  2. 2. whoami• Anton Arhipov• Java Dev / Product Lead• ZeroTurnaround, JRebel• @antonarhipov @javarebel
  3. 3. Today – Java Core• Mastering Java Bytecode• The Future of Java on Multi- Core, Lambdas, Spliterators and Methods• OpenJDK JVM Internals• Invokedynamic Deep Dive
  4. 4. Why Bytecode?• Know your platform!• Create your own compiler?• Programming model (AOP, ORM)• Awesome tools (JRebel )• … just bored?
  5. 5. THE INTRO
  6. 6. 1+2
  7. 7. 1+212+
  8. 8. 1+212+
  9. 9. 1+212+ PUSH 1 1
  10. 10. 1+212+ PUSH 1 PUSH 2 2 1
  11. 11. 1+212+ PUSH 1 PUSH 2 3 ADD
  12. 12. 1+212+ ICONST_1 ICONST_2 3 IADD
  13. 13. ?=1+2
  14. 14. TAXONOMY
  15. 15. Bytecode• One-byte instructions• 256 possible opcodes• 200+ in use
  16. 16. TYPE OPERATION
  17. 17. TYPE OPERATION• <TYPE> ::= b, s, c, i, l, f, d, a
  18. 18. TYPE OPERATION• <TYPE> ::= b, s, c, i, l, f, d, a• constant values (ldc, iconst_1)
  19. 19. TYPE OPERATION• <TYPE> ::= b, s, c, i, l, f, d, a• 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)
  20. 20. Bytecode Taxonomy
  21. 21. Bytecode Taxonomy StackManipulation
  22. 22. Bytecode Taxonomy Stack FlowManipulation Control
  23. 23. Bytecode Taxonomy Stack FlowManipulation Control Object Model
  24. 24. Bytecode Taxonomy Stack FlowManipulation Control Object Arithmetics Model
  25. 25. Bytecode Taxonomy Stack FlowManipulation Control monitorenter monitorexit Object Arithmetics Model
  26. 26. TOOLING
  27. 27. javap• Java class file disassembler• Used with no options shows class structure only – Methods, superclass, interfaces, etc• -c shows the bytecode• -private shows all methods and members• -s prints internal signatures• -l prints line numbers and local variable tables
  28. 28. HELLO WORLD!
  29. 29. C:workgeeconclasses>javap Hello -c
  30. 30. C:workgeeconclasses>javap Hello -cCompiled 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
  31. 31. C:workgeeconclasses>javap Hello -cCompiled 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
  32. 32. C:workgeeconclasses>javap Hello -cCompiled 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
  33. 33. C:workgeeconclasses>javap Hello -cCompiled 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
  34. 34. C:workgeeconclasses>javap Hello -cCompiled 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
  35. 35. C:workgeeconclasses>javap Hello -cCompiled 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: returnpublic 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
  36. 36. C:workgeeconclasses>javap Hello -cCompiled 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 fieldpublic 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
  37. 37. C:workgeeconclasses>javap Hello -cCompiled 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: returnpublic 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
  38. 38. C:workgeeconclasses>javap Hello -cCompiled 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: returnpublic 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
  39. 39. C:workgeeconclasses>javap Hello -cCompiled 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: returnpublic 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. 40. What’s #1,#2, etc ?C:workgeeconclasses>javap Hello -cCompiled 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: returnpublic 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
  41. 41. C:workgeeconclasses>javap Hello -c -verbose
  42. 42. C:workgeeconclasses>javap Hello -c -verbose
  43. 43. C:workgeeconclasses>javap Hello -c -verboseCompiled 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>":()Vconst #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;)Vconst #5 = class #26; // Helloconst #6 = class #27; // java/lang/Objectconst #7 = Asciz <init>;const #8 = Asciz ()V;
  44. 44. C:workgeeconclasses>javap Hello -c -verboseCompiled 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>":()Vconst #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;)Vconst #5 = class #26; // Helloconst #6 = class #27; // java/lang/Objectconst #7 = Asciz <init>;const #8 = Asciz ()V;
  45. 45. 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;
  46. 46. 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;
  47. 47. 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;
  48. 48. C:workgeeconclasses>javap Hello -c -verbose…public static void main(java.lang.String[]); Code: Stack=2, Locals=1, Args_size=1 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 8: return LineNumberTable: line 4: 0 line 5: 8 LocalVariableTable: Start Length Slot Name Signature 0 9 0 args [Ljava/lang/String;
  49. 49. MODEL OFCOMPUTATION
  50. 50. Stack Machine
  51. 51. Stack Machine• JVM is a stack-based machine
  52. 52. Stack Machine• JVM is a stack-based machine• Each thread has a stack
  53. 53. Stack Machine• JVM is a stack-based machine• Each thread has a stack• Stack stores frames
  54. 54. Stack Machine• JVM is a stack-based machine• Each thread has a stack• Stack stores frames• Frame is created on method invocation
  55. 55. 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
  56. 56. The FrameLocal variables0 1 2 … NOperand stack #1 Constant Pool
  57. 57. public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  58. 58. 0 1 2 3 4 aload_0 getfield 00 02 areturnpublic java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  59. 59. 0 1 2 3 4 2A B4 00 02 B0public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  60. 60. public java.lang.String getName(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field name:Ljava/lang/String; 4: areturnLocalVariableTable: Start Length Slot Name Signature 0 5 0 this LGet;
  61. 61. STACK CRUNCHING
  62. 62. dup Apop Bswapdup_x1dup2_x1
  63. 63. dup Apop Aswap Bdup_x1dup2_x1
  64. 64. dup Apop Bswapdup_x1dup2_x1
  65. 65. dup Bpop Aswapdup_x1dup2_x1
  66. 66. dup Bpop Aswap Bdup_x1dup2_x1
  67. 67. dup Bpop Aswap Bdup_x1 Bdup2_x1 A
  68. 68. dup2_x2 How do youswap doubles?
  69. 69. dup2_x2
  70. 70. dup2_x2dconst_0 0.0
  71. 71. dup2_x2dconst_0dconst_1 1.0 0.0
  72. 72. dup2_x2dconst_0dconst_1 1.0swap 0.0
  73. 73. dup2_x2dconst_0dconst_1 1.0swap not allowed! 0.0
  74. 74. dup2_x2dconst_0dconst_1 1.0swap2 0.0
  75. 75. dup2_x2dconst_0dconst_1 1.0 doesn’tswap2 exist 0.0
  76. 76. dup2_x2dconst_0dconst_1 1.0dup2_x2 0.0 1.0
  77. 77. dup2_x2dconst_0dconst_1 0.0dup2_x2pop2 1.0
  78. 78. dup2_x2dconst_0dconst_1 0.0dup2_x2pop2 1.0profit! 
  79. 79. LOCAL VARIABLES
  80. 80. Local Variables
  81. 81. Local Variablespublic 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
  82. 82. Local Variablespublic 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
  83. 83. Local Variablespublic 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
  84. 84. Local Variables The tablepublic int calculate(int); Code: maps Stack=2, Locals=2, Args_size=2 numbers to … names LocalVariableTable: Start Length Slot Name Signature 0 5 0 this LLocalVariables; 0 5 1 value I
  85. 85. Local Variablespublic 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
  86. 86. Local Variables Stackvar value depth value ldc "Hello"0 0 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  87. 87. Local Variables Stackvar value depth value ldc "Hello"0 0 "Hello" astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  88. 88. Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  89. 89. Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 1 astore_01 iconst_1 1 astore_12 aload_0 23 34 4
  90. 90. Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 astore_01 1 iconst_1 1 astore_12 aload_0 23 34 4
  91. 91. Local Variables Stackvar value depth value ldc "Hello"0 "Hello" 0 "Hello" astore_01 1 iconst_1 1 astore_12 aload_0 23 34 4
  92. 92. load LocalVariables Stack Table store
  93. 93. OBJECTS
  94. 94. Object Initializationnew 0xBB <init> Instance initialization method <clinit> Class and interface initialization method
  95. 95. Object Initialization: static {} static {}; Code: 0: iconst_1 1: putstatic #2; //Field a:I 4: iconst_2 5: putstatic #3; //Field b:I 8: return
  96. 96. Object Initialization: static {} <clinit> static {}; Code: 0: iconst_1 1: putstatic #2; //Field a:I 4: iconst_2 5: putstatic #3; //Field b:I 8: return
  97. 97. Object Initialization: new
  98. 98. Object Initialization: new
  99. 99. Object Initialization: newpublic Initializer(); Code:
  100. 100. Object Initialization: newpublic Initializer(); Code: 0: aload_0
  101. 101. Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V
  102. 102. Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0
  103. 103. Object Initialization: newpublic 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
  104. 104. Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class java/lang/Object 8: dup 9: invokespecial #1; //Method java/lang/Object."<init>":()V12: putfield #3; //Field o:Ljava/lang/Object;
  105. 105. Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class java/lang/Object 8: dup 9: invokespecial #1; //Method java/lang/Object."<init>":()V12: putfield #3; //Field o:Ljava/lang/Object;15: return
  106. 106. Object Initialization: newpublic Initializer(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class java/lang/Object 8: dup 9: invokespecial #1; //Method java/lang/Object."<init>":()V12: putfield #3; //Field o:Ljava/lang/Object;15: return
  107. 107. Object Initialization: {}
  108. 108. Object Initialization: {}
  109. 109. Object Initialization: {} public Initializer(int); Code: 0:aload_0 1:invokespecial #1; // ..<init> 4:aload_0 5:iconst_1 6:putfield #2; //Field a:I 9:aload_0 10:iconst_2 11: putfield #3; //Field c:I 14:aload_0 15:iload_1 16:putfield #4; //Field b:I 19:return
  110. 110. There’s no initializer
  111. 111. METHODINVOCATION
  112. 112. invokeXXX• invokestatic• invokespecial• invokevirtual• invokeinterface• invokedynamic
  113. 113. invokestatic• invokestatic• invokespecial• invokevirtual Integer.valueOf(“42”)• invokeinterface• invokedynamic
  114. 114. invokespecial• invokestatic <init>• invokespecial• invokevirtual private void foo();• invokeinterface• invokedynamic super.method();
  115. 115. invokevirtual class A• invokestatic A/method1 A/method2• invokespecial• invokevirtual• invokeinterface• invokedynamic
  116. 116. invokevirtual class A• invokestatic A/method1 A/method2• invokespecial class B• invokevirtual• invokeinterface• invokedynamic
  117. 117. invokevirtual class A• invokestatic A/method1 A/method2• invokespecial class B• invokevirtual A/method1 B/method2• invokeinterface B/method3• invokedynamic
  118. 118. invokeinterface class A• invokestatic A/method1 A/method2• invokespecial class B impl X• invokevirtual A/method1 B/method2• invokeinterface B/method3 X/methodX• invokedynamic
  119. 119. invokeinterface class A• invokestatic A/method1 A/method2• invokespecial class B impl X• invokevirtual A/method1• invokeinterface B/method2 B/method3 D impl X X/methodX D/method1• invokedynamic X/methodX
  120. 120. invokeinterface class A• invokestatic A/method1 A/method2• invokespecial class B impl X• invokevirtual A/method1• invokeinterface B/method2 B/method3 D impl X X/methodX D/method1• invokedynamic X/methodX
  121. 121. invokeinterface class A • invokestatic A/method1 A/method2 • invokespecial class B impl X • invokevirtual A/method1 • invokeinterface B/method2 B/method3 D impl X X/methodX D/method1 • invokedynamic X/methodXEfficient Implementation of Java Interfaces: Invokeinterface ConsideredHarmless, Bowen Alpern, Anthony Cocchi, Stephen Fink, David Grove, andDerek Lieber, OOPSLA’01
  122. 122. Method Invocation
  123. 123. Method Invocationobj.method(param1, param2);
  124. 124. Method Invocationobj.method(param1, param2);
  125. 125. Method Invocationobj.method(param1, param2); push obj push param1 push param2 call method
  126. 126. Method Invocationobj.method(param1, param2); obj push obj push param1 push param2 call method
  127. 127. Method Invocationobj.method(param1, param2); param1 push obj obj push param1 push param2 call method
  128. 128. Method Invocationobj.method(param1, param2); param2 push obj param1 push param1 push param2 obj call method
  129. 129. Method Invocationobj.method(param1, param2); obj? push obj push param1 push param2 call method
  130. 130. Method Invocationthis.add(1, 2);0: aload_01: iconst_12: iconst_23: invokevirtual #2; //Method add:(II)I
  131. 131. INNER CLASSES
  132. 132. Inner Classes
  133. 133. Inner Classes
  134. 134. Inner Classesclass Car$Engine extends j.l.Object{final Car this$0;Car$Engine(Car);public void start(); Code: 0: aload_0 1: getfield #1; //Field this$0:LCar; 4: invokestatic #3; // Car.access$000:(LCar;)V 7: return}
  135. 135. Inner Classes public class Car extends j.l.Object{ public Car(); private void move();class Car$Engine extends j.l.Object{final Car this$0; static void access$000(Car); Code:Car$Engine(Car); 0: aload_0 1: invokespecial #1; // move: ()V;public void start(); 4: return Code: } 0: aload_0 1: getfield #1; //Field this$0:LCar; 4: invokestatic #3; // Car.access$000:(LCar;)V 7: return}
  136. 136. Inner Classes public class Car extends j.l.Object{ public Car(); private void move();class Car$Engine extends j.l.Object{final Car this$0; static void access$000(Car); Code:Car$Engine(Car); 0: aload_0 1: invokespecial #1; // move: ()V;public void start(); 4: return Code: } 0: aload_0 1: getfield #1; //Field this$0:LCar; 4: invokestatic #3; // Car.access$000:(LCar;)V 7: return}
  137. 137. “HOW DO THEY DO THAT?”
  138. 138. object Singleton { def test={}}
  139. 139. object Singleton { def test={} } $> scalac Singleton.scalaSingleton.class Singleton$.class
  140. 140. 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}
  141. 141. 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}
  142. 142. 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}
  143. 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}public final class Singleton$ extends java.lang.Object implementsscala.ScalaObject {public static final Singleton$ MODULE$;public static {}; Code: 0: new #9; //class Singleton$ 3: invokespecial #12; //Method "<init>":()V 6: returnpublic void test();private Singleton$();}
  144. 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}public final class Singleton$ extends java.lang.Object implementsscala.ScalaObject {public static final Singleton$ MODULE$;public static {}; Code: 0: new #9; //class Singleton$ 3: invokespecial #12; //Method "<init>":()V 6: returnpublic void test();private Singleton$();}
  145. 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}public final class Singleton$ extends java.lang.Object implementsscala.ScalaObject {public static final Singleton$ MODULE$;public static {}; Code: 0: new #9; //class Singleton$ 3: invokespecial #12; //Method "<init>":()V 6: returnpublic void test();private Singleton$();}
  146. 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 implementsscala.ScalaObject {public static final Singleton$ MODULE$;public static {};public void test();private Singleton$();Code: 0: aload_0 1: invokespecial #17; //Method java/lang/Object."<init>":()V 4: aload_0 5: putstatic #19; //Field MODULE$:LSingleton$; 8: return
  147. 147. object Singleton { def test={}}
  148. 148. public class Singleton { public void test(){ Singleton$.MODULE$.test(); } }object Singleton { def test={}}
  149. 149. 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() { } }
  150. 150. class Groovy { }
  151. 151. class Groovy { }$> groovyc Groovy.groovy$> javap –c –p Groovy
  152. 152. class Groovy { }$> groovyc Groovy.groovy$> javap –c –p Groovypublic class Test extends java.lang.Object implements groovy.lang.GroovyObject{ private static org.codehaus.groovy.reflection.ClassInfo $staticClassInfo; private transient groovy.lang.MetaClass metaClass; public static java.lang.Long __timeStamp; public static java.lang.Long __timeStamp__239_neverHappen1304807931117; private static java.lang.ref.SoftReference $callSiteArray; private static java.lang.Class $class$groovy$lang$MetaClass; private static java.lang.Class $class$Test; private static java.lang.Class $class$java$lang$String; public java.lang.Object this$dist$invoke$2(java.lang.String, java.lang.Object); public void this$dist$set$2(java.lang.String, java.lang.Object); public java.lang.Object this$dist$get$2(java.lang.String); protected groovy.lang.MetaClass $getStaticMetaClass(); public groovy.lang.MetaClass getMetaClass(); public void setMetaClass(groovy.lang.MetaClass); public java.lang.Object invokeMethod(java.lang.String, java.lang.Object); public java.lang.Object getProperty(java.lang.String); public void setProperty(java.lang.String, java.lang.Object);
  153. 153. OBJECTWEB ASM
  154. 154. SLIDESGOTO IDESLIDESIDE: DEMO
  155. 155. https://github.com/antonarhipov
  156. 156. @antonarhipovanton@zeroturnaround.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×