上善若水
In general the OO style is to use a lot of little objects with a lot of little methods that give us a lot of plug points for overriding and variation. To do is to be -Nietzsche, To bei is to do -Kant, Do be do be do -Sinatra
posts - 146,comments - 147,trackbacks - 0

2.9   methods

methods数组记录了类或接口中的所有方法,包括实例方法、静态方法、实例初始化方法和类初始化方法,但不包括父类或父接口中定义的方法。methods数组中每项都是method_info类型值,它描述了方法的详细信息,如名称、描述符、方法中的attribute(如Code Attribute记录了方法的字节码)等。

method_info

type

descriptor

remark

u2

access_flags

记录方法的访问权限。见2.9.1

u2

name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定方法名称。

u2

descriptor_index

constant_pool中的索引,CONSTANT_Utf8_info类型,指定方法的描述符(见附录C)。

u2

attributes_count

attributes包含的项目数。

attribute_info

attributes[attributes_count]

字段中包含的Attribute集合。见2.9.2-2.9.11

methods数组同样有和fields数组一样的问题,包括fields中项和CONSTANT_Methodref_info以及CONSTANT_InterfaceMethodref_info中的区别。以及设计上的问题。详见field_info中的注。

 

2.9.1    方法访问权限

方法的访问权限

Flag Name

Value

Remarks

ACC_PUBLIC

0x0001

pubilc,包外可访问。

ACC_PRIVATE

0x0002

private,只可在类内访问。

ACC_PROTECTED

0x0004

protected,类内和子类中可访问。

ACC_STATIC

0x0008

static,静态。

ACC_FINAL

0x0010

final,不可被重写。

ACC_SYNCHRONIZED

0x0020

synchronized,同步方法。

ACC_BRIDGE

0x0040

bridge方法,由编译器生成。(什么是bridge方法?)

ACC_VARARGS

0x0080

包含不定参数个数的方法。

ACC_NATIVE

0x0100

native,非Java语言实现的方法。(如何实现Native方法?)

ACC_ABSTRACT

0x0400

abstract,抽象方法。

ACC_STRICT

0x0800

strictfp,设置floating-point模式为FP-strict(什么是FP-strict模式?)

ACC_SYNTHETIC

0x1000

synthetic,由编译器产生,不存在于源代码中。

注:接口中的方法必须同时设置:ACC_PUBLICACC_ABSTRACT。设置了ACC_ABSTRACT后,不可以再设置ACC_FINALACC_STATICACC_PRIVATEACC_NATIVEACC_SYNCHRONIZED ACC_STRICT

 

2.9.2    Code Attribute JVM识别)

每个非abstract、非native方法的attributes集合都包含有且仅有一项Code Attribute。它包含了一个方法的栈、局部变量、字节码以及和代码相关的Attribute信息。

Code Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称(“Code”)

u4

attribute_length

Attribute内容的字节长度。

u2

max_stack

该方法操作栈的最大深度。

u2

max_locals

该方法调用时需要分配的局部变量的最大个数,包括该方法的参数。

u4

code_length

该方法字节码长度(以字节为单位)

u1

code[code_length]

存放字节码数组(字节码如何解析?)

u2

exception_table_length

异常表的长度。

exception_table_info

每个表项记录一段异常处理代码信息和范围。

u2

start_pc

记录应用该项异常处理的起始字节码。在字节码数组中的起始索引号[start_pc, end_pc)索引号必须是opcode(一条指令的开始位置)对应的位置。

u2

end_pc

u2

handler_pc

记录该项异常处理代码的开始地址。在字节码数组中的开始索引号。索引号必须是opcode对应的位置。

u2

catch_type

constant_pool中的索引,CONSTANT_Class_info类型。指定该项能捕获的异常类(或其子类)。或0用于实现finally语法(即不管什么类型都会捕获。)

exception_table[exception_table_length]

 

u2

attributes_count

attributes包含的项目数。

attribute_info

attributes[attributes_count]

字段中包含的Attribute集合。见2.9.2.1-2.9.2.4

 

2.9.2.1 StackMapTable Attribute JVM识别)

StackMapTable AttributeJ2SE 6中引入,记录了类型检查时需要用到的信息,如字节码的偏移量、局部变量的验证类型、操作栈中的验证类型,用于类型检查过程。在Code Attribute只能包含一项StackMapTable Attribute,记录所有当前Code Attribute中的验证信息。

 

