IT 森林互联网科技程序员

2-JVM-字节码

2018-01-28  本文已影响6人  宠辱不惊的咸鱼

概述

jvm-001.jpg

文件结构

ClassFile {
    u4             magic; # 0xCAFEBABE
    u2             minor_version;
    u2             major_version;
    u2             constant_pool_count;
    cp_info        contant_pool[constant_pool_count – 1]; # 之所以-1,是因为#0被做他用了
    u2             access_flags;
    u2             this_class;
    u2             super_class;
    u2             interfaces_count;
    u2             interfaces[interfaces_count];
    u2             fields_count;
    field_info     fields[fields_count];
    u2             methods_count;
    method_info    methods[methods_count];
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}
magic, minor_version, major_version
    specifies information about the version of the class,  and the version of the JDK this class was compiled for
constant_pool
    similar to a symbol table although it contains more data this is described in more detail below
access_flags
    provides the list of modifiers for this class
this_class
    index into the constant_pool providing the fully qualified name of this class i.e. org/jamesdbloom/foo/Bar
super_class
    index into the constant_pool providing a symbolic reference to the super class i.e. java/lang/Object
interfaces
    array of indexes into the constant_pool providing a symbolic references to all interfaces that have been implemented
fields
    array of indexes into the constant_pool giving a complete description of each field
methods
    array of indexes into the constant_pool giving a complete description of each method signature, if the method is not abstract or native then the bytecode is also present
attributes
    array of different value that provide additional information about the class including any annotations with RetentionPolicy.CLASS or RetentionPolicy.RUNTIME

结构详解

image.png
image.png

访问标识

名称 描述
ACC_PUBLIC 0x0001 public
ACC_FINAL 0x0010 final,不可被继承
ACC_SUPER 0x0020 兼容早期编译器,新编译器均设该标记,invokespecial指令会对子类方法做特定处理
ACC_INTERFACE 0x0200 接口,需同时设置ABSTRACT
ACC_ABSTRACT 0x0400 抽象类
ACC_SYNTHETIC 0x1000 synthetic,编译器产生
ACC_ANNOTATION 0x2000 注解,需同时INTERFACE和ABSTRACT
ACC_ENUM 0x4000 枚举

字段表

名称 类型 描述
access_flags u2 见属性访问标识
name_index u2 名称索引
descriptor_index u2 描述符索引
attributes_count u2 属性个数
attributes[attributes_count] attribute_info 属性集合
名称 描述
ACC_PUBLIC 0x0001 public
ACC_PRIVATE 0x0002 private
ACC_PROTECTED 0x0004 protected
ACC_STATIC 0x0008 static
ACC_FIANL 0x0010 final
ACC_VOLATILE 0x0040 volatile,直接读写内存,不可被缓存;与FINAL互斥
ACC_TRANSIENT 0x0080 transient
ACC_SYNTHETIC 0x1000 synthetic
ACC_ENUM 0x4000 enum

方法表

名称 类型 描述
access_flags u2 见方法访问标识
name_index u2 名称索引
descriptor_index u2 描述符索引
attributes_count u2 属性个数
attributes[attributes_count] attribute_info 属性集合
名称 描述
ACC_PUBLIC 0x0001 public
ACC_PRIVATE 0x0002 private
ACC_PROTECTED 0x0004 protected
ACC_STATIC 0x0008 static
ACC_FIANL 0x0010 final
ACC_SYNCHRONIZED 0x0040 synchronized
ACC_BRIDGE 0x0080 桥接方法,泛型时出现过
ACC_VARARGS 0x0080 含不定参数
ACC_NATIVE 0x0080 native
ACC_ABSTRACT 0x0080 abstract
ACC_STRICTFP 0x1000 strictfp
ACC_SYNTHETIC 0x4000 synthetic,编译器产生,如<init>,<clinit>

字节码命令

# 变量操作
aload_n # 本地变量数组->操作数栈 a代表对象引用,n代表本地变量数组索引
iload # int
lload # long
fload # float
dload # double
ldc # 运行时常量池常量->操作数栈
getstatic # 运行时常量池静态变量->操作数栈

# 方法
invokedynamic
invokeinterface
invokespecial # 实例初始化,私有方法,父类方法
invokestatic
invokevirtual # 普通实例方法

# return语句
return:void
ireturn:int
lreturn:long
freturn:float
dreturn:double
areturn:object reference

# java源码
Object foo = new Object();
# 字节码
 0: new #2           // Class java/lang/Object
 1: dup
 2: invokespecial #3 // Method java/lang/Object:"<init>"( ) V
 
 0 # 堆中开了块内存,返回对象引用至操作数栈
 1 # 复制栈顶对象引用,塞入栈顶
 3 # 对象初始化,消耗栈顶对象引用(作为参数)
 # 留下的那个栈顶对象引用,就是初始化好的对象引用

字节码文件例子

package some;
public class SimpleClass {
    private int m;
    public int inc() {
        return m + 1;
    }
}
cafe babe 0000 0034 0016 0700 0201 0010
736f 6d65 2f53 696d 706c 6543 6c61 7373
0700 0401 0010 6a61 7661 2f6c 616e 672f
4f62 6a65 6374 0100 016d 0100 0149 0100
063c 696e 6974 3e01 0003 2829 5601 0004
436f 6465 0a00 0300 0b0c 0007 0008 0100
0f4c 696e 654e 756d 6265 7254 6162 6c65
0100 124c 6f63 616c 5661 7269 6162 6c65
5461 626c 6501 0004 7468 6973 0100 124c
736f 6d65 2f53 696d 706c 6543 6c61 7373
3b01 0003 696e 6301 0003 2829 4909 0001
0013 0c00 0500 0601 000a 536f 7572 6365
4669 6c65 0100 1053 696d 706c 6543 6c61
7373 2e6a 6176 6100 2100 0100 0300 0000
0100 0200 0500 0600 0000 0200 0100 0700
0800 0100 0900 0000 2f00 0100 0100 0000
052a b700 0ab1 0000 0002 000c 0000 0006
0001 0000 0003 000d 0000 000c 0001 0000
0005 000e 000f 0000 0001 0010 0011 0001
0009 0000 0031 0002 0001 0000 0007 2ab4
0012 0460 ac00 0000 0200 0c00 0000 0600
0100 0000 0700 0d00 0000 0c00 0100 0000
0700 0e00 0f00 0000 0100 1400 0000 0200
15

javap -v -p -s -sysinfo -constant some/SimpleClass.class

package some;
public class SimpleClass {
    public int a = 1;//这个1并不会出现在常量池中,而a以Filedref出现
    final int b = 3;//这个3会以Integer表的身份出现在常量池中,b也以Fieldref出现
    public void sayHello() {
                final int c = 10;//只有c以Utf8出现,10并没有出现(这个final待究)
        System.out.println(c);
        System.out.println("Hello");
    }
}
上一篇下一篇

猜你喜欢

热点阅读