public class TestClass { private int m; public int inc(){ return m+1; } }
nagat@nagat-PC /cygdrive/g/javacode $ java -version java version "1.7.0_40" Java(TM) SE Runtime Environment (build 1.7.0_40-b43) Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode) nagat@nagat-PC /cygdrive/g/javacode $ javap -v TestClass.class Classfile /G:/javacode/TestClass.class Last modified 2013-9-12; size 275 bytes MD5 checksum 0db50c82c3b4534130e9d32e5be7639e Compiled from "TestClass.java" public class TestClass SourceFile: "TestClass.java" minor version: 0 major version: 51 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #4.#15 // java/lang/Object."<init>":()V #2 = Fieldref #3.#16 // TestClass.m:I #3 = Class #17 // TestClass #4 = Class #18 // java/lang/Object #5 = Utf8 m #6 = Utf8 I #7 = Utf8 <init> #8 = Utf8 ()V #9 = Utf8 Code #10 = Utf8 LineNumberTable #11 = Utf8 inc #12 = Utf8 ()I #13 = Utf8 SourceFile #14 = Utf8 TestClass.java #15 = NameAndType #7:#8 // "<init>":()V #16 = NameAndType #5:#6 // m:I #17 = Utf8 TestClass #18 = Utf8 java/lang/Object { public TestClass(); flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 3: 0 public int inc(); flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: getfield #2 // Field m:I 4: iconst_1 5: iadd 6: ireturn LineNumberTable: line 8: 0 }
按照
http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.1
看下图
看到蓝色前,正好有10个CONSTANT_Utf8_info
,蓝色开始往后,求解释,没对上呢。。。。,orz。。。
其实红色结束了,还有2个NameAndType和2个CONSTANT_Utf8_info。。。。我以为constant_pool应该结束了,就是access_flags了,结果就对不上了。。所以要仔细!
贴一下inc方法吧,我发现要是想用文字说明白得打很多字,引用很多资料。。。。。
另外,有些,比如 Code_attribute 中的u2 exception_table_length 为0,上面的异常表长度为0,
u2 exception_table_length; { u2 start_pc; u2 end_pc; u2 handler_pc; u2 catch_type; } exception_table[exception_table_length];
后面的{}中的也就没有了貌似。。。
我也没仔细看到底为什么,有些好像是有规定如果某个项为0,后面一个项就没有了,我忘了是哪个。。。反正肯定在这个字节码中有。。。看了转头就忘了。。。
PS:分析个字节码真是麻烦啊,看着好几个东西,对照着。。。还得一直看,要不就得做记录,要不就忘了看到哪里了。。。不过看完这个后,貌似自己也可以搞一个javap了。是不是看看javap其实就o了。
这是哪个大神定义的这个
ClassFile
Structure和
Instructions呀,真是给力