一项StackMapTable Attribute中包含多项stack_map_frame。每项stack_map_frame显式或隐式得记录了字节码的偏移量、局部变量验证类型和操作栈的验证类型。验证器就是通过获取局部变量类型和操作栈类型进行验证的(具体如何验证呢?)

 

stack_map_frame中,并不是直接记录了字节码的索引值,而是记录了offset_delta的值。stack_map_frame中的每一项都通过前一项的值+1+offset_delta计算出当前项对应的真正的字节码的位置,只有当当前stack_map_frame的前一项是当前方法的初始帧(initial frame of the method什么是方法的初始帧?)的时候,offset_delta的值才直接表示字节码位置。

(为什么不直接记录字节码的索引值?为了保证stack_map_frame被正确的排序了。为什么要加1再加offset_delta?为了避免重复出现stack_map_frame项。)

StackMapTable Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称(“StackMapTable”)

u4

attribute_length

Attribute内容的字节长度。

u2

number_of_entries

stack_map_frame项的数目。

stack_map_frameunion,联合体类型)

每一项显式或隐式得记录了字节码的偏移量、局部变量验证类型和操作栈的验证类型。每一项第一个字节指定当前stack_map_frame的类型(tag

same_frame

same_frame {

     u1 frame_type = SAME; /* 0-63 */

}

tag的值为[0-63]offset_delta = frame_type。表示当前帧和前一帧有相同的局部变量,并且当前操作数栈为空。

same_locals_1_stack

_item_frame

same_locals_1_stack_item_frame {

u1 frame_type = SAME_LOCALS_1_STACK_ITEM; /* 64-127 */

     verification_type_info stack[1]

}

tag值为[64-127]offset_delta = frame_type – 64。表示当前帧和前一帧有相同的局部变量,并且操作栈内的操作数条目数为1,因而它为该操作数栈内的操作数保存了一项verification_type_info(详见下表)。([128-246]tag值保留)

same_locals_1_stack

_item_frame_extended

same_locals_1_stack_item_frame_extended {

     u1 frame_type = SAM_LOCALS_1_STACK_ITEM_EXTENEDED; /* 247 */

     u2 offset_delta;

     verification_type_info stack[1];

}

tag值为247offset_delta = offset_delta。表示当前值帧和前一帧有相同的局部变量,并且操作栈内的操作数条目数为1,因而它为该操作数栈内的操作数保存了一项verification_type_info(详见下表)。和上一类型的区别是这里的offset_delta是直接给出的。

chop_frame

chop_frame {

     u1 frame_type = CHOP; /* 248 – 250 */

     u2 offset_delta;

}

tag值为[248, 250]offset_delta = offset_delta。表示当前操作栈为空,而当前局部变量比前一帧的局部变量少后面的251 – frame_type个局部变量。

same_frame_extended

same_frame_extended {

     u1 frame_type = SAME_FRAME_EXTENDED; /* 251 */

     u2 offset_delta;

}

tag值为251offset_delta = offset_delta。表示当前帧和前一帧有相同的局部变量,并且操作数栈为空。和samep_frame的区别是same_frame_extended中的offset_delta值直接给出。

append_frame

append_frame {

     u1 frame_type = APPEND; /* 252 – 254 */

     u2 offset_delta;

     verification_type_info locals[frame_type – 251];

}

tag值为[252-254]offset_delta = offset_delta。表示操作数栈为空,而当前帧的局部变量比前一帧的局部变量多frame_type – 251个。因而它也定义了frame_type – 251项的verification_type_info类型。详见下表。

full_frame

full_frame {

     u1 frame_type = FULL_FRAME; /* 255 */

     u2 offset_delta;

     u2 number_of_locals;

     verification_type_info locals[number_of_locals];

     u2 number_of_stack_items;

     verification_type_info stack[number_of_stack_items];

}

tag255offset_delta = offset_deltafull_frame则定义了所有的信息,包括offset_delta的值,以及当前帧和前一帧不同的所有局部变量和操作数。locals[0]表示0号局部变量;stack[0]表示栈底操作数。

entries[number_of_entries]

 

 

verification_type_infounion,联合体类型)

