Java字节码(.class文件)格式详解(三) (二)

2014-11-24 02:33:29 · 作者: · 浏览: 6

CONSTANT_NameAndType_info(12)

access_flags

this_class

super_class

interfaces

fields

access_flags

name

descriptor

attributes

ConstantValue Attribute

Synthetic Attribute

Signature Attribute

Deprecated Attribute

RuntimeVisibleAnnotations Attribute

RuntimeInvisibleAnnotations Attribute

methods

access_flags

name

descriptor

attributes

Code Attribute

StackMapTable Attribute

LineNumberTable Attribute

LocalVariableTable Attribute

LocalVariableTypeTable Attribute

Exceptions Attribute

RuntimeVisibleParameterAnnotations Attribute

RuntimeInvisibleParameterAnnotations Attribute

AnnotationDefault Attribute

Synthetic Attribute

Signature Attribute

Deprecated Attribute

RuntimeVisibleAnnotations Attribute

RuntimeInvisibleAnnotations Attribute

attributes

InnerClasses Attribute

EnclosingMethod Attribute

SourceFile Attribute

SourceDebugExtension Attribute

Synthetic Attribute

Signature Attribute

Deprecated Attribute

RuntimeVisibleAnnotations Attribute

RuntimeInvisibleAnnotations Attribute


附件A Java字节码中的类和接口名

在Java字节码中类和接口名主要表现以下几点:

1. 类和接口名都是以全限定名的方式存放(包名加类或接口名)。

2. 在源代码中的点分隔符”.”在字节码中以斜杠”/”代替。如:“java.lang.Object”-> “java/lang/Object

3. 数组类型名做了特殊处理。如:“int[][]”-> “[[I”、“Thread[]”->“[Ljava/lang/Thread”。详见附录BJava字节码中的数组类型名

附件B Java字节码中的数组类型名

在Java中,数组被认为是类,因而它也有对应的类名表示,而Java字节码为数组名指定了特定的格式:

1. 所有数组名都以“[”开头,n维数组有n个“[”。

2. 对引用类型的数组,在“[”后加“L后加引用类型的全限定名。

3. 对基本类型,在“[”后加基本类型的对应字符

基本类型对应字符表

基本类型

对应字符

byte

B

char

C

double

D

float

F

int

I

long

J

short

S

boolean

Z

附件C 描述符(Descriptor

描述符(Descriptor)定义了字段或方法的类型(A descriptor is a string representing the type of a field or method.这段描述感觉不怎么精确)。它存放在constant pool中的CONSTANT_Utf8_info类型项中。

1. 字段描述符(Field Descriptor

字段描述符是定义了字段、局部变量、参数等类型的字符串。即附录A中的类或接口名。

语法定义:

FieldDescrptor :

FieldType

BaseType:B、C、D、F、I、J、S、Z(参考附录B中的基本类型对应字符表)

ObjectType:LfullClassName;

ArrayType:[+BaseType | [+ObjectType

FieldType:BaseType | ObjectType | ArrayType

如:[[D -> double[][]、[Ljava/lang/Thread; -> Thread[]、I->int、Ljava/lang/Object; -> Object

2. 方法描述符(Method Descriptor

方法描述符是定义了方法参数、方法返回等信息的字符串。

语法定义:

MethodDescriptor:

ParameterDescriptor*ReturnDescriptor

ParameterDescriptor :FieldType

ReturnDescriptor :FieldType | VoidDescriptor

VoidDescriptor :V

如:void method(int i, Object obj)-> (ILjava/lang/Object;)V

Object getValue()-> ( )Ljava/lang/Object;

Object mymethod(int i, double d, Object o) -> (IDLjava/lang/Object;)Ljava/lang/Object;

p align="left">

附件D 签名(Signature

签名(Signature)定义了类、字段或方法的泛型类型信息(A signature is a string representing the generic type of a field or method, or generic type information for a class declaration. 这段描述感觉不怎么精确)。它也存放在constant pool中的CONSTANT_Utf8_info类型项中。

它存在于Signature Attribute中,只有包含泛型的类、字段、方法才会产生Signature Attribute

签名信息并不是给JVM用的,而是用于编译、调试、反射。

1. 类签名

语法定义:

ClassSignature:

FormalTypeParametersopt SuperclassSignature SuperinterfaceSignature*

FormalTypeParameters:

<FormalTypeParameter+>

FormalTypeParameter:

Identifier ClassBo