今天继续分析Class文件的结构。
上次读取了constant pool,紧接着是这个类或接口的Access flags,JVM定义的关于class或interface的Access flags有:
private static short ACC_PUBLIC = 0x0001;
private static short ACC_FINAL = 0x0010;
private static short ACC_SUPER = 0x0020;
private static short ACC_INTERFACE = 0x0200;
private static short ACC_ABSTRACT = 0x0400;
// read access flags:
short access_flags = input.readShort();
System.out.print("Access flags:");
if((access_flags & ACC_PUBLIC) == ACC_PUBLIC)
System.out.print(" public");
if((access_flags & ACC_FINAL) == ACC_FINAL)
System.out.print(" final");
if((access_flags & ACC_SUPER) == ACC_SUPER)
System.out.print(" super");
if((access_flags & ACC_INTERFACE) == ACC_INTERFACE)
System.out.print(" interface");
if((access_flags & ACC_ABSTRACT) == ACC_ABSTRACT)
System.out.print(" abstract");
System.out.println();
然后是this class和super class:
// read this class and super class:
short this_class_index = input.readShort();
short super_class_index = input.readShort();
System.out.println("This class = " + this_class_index);
System.out.println("Super class = " + super_class_index);
根据this class的index可以从constant pool中得到这个class的信息,super class也一样。每个class都有super class,只有Object的super class index=0。
接下来是这个class继续了多少个interface和每个interface在constant pool中的index:
// read interfaces count:
short interfaces_count = input.readShort();
System.out.println("Interfaces count = " + interfaces_count);
// read each interface:
for(int i=1; i<=interfaces_count; i++) {
short interface_index = input.readShort();
System.out.println("No. " + i + " interface index = " + interface_index);
}
结果如下:
Access flags: public final super
This class = 2
Super class = 4
Interfaces count = 0
待续...