记录字节码的验证类型。每一项第一个字节指定验证类型(tag)。

Top_variable_info

Top_variable_info {

    u1 tag = ITEM_Top; /* 0 */

}

指定局部变量的验证类型为Top

Integer_variable_info

Integer_variable_info {

    u1 tag = ITEM_Integer; /* 1 */

}

指定验证类型为int

Float_variable_info

Float_variable_info {

    u1 tag = ITEM_Float; /* 2 */

}

指定验证类型为float

Long_variable_info

Long_variable_info {

    u1 tag = ITEM_Long; /* 4 */

}

指定验证类型为long

Double_variable_info

Double_variable_info {

    u1 tag = ITEM_Double; /* 3 */

}

指定验证类型为double

Null_variable_info

Null_variable_info {

    u1 tag = ITEM_Null; /* 5 */

}

指定验证类型为null

UninitializedThis_variable_info

UninitializedThis_variable_info {

    u1 tag = ITEM_UninitializedThis; /* 6 */

}

指定验证类型为uninitializedThis(什么是uninitializedThis?)。

Object_variable_info

Object_variable_info {

u1 tag = ITEM_Object; /* 7 */

u2 cpool_index; //constant_pool索引,CONSTANT_Class_info类型。

}

指定验证类型为cpool_index中指定的类型实例。

Uninitialized_variable_info

Uninitiated_variable_info {

u1 tag = ITEM_Uninitialized; /* 8 */

u2 offset;

}

指定验证类型为uninitialized(这种类型是指什么?)。offset记录了用于创建实例的new指令的偏移量。(The offset item indicates the offset of the new instruction that created the object being stored in the location.这段话是什么意思?)

注:对验证过程不太了解,因而StackMapTable Attribute的一些描述也没能理解。

 

2.9.2.2 LineNumberTable Attribute (调试信息)

LineNumberTable Attribute用于调试器,以获取某条指令对应的源代码中的行号。多条指令可以对应相同的行号。

LineNumberTable Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称(“LineNumberTable”)

u4

attribute_length

Attribute内容的字节长度。

u2

line_number_table_length

异常表的长度。

line_number_table

一条指令和源代码行号的映射关系表。

u2

start_pc

一条指令的开始索引(字节码数组中的索引号)

u2

line_number

源代码中的行号。

line_number_table[line_number_table_length]

 

 

2.9.2.3 LocalVariableTable Attribute (调试信息)

LocalVariableTable Attribute用于调试器,以获取在方法运行时局部变量的信息。在一个Code Attribute中只包含10LocalVariableTable Attribute

LocalVariableTable Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称(“LocalVariableTable”)

u4

attribute_length

Attribute内容的字节长度。

u2

local_variable_table_length

局部变量表的长度。

local_variable_table

每项记录了一个局部变量有值的范围和该局部变量在局部变量数组中的索引值。

u2

start_pc

记录该局部变量的有值范围,字节码数组中的索引范围[start_pc, start_pc+length)

u2

length

u2

name_index

constant_pool中索引,CONSTANT_Utf8_info类型,记录该项代表的局部变量名。

u2

descriptor_index

constant_pool中索引,CONSTANT_Utf8_info类型,记录该项代表的局部变量的字段描述符(见附录C)。

u2

index

记录该项代表的局部变量在方法的局部变量数组中的索引。

local_variable_table[local_variable_table_length]

 

 

2.9.2.4 LocalVariableTypeTable Attribute (调试信息)

LocalVariableTypeTable Attribute用于调试器,以获取在方法运行时泛型局部变量的信息。在一个Code Attribute中只包含10LocalVariableTypeTable Attribute

 

LocalVariableTable AttributeLocalVariableTypeTable Attribute表达的信息是类似的,他们的区别是对泛型类型的局部变量,需要用Signature的形式表达,而不能仅仅用Descriptor的形式表达,因而对泛型类型的局部变量,需要在LocalVariableTable AttributeLocalVariableTypeTable Attribute中同时存在一项;而对非泛型类型的局部变量来说,只要在LocalVariableTable Attribute存在表项就可以了。

 

从这里我们也可以看出泛型是后期才被字节码所支持的痕迹。我感觉很奇怪的是Java在设计的时候,泛型应该已经开始流行了,为什么它在设计之初没有把它考虑进去,而要到后期加入,然后让这种修补的设计做的那么糟糕呢,Java的设计者如果能在设计的时候把它作为扩展考虑,然后再后期去实现,不是更好吗?

