Let’s Talk aboutJava Class FileYongqiang Li
Outlines Brief      Introduction to Java Class File Java Class Format and Layout References
Brief Introduction to Java Class File Has  .class extension Compiled from java source files (.java) Platform-independen...
Java Source File (ClassA.java)                                      javac ClassA.java     Java Class File (ClassA.class)
 Bytecode
 Only   “javac” can generate class file?          JRuby                   ClojureJython             Java Class          G...
Java Class Format and Layout   Class File contains different sections.       Magic Number       Version of Class File F...
Magic Number4 bytes Always “CAFEBABE” ( 咖啡宝贝 )
Version of Class File Format  2  bytes for minor version   2 bytes for major version   Java major version starts from 4...
Constant Pool2  bytes used to store constant count Index of constant starts from 1 not 0.
Tag Byte   Constant Type           Additional Bytes0x01       UTF8-String             2+x0x03       Integer               ...
 Constant    Count 0x0021(hex) = 33 (dec)
 Double           • 0x06: Double Tag           • 0x4000 1893 74bc 6a7f: 2.012
• 0x01: UTF-8 Tag UTF-8   • 0x0006: “String” has 6 char.          • 0x5374 7269 6367: “String”
 More   Strings    Field/Method     Name    Field/Method     Type    Attribute Name
 Class   Reference                      • 0x07: Class Ref Tag                      • 0x0002: Constant Index
Access Flags For Class 2 Bytes
This and Super  Both  are 2 bytes  Direct to Class Reference in Constant   Pool.This Ref: 0x0001Super Ref: 0x0003
Interfaces2  bytes for count of interfaces 2 * interfaces_count bytes
Fields2  bytes for fields count field_info * fields_count
 Attribute   Structure
 Field   Access     2 bytes
•   0x0001: Only 1 Field•   0x0019: public static final•   0x0005: “obj”•   0x0006: Descriptor “Ljava/lang/Object;”•   0x0...
Methods2 bytes for methods count method_info * methods_count                          Method Access Flags
 Method Attributes CodeAttributes
•   Max Stack: 2•   Max Locals: 3•   Args Size: 2•   Code:
 JVM   Opcode    Based on stack     not register    About 200     opcode in JVM 6
 0: push the integer in  slot 0 to stack 1: push the integer in  slot 1 to stack 2: add first 2 integers in  stack and ...
 Exception   Attributes
Who can help me on this? Magic Number Version of Class File Format Constant Pool Access Flags This Class Super Class...
References JVM    6 Specification 《深入浅出 Java 虚拟机》
Let's talk about java class file
Let's talk about java class file
Let's talk about java class file
Let's talk about java class file
Let's talk about java class file
Upcoming SlideShare
Loading in …5
×

Let's talk about java class file

980 views

Published on

Some research on java class file.

Published in: Technology, Education
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
980
On SlideShare
0
From Embeds
0
Number of Embeds
183
Actions
Shares
0
Downloads
0
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Let's talk about java class file

  1. 1. Let’s Talk aboutJava Class FileYongqiang Li
  2. 2. Outlines Brief Introduction to Java Class File Java Class Format and Layout References
  3. 3. Brief Introduction to Java Class File Has .class extension Compiled from java source files (.java) Platform-independent Machine-readable Stores so-called “bytecodes”
  4. 4. Java Source File (ClassA.java) javac ClassA.java Java Class File (ClassA.class)
  5. 5.  Bytecode
  6. 6.  Only “javac” can generate class file? JRuby ClojureJython Java Class Groovy Scala
  7. 7. Java Class Format and Layout Class File contains different sections.  Magic Number  Version of Class File Format  Constant Pool  Access Flags  This Class  Super Class  Interfaces  Fields  Methods  Attributes
  8. 8. Magic Number4 bytes Always “CAFEBABE” ( 咖啡宝贝 )
  9. 9. Version of Class File Format 2 bytes for minor version  2 bytes for major version  Java major version starts from 45 (JDK 1.1)  Java SE 5 is 49, SE 6 is 50 (0x32), SE 7 is 51.
  10. 10. Constant Pool2 bytes used to store constant count Index of constant starts from 1 not 0.
  11. 11. Tag Byte Constant Type Additional Bytes0x01 UTF8-String 2+x0x03 Integer 4 (Big endian)0x04 Float 4 (IEEE 754 floating- point)0x05 Long 8 (Big endian)0x06 Double 8 (IEEE 754 floating- point)0x07 Class Reference 20x08 String Reference 20x09 Field Reference 2+2 (first 2 for class Ref)0x0A Method Reference 2+2 (first 2 for class Ref)0x0B Interface Method Ref. 2+2 (first 2 for class Ref)0x0C Name and Type Ref. 2+2
  12. 12.  Constant Count 0x0021(hex) = 33 (dec)
  13. 13.  Double • 0x06: Double Tag • 0x4000 1893 74bc 6a7f: 2.012
  14. 14. • 0x01: UTF-8 Tag UTF-8 • 0x0006: “String” has 6 char. • 0x5374 7269 6367: “String”
  15. 15.  More Strings  Field/Method Name  Field/Method Type  Attribute Name
  16. 16.  Class Reference • 0x07: Class Ref Tag • 0x0002: Constant Index
  17. 17. Access Flags For Class 2 Bytes
  18. 18. This and Super  Both are 2 bytes  Direct to Class Reference in Constant Pool.This Ref: 0x0001Super Ref: 0x0003
  19. 19. Interfaces2 bytes for count of interfaces 2 * interfaces_count bytes
  20. 20. Fields2 bytes for fields count field_info * fields_count
  21. 21.  Attribute Structure
  22. 22.  Field Access  2 bytes
  23. 23. • 0x0001: Only 1 Field• 0x0019: public static final• 0x0005: “obj”• 0x0006: Descriptor “Ljava/lang/Object;”• 0x0002: 2 Attributes • 1st: 0x0007 (Deprecated) • 2nd: 0x0008 (RuntimeVisibleAnnotations)
  24. 24. Methods2 bytes for methods count method_info * methods_count Method Access Flags
  25. 25.  Method Attributes CodeAttributes
  26. 26. • Max Stack: 2• Max Locals: 3• Args Size: 2• Code:
  27. 27.  JVM Opcode  Based on stack not register  About 200 opcode in JVM 6
  28. 28.  0: push the integer in slot 0 to stack 1: push the integer in slot 1 to stack 2: add first 2 integers in stack and result will be push into stack 3: load the integer from stack top to integer in slot 2 4: push the integer in slot 2 to stack 5: return an integer
  29. 29.  Exception Attributes
  30. 30. Who can help me on this? Magic Number Version of Class File Format Constant Pool Access Flags This Class Super Class Interfaces Fields Methods Attributes
  31. 31. References JVM 6 Specification 《深入浅出 Java 虚拟机》

×