1. Class格式及字节码介绍Class文件的格式如下 [6]: struct Class_File_Format { u4 magic_number; // HEX:CAFEBABE u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count - 1]; 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]; } 详细说明请参看:Class格式 那么,字节码是如何执行的? int i = 1; int j = 3; int sum = i + j; System.out.println(sum); 通过javac编译,并javap -v反编译以上代码: Code: Stack=2, Locals=4, Args_size=1 0: iconst_1 1: istore_1 2: iconst_3 3: istore_2 4: iload_1 5: iload_2 6: iadd 7: istore_3 8: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 11: iload_3 12: invokevirtual #3; //Method java/io/PrintStream.println:(I)V 15: return LineNumberTable: line 4: 0 line 5: 2 line 6: 4 line 7: 8 line 8: 15 直接打印出class文件的相关内容为: 04 3c 06 3d 1b 1c 60 3e b2 00 02 1d b6 00 03 b1 对以上class内容反编译 [5]如下: 04 iconst_1 3c istore_1 06 iconst_3 3d istore_2 1b iload_1 1c iload_2 60 iadd 3e istore_3 b2 getstatic [ constant pool index = index1 << 8 + index2, here is "2" ] 00 index1 02 index2 1d iload_3 b6 invokevirtual [ constant pool index = index1 << 8 + index2, here is "3" ] 00 index1 03 index2 b1 return JVM中执行bytecode是在栈上执行的,以上字节码是这样运行的: 2. 字节码操纵框架
3. 反编译软件
参考资料[1]. http://www.open-open.com/54.htm |
|||||||||||||||||||||