LocalVariableTypeTable Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称(“LocalVariableTypeTable”)

u4

attribute_length

Attribute内容的字节长度。

u2

local_variable_type_table_length

泛型局部变量表的长度。

local_variable_type_table

每项记录了一个泛型局部变量有值的范围和该局部变量在局部变量数组中的索引值。

u2

start_pc

记录该局部变量的有值范围,字节码数组中的索引范围[start_pc, start_pc+length)

u2

length

u2

name_index

constant_pool中索引,CONSTANT_Utf8_info类型,记录该项代表的局部变量名。

u2

signature_index

constant_pool中索引,CONSTANT_Utf8_info类型,记录该项代表的局部变量的字段签名(见附录D)。

u2

index

记录该项代表的局部变量在方法的局部变量数组中的索引。

local_variable_type_table[local_variable_table_length]

 

 

2.9.3    Exceptions Attribute JVM识别)

Exceptions Attribute记录了一个方法需要检验的异常类型。一个method_infoattributes中只能包含一项Exceptions Attribute。即记录一个方法可以抛出的异常类型。

一个方法可以抛出的异常类型遵循三点:

1. 抛出的异常是RuntimeException类型或其子类。

2. 抛出的异常是Error类型或其子类。

3. 抛出的异常是Exceptions Attribute中记录的类型或它们的子类。

Exceptions Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称(“Exceptions”)

u4

attribute_length

Attribute内容的字节长度。

u2

number_of_exceptions

exception_index_table表项长度

u2

exception_index_table[number_of_exceptions]

每项为constant_pool中的索引,CONSTANT_Class_info类型。记录该方法可抛出的异常类型。

 

2.9.4    RuntimeVisibleParameterAnnotations Attribute

RuntimeVisibleParameterAnnotations Attribute记录该方法在运行时可见的修饰该方法参数的Annotation,从而Java程序可以通过反射机制获取这些Annotation中的值。一个method_info中的attributes中只能包含一项RuntimeVisibleAnnotations Attribute

RuntimeVisibleAnnotations Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称(“RuntimeVisibleAnnotations”)

u4

attribute_length

Attribute内容的字节长度。

u2

num_parameters

记录该方法中参数个数

parameter_annotations

每个参数和在它之上定义的annotations的映射表。顺序和源码定义顺序一致。

u2

num_annotations

记录该参数定义的annotation的个数

annotation

annotations[num_annotations]

记录该项对应的参数中所有运行时可见的annotaion项,顺序和源码定义顺序一致。annotation类型见附件E

parameter_annotations[num_parameters]

 

注:从该数据结构的定义中可以看到,JavaParameter Annotation存放的信息是很少的,我们只能依赖于定义的顺序来获取这些Annotation,而不能通过参数名或者参数类型来获取相应的Annotation。事实上,

1.       由于参数名的信息只在调试时才有,如LocalVariableTable AttributeLocalVariableTypeTable Attribute中;

2.       而一个方法中不同参数的类型极有可能是相同的;

因而从逻辑上来说,通过参数名或者参数类型返回相应的Annotation信息的方式也是不合理的。由于这个原因,在java.lang.reflect.Method类的方法中也只是给出了:
Annotation[][] getParameterAnnotations()

的方法,获得所有参数中的Annotation,这里的二维数组一维代表参数,一维代表多个Annotation,它们的顺序和源码定义时顺序相同。

2.9.5    RuntimeInvisibleParameterAnnotations Attribute

RuntimeInvisibleParameterAnnotations Attribute记录该方法在运行时不可见的修饰该方法参数的Annotation。一个method_info中的attributes中只能包含一项RuntimeInvisibleAnnotations Attribute

 

RuntimeInvisibleParameterAnotations AttributeRuntimeVisibleAnnotations Attribute区别在于:

后者中的Annotation默认情况下,可以通过Java提供的反射函数获取相应的Annotation,而前者的Annotation在默认情况下是无法通过Java提供的反射函数被获取的,而需要通过特定的机制(如设置JVM的特定参数,该机制由不同的JVM实现来决定)才能通过Java提供的反射函数获取内部的Annotation

 

