君子生非异也

导航

 

开发工具:IEDA、JDK1.8、WinHex

一、字节码文件结构

.Class 字节码中有两种数据类型:

  • 字节数据直接量:这是基本的数据类型。共细分为 u1 、 uZ 、 u4 、 u8 四种,分别代表连续的 1 个字节、 2 个字节、 4 个字节、 8 个字节组成的整体数据。
  • 表(数组):表是山多个基本数据或其他表,按照既定顺序组成的大的数据集合。表是有结构的,它的结构体现在:组成表的成分所在的位置和顺序都是已经严格定义好的。

Class字节码文件的整体结构

 

 

 

 

二、Demo分析

源代码:该代码贯穿整个字节码学习的章节

package com.jalja.java.bytecode;

/**
 * @Auther: XL
 * @Date: 2020/1/4 12:58
 * @Description:
 */
public class BytecodeTest {
    private int num=1;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

javap -verbose 分析一个字节码文件,会输出字节码文件的魔数、版本号、常量池、类信息、类的构造方法、类中方法的信息、类变量与成员变量等信息

F:\workspace\IDEA\study\jalja-base-utils\target\test-classes>javap -verbose com.jalja.java.bytecode.BytecodeTest
Classfile /F:/workspace/IDEA/study/jalja-base-utils/target/test-classes/com/jalja/java/bytecode/BytecodeTest.class
  Last modified 2020-1-4; size 514 bytes
  MD5 checksum b661c2792027e7c9169a0266523412c1
  Compiled from "BytecodeTest.java"
public class com.jalja.java.bytecode.BytecodeTest
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #4.#20         // java/lang/Object."<init>":()V
   #2 = Fieldref           #3.#21         // com/jalja/java/bytecode/BytecodeTest.num:I
   #3 = Class              #22            // com/jalja/java/bytecode/BytecodeTest
   #4 = Class              #23            // java/lang/Object
   #5 = Utf8               num
   #6 = Utf8               I
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               LocalVariableTable
  #12 = Utf8               this
  #13 = Utf8               Lcom/jalja/java/bytecode/BytecodeTest;
  #14 = Utf8               getNum
  #15 = Utf8               ()I
  #16 = Utf8               setNum
  #17 = Utf8               (I)V
  #18 = Utf8               SourceFile
  #19 = Utf8               BytecodeTest.java
  #20 = NameAndType        #7:#8          // "<init>":()V
  #21 = NameAndType        #5:#6          // num:I
  #22 = Utf8               com/jalja/java/bytecode/BytecodeTest
  #23 = Utf8               java/lang/Object
{
  public com.jalja.java.bytecode.BytecodeTest();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: aload_0
         5: iconst_1
         6: putfield      #2                  // Field num:I
         9: return
      LineNumberTable:
        line 8: 0
        line 9: 4
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      10     0  this   Lcom/jalja/java/bytecode/BytecodeTest;

  public int getNum();
    descriptor: ()I
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: getfield      #2                  // Field num:I
         4: ireturn
      LineNumberTable:
        line 12: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/jalja/java/bytecode/BytecodeTest;

  public void setNum(int);
    descriptor: (I)V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         0: aload_0
         1: iload_1
         2: putfield      #2                  // Field num:I
         5: return
      LineNumberTable:
        line 16: 0
        line 17: 5
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       6     0  this   Lcom/jalja/java/bytecode/BytecodeTest;
            0       6     1   num   I
}
SourceFile: "BytecodeTest.java"
View Code

WinHex:打开class文件

二、魔数(Magic Number)

1、魔数 :所有的class字节码文件的前四个字节都是魔数,魔数是固定值0XCAFEBABE,JVM加载class时会验证该数据是否符合规范。

二、版本号(Version)

2、版本号: 

 魔数之后的四个字节是版本信息,前两个字节(00 00)是次版本号(minor version: 0),后两个字节(00 34)是主版本号(major version: 52),34是十六进制转换十进制为52;52对应的就是JDK1.8;所以该class文件的版本号=1.8.0;用过java -version 可以验证。由于JVM是向下兼容,所以如何你的字节码文件版本号,小于等于当前JVM的版本,都是可以在当前JVM中运行的。

 

下一节:JAVA字节码文件之常量池

posted on 2020-01-05 16:04  徐知语的笔记  阅读(501)  评论(0编辑  收藏  举报