然而这样就又有一个问题了,RuntimeInvisibleParameterAnotations Attribute是如何被填入值的呢?通过什么机制让源码中方法参数的Annotation是默认不可见的呢?我感觉这个也可能也是由不同编译器提供不同的机制来实现的,不知道sun提供的编译器有没有什么机制支持它了??

 

RuntimeInvisibleAnnotations Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称(“RuntimeInvisibleAnnotations”)

u4

attribute_length

Attribute内容的字节长度。

u2

num_parameters

记录该方法中参数个数

parameter_annotations

每个参数和在它之上定义的annotations的映射表。顺序和源码定义顺序一致。

u2

num_annotations

记录该参数定义的annotation的个数

annotation

annotations[num_annotations]

记录该项对应的参数中所有运行时不可见的annotaion项,顺序和源码定义顺序一致。annotation类型见附件E

parameter_annotations[num_parameters]

 

 

2.9.6    AnnotationDefault Attribute

AnnotationDefault Attribute用于Annotation类型方法中,以记录该方法所代表的Annotation类型的默认值。每个Annotation类型的method_info中的attributes中只能包含一个AnnotationDefault Attribute项。如:

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.METHOD, ElementType.PARAMETER})

public @interface Test {

    public int id() default -1;

    public String description() default "no description";

}

Annotation类产生的class二进制文件中的id方法和description方法的attributes数组中都会包含一项AnnotationDefault Attribute,它们的默认值分别为-1CONSTANT_Integer_info类型)和”no description”CONSTANT_String_info类型)。

AnnotationDefault Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称(“AnnotationDefault”)

u4

attribute_length

Attribute内容的字节长度。

element_value

default_value

记录该方法表示的Annotation类型的默认值。(element_value结构详见附录E

 

2.9.7    Synthetic Attribute

参见2.11.1

2.9.8    Signature Attribute

参见2.11.2

2.9.9    Deprecated Attribute

参见2.11.3

2.9.10    RuntimeVisibleAnnotations Attribute

参见2.11.4

2.9.11    RuntimeInvisibleAnnotations Attribute

参见2.11.5

 

 

2.10 attributes

attributes数组记录了和类或接口相关的所有Attribute项(和字段相关的Attributefield_infoattributes中,和方法相关的Attributemethod_infoattrubutes中,和字节码相关的AttributeCode Attributeattributes中)。attributes数组中的每项都是attribute_info类型,它描述了Attribute的名称、详细信息等。该attributes数组描述了ClassFile的一些额外信息。JVM必须忽略它不能识别的Attribute,而且那些JVM不能识别的的Attribute也不能影响class文件的语义。

 

当前定义的Attribute有:Code AttributeConstant Value AttibuteDeprecated AttributeEnclosing Method AttributeExceptions AttributeInner Classes AttributeLine Number Table AttributeLocal Variable Table AttributeLocal Variable Type Table AttributeRuntime Visible Annotations AttributeRuntime Invisible Annotation AttributeRuntime Visible Parameter Annotation AttributeRuntime Invisible Parameter Annotation AttributeSignature AttributeSource Debug Extension AttributeSource File AttributeStack Map Table AttributeSynthetic AttributeAnnotation Default Attribute等。它们有些只存在于field_info中,有些只存在method_info中,有些只存在ClassFile中,有些只存在于Code Attribute中,还有些可以同时存在于field_infomethod_infoclassfile中。

 

Attribute结构只存在与ClassFilemethod_infofield_infoCode Attribute结构中。

attribute_infoAttribute的基本数据结构)

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称。

u4

attribute_length

Attribute内容的字节长度。

u1

info[attribute_length]

记录Attribute的内容字节数据。

 

在用户自定义的编译器或者Java虚拟机中可以扩展ClassFile中的Attribute表(即自定义新的Attribute)。但是对自定义的Attribute必须遵循一些规则:

1.       用户自定义的新的Attribute(非sun定义的Attribute)命名必须是遵循Java命名规则,即加入公司的包名信息,如:“com.levin.new_attribute

2.       新增的Attribute只可以作为辅助的信息,如增加和自定义调试器相关的调试信息,但是它们不可以改变ClassFile的语义。什么叫改变ClassFile的语义呢?我现在的理解,比如在自定义的Java虚拟机中,为某个方法新增一个Attribute,用以标记该方法在运行是不可以被调用。不知道这个例子合适不合适。

3.       对于自定义的Java虚拟机,禁止因为某些它不识别的Attribute存在而抛出异常或者直接报错。但是这一层限制可以加载自定义编译器中。Java虚拟机必须忽略它不识别的Attribute

 

以下是定义在ClassFile中的Attribute

2.10.1     InnerClasses Attribute

InnerClasses记录当前类的所有内部类。当前类需要记录的内部类的算法如下:

1. 当前类中定义的内部类,包括方法中定义的类。

2. 如果当前类本身是内部类,则还要记录当前类的外部类,直到外部类不是一个内部类。

如:

class Outer {

    public class Inner {

       public void getMethod() {

           class Inner3 {

              class Inner4 {

              }

           }

       }

       public class Inner2 {

           public class Inner5 {

           }

       }

    }

    public class Inner_1 {

    }

}

Outer中的InnerClasses Attribute

    [inner class info: #17 org/levin/insidejvm/miscs/instructions/Outer$Inner, outer class info: #1 org/levin/insidejvm/miscs/instructions/Outer

     inner name: #19 Inner, accessflags: 1 public],

    [inner class info: #20 org/levin/insidejvm/miscs/instructions/Outer$Inner_1, outer class info: #1 org/levin/insidejvm/miscs/instructions/Outer

     inner name: #22 Inner_1, accessflags: 1 public]

Inner中的InnerClasses Attribute

    [inner class info: #1 org/levin/insidejvm/miscs/instructions/Outer$Inner, outer class info: #23 org/levin/insidejvm/miscs/instructions/Outer

     inner name: #25 Inner, accessflags: 1 public],

    [inner class info: #26 org/levin/insidejvm/miscs/instructions/Outer$Inner$1Inner3, outer class info: #0

     inner name: #28 Inner3, accessflags: 16 final],

    [inner class info: #29 org/levin/insidejvm/miscs/instructions/Outer$Inner$Inner2, outer class info: #1 org/levin/insidejvm/miscs/instructions/Outer$Inner

     inner name: #31 Inner2, accessflags: 1 public]

Inner5中的InnerClasses Attribute

    [inner class info: #22 org/levin/insidejvm/miscs/instructions/Outer$Inner, outer class info: #24 org/levin/insidejvm/miscs/instructions/Outer

     inner name: #26 Inner, accessflags: 1 public],

    [inner class info: #27 org/levin/insidejvm/miscs/instructions/Outer$Inner$Inner2, outer class info: #22 org/levin/insidejvm/miscs/instructions/Outer$Inner

     inner name: #29 Inner2, accessflags: 1 public],

    [inner class info: #1 org/levin/insidejvm/miscs/instructions/Outer$Inner$Inner2$Inner5, outer class info: #27 org/levin/insidejvm/miscs/instructions/Outer$Inner$Inner2

     inner name: #30 Inner5, accessflags: 1 public]

InnerClasses Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称(“InnerClasses”)

u4

attribute_length

Attribute内容的字节长度。

u2

number_of_classes

记录内部类的数量。

classes

定义每项Inner Class的信息。

u2

inner_class_info_index

constant_pool中的索引,CONSTANT_Class_info类型。记录内部类名。

u2

outer_class_info_index

若当前项内部类不是包含它的类的成员类,则该值为0;否则该值为constant_pool中的索引,CONSTANT_Class_info类型,记录外部类名。

u2

inner_name_index

若当前项的内部类为匿名类,则该值为0;否则该值为constant_pool中的索引,CONSTANT_Utf8_info类型,记录该内部类的简单名(没有前缀)。

u2

inner_class_access_flags

定义当前项的内部类的访问属性。见下表。

classess[number_of_classes]

 

注:为什么需要为内部类保留那么多的信息呢?是为了在反射的时候获取必要的信息或者在反编译的时候可以更好的还原源代码的结构吗?还是有其他的作用?

内部类的访问权限

Flag Name

Value

Remarks

ACC_PUBLIC

0x0001

pubilc,类外可访问。

ACC_PRIVATE

0x0002

private,类内才可访问。

ACC_PROTECTED

0x0004

protected,类和其子类可访问。

ACC_STATIC

0x0008

static,静态内部类。

ACC_FINAL

0x0010

final,不能有子类。

ACC_INTERFACE

0x0200

接口,同时需要设置:ACC_ABSTRACT。不可同时设置:ACC_FINALACC_SUPERACC_Enum

ACC_ABSTRACT

0x0400

抽象类,无法实例化。不可和ACC_FINAL同时设置。

ACC_SYNTHETIC

0x1000

synthetic,由编译器产生,不存在于源代码中。

ACC_ANNOTATION

0x2000

注解类型(annotation),需同时设置:ACC_INTERFACEACC_ABSTRACT

ACC_ENUM

0x4000

枚举类型

 

2.10.2     EnclosingMethod Attribute

当且仅当一个类是匿名类或者本地类(local class),该类才会包含一项且仅有一项EnclosingMethod Attribute

 

然而什么是本地类(local class)呢?我的理解,所谓本地类就是在方法内部定义的类,如以下类的定义:

class A {

    public Iterator getIterator() {

       class LocalClass {

       }

       return new Iterator() {

           public boolean hasNext() { return false; }

           public Object next() { return null; }

           public void remove() { }

       };

    }

}

匿名类A$1.class中的EnclosingMethod Attribute

 Enclosing Method: #29 #31 org/levin/insidejvm/miscs/instructions/A.getIterator()Ljava/util/Iterator;

本地类A$1LocalClass.class中的EnclosingMethod Attribute

 Enclosing Method: #22 #24 org/levin/insidejvm/miscs/instructions/A.getIterator()Ljava/util/Iterator;

 

EnclosingMethod Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称(“EnclosingMethod”)

u4

attribute_length

Attribute内容的字节长度(4)

u2

class_index

constant_pool中的索引,CONSTANT_Class_info类型。记录定义当前类所在方法的宿主类。如上例中,A$1.classA$1LocalClass.class中的class_index都指向类A

u2

method_index

若当前类没有被方法方法包含,如当前类是赋值给类成员的匿名类,则method_index值为0,否则该method_index的值为constant_pool中的索引,CONSTANT_NameAndType_info类型。记录了class_index指定的类中定义的包含当前类的方法名和类型信息。

注:这里同样也有一个问题,就是EnclosingMethod Attribute存在的目的问题。我现在的理解,该Attribute的存在也应该只是为了用于反射信息和反编译时可以更好的还原原来代码的结构,在虚拟机运行该程序的时候,由于所有的指令已经编译好了,虚拟机应该不需要这些信息。但是事实是这样的吗?有待考证。

 

2.10.3     SourceFile Attribute

SourceFile Attribute用于记录和当前字节码对应的源代码的文件(由编译器产生,该Attribute只是记录相应源代码的文件名,而不记录和路径相关的信息)。一个ClassFile中只能包含一项SourceFile Attribute

SourceFile Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute的名称(“SourceFile”)

u4

attribute_length

Attribute内容的字节长度(2)

u2

sourcefile_index

constant_pool中的索引,CONSTANT_Utf8_info类型。记录相应的源代码文件名。

 

2.10.4     SourceDebugExtension Attribute

SourceDebugExctension AttributeJava为调试时提供的扩展信息,主要用于自定义(扩展)的编译器和调试器。一个ClassFile中只能包含一项SourceFile Attribute

SourceDebugExtension Attribute

type

descriptor

remark

u2

attribute_name_index

constant_pool中的索引,CONSTANT_Utf8_info类型。指定Attribute名称(“SourceDebugExtension”)

u4

attribute_length

Attribute内容的字节长度。

u1

debug_extension[attribute_length]

Utf-8格式的字符串记录扩展调试信息,不以0结尾。

 

2.10.5     Synthetic Attribute

参见2.11.1

2.10.6     Signature Attribute

参见2.11.2

2.10.7     Deprecated Attribute

参见2.11.3

2.10.8     RuntimeVisibleAnnotations Attribute

参见2.11.4

2.10.9     RuntimeInvisibleAnnotations Attribute

参见2.11.5

                                                                                                                                                于2010-12-19日

posted on 2011-09-05 23:33 DLevin 阅读(9305) 评论(0)  编辑  收藏 所属分类: 深入JVM

只有注册用户登录后才能发表评论。


网站导航: