Java Class File Structrue

本文详细介绍了Java类文件的内部结构,包括ClassFile的主要组成部分及其字段类型解释,常量池中不同类型的条目格式,以及字段、方法和属性的具体结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A class file consists of a single ClassFile structure: 

ClassFile {

u4 magic;

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];

}

The interpretation of the field types is as shown in Table 4.2.

BaseType Character Type Interpretation
Bbytesigned byte
Cchar Unicode character
Ddouble double-precision floating-point value
Ffloatsingle-precision floating-point value
Iintinteger
Jlonglong integer
L ; referencean instance of class
Sshortsigned short
Zbooleantrue or false
[ referenceone array dimension

All constant_pool table entries have the following general format: 

cp_info {

u1 tag;

u1 info[];

}

Each item in the constant_pool table must begin with a 1-byte tag indicating the kind of cp_info entry. The contents of the info array vary with the value of tag. The valid tags and their values are listed in Table 4.3. Each tag byte must be followed by two or more bytes giving information about the specific constant. The format of the additional information varies with the tag value.

Constant Type Value
CONSTANT_Class 7
CONSTANT_Fieldref 9
CONSTANT_Methodref 10
CONSTANT_InterfaceMethodref 11
CONSTANT_String 8
CONSTANT_Integer 3
CONSTANT_Float 4
CONSTANT_Long 5
CONSTANT_Double 6
CONSTANT_NameAndType 12
CONSTANT_Utf8 1

Fields, methods, and interface methods are represented by similar structures: 

CONSTANT_Fieldref_info {

u1 tag;

u2 class_index;

u2 name_and_type_index;

}

CONSTANT_Methodref_info {

u1 tag;

u2 class_index;

u2 name_and_type_index;

}

CONSTANT_InterfaceMethodref_info {

u1 tag;

u2 class_index;

u2 name_and_type_index;

}

The CONSTANT_String_info structure is used to represent constant objects of the type String: CONSTANT_String_info {

u1 tag;

u2 string_index;

}

The CONSTANT_Integer_info and CONSTANT_Float_info structures represent 4-byte numeric (int and float) constants:

CONSTANT_Integer_info {

u1 tag;

u4 bytes;

}

CONSTANT_Float_info {

u1 tag;

u4 bytes;

}

The CONSTANT_Long_info and CONSTANT_Double_info represent 8-byte numeric (long and double) constants:

CONSTANT_Long_info {

u1 tag;

u4 high_bytes;

u4 low_bytes;

}

CONSTANT_Double_info {

u1 tag;

u4 high_bytes;

u4 low_bytes;

}

The CONSTANT_NameAndType_info structure is used to represent a field or method, without indicating which class or interface type it belongs to:

CONSTANT_NameAndType_info {

u1 tag;

u2 name_index;

u2 descriptor_index;

}

Each field is described by a field_info structure. No two fields in one class file may have the same name and descriptor (§4.3.2). The format of this structure is

field_info {

u2 access_flags;

u2 name_index;

u2 descriptor_index;

u2 attributes_count;

attribute_info attributes[attributes_count];

}

access_flags

The value of the access_flags item is a mask of flags used to denote access permission to and properties of this field. The interpretation of each flag, when set, is as shown in 

Flag Name Value Interpretation
ACC_PUBLIC 0x0001 Declared public; may be accessed from outside its package.
ACC_PRIVATE 0x0002 Declared private; usable only within the defining class.
ACC_PROTECTED 0x0004 Declared protected; may be accessed within subclasses.
ACC_STATIC 0x0008 Declared static.
ACC_FINAL 0x0010 Declared final; no further assignment after initialization.
ACC_VOLATILE 0x0040 Declared volatile; cannot be cached.
ACC_TRANSIENT 0x0080 Declared transient; not written or read by a persistent object manager.

Each method, including each instance initialization method and the class or interface initialization method , is described by a method_info structure. No two methods in one class file may have the same name and descriptor .

The structure has the following format: 

method_info {

u2 access_flags;

u2 name_index;

u2 descriptor_index;

u2 attributes_count;

attribute_info attributes[attributes_count];

}

access_flags

The value of the access_flags item is a mask of flags used to denote access permission to and properties of this method. The interpretation of each flag, when set, is as shown in Table 4.5.

  
  

Flag Name Value Interpretation
ACC_PUBLIC 0x0001 Declared public; may be accessed from outside its package.
ACC_PRIVATE 0x0002 Declared private; accessible only within the defining class.
ACC_PROTECTED 0x0004 Declared protected; may be accessed within subclasses.
ACC_STATIC 0x0008 Declared static.
ACC_FINAL 0x0010 Declared final; may not be overridden.
ACC_SYNCHRONIZED 0x0020 Declared synchronized; invocation is wrapped in a monitor lock.
ACC_NATIVE 0x0100 Declared native; implemented in a language other than Java.
ACC_ABSTRACT 0x0400 Declared abstract; no implementation is provided.
ACC_STRICT 0x0800 Declared strictfp; floating-point mode is FP-strict
Attributes are used in the ClassFile, field_info, method_info, and Code_attribute structures of the class file format. All attributes have the following general format:

attribute_info {

u2 attribute_name_index;

u4 attribute_length;

u1 info[attribute_length];

}

The ConstantValue attribute is a fixed-length attribute used in the attributes table of the field_info structures

The ConstantValue attribute has the following format:

ConstantValue_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 constantvalue_index;
}
The Code attribute is a variable-length attribute used in the attributes table of method_info structures

The Code attribute has the following format:

Code_attribute {	
u2 attribute_name_index;	
u4 attribute_length;	
u2 max_stack;	
u2 max_locals;	
u4 code_length;	
u1 code[code_length];	
u2 exception_table_length;	{    	
u2 start_pc;	      	
u2 end_pc;	      	
u2  handler_pc;	      	
u2  catch_type;	}
exception_table[exception_table_length];	
u2 attributes_count;	
attribute_info attributes[attributes_count];}
The Exceptions attribute is a variable-length attribute used in the attributes table of a method_info 

The Exceptions attribute has the following format:

Exceptions_attribute {	
u2 attribute_name_index;	
u4 attribute_length;	
u2 number_of_exceptions;	
u2 exception_index_table[number_of_exceptions];
}
The InnerClasses attribute5 is a variable-length attribute in the attributes table of the ClassFile 

The InnerClasses attribute has the following format:

InnerClasses_attribute {	
u2 attribute_name_index;	
u4 attribute_length;	
u2 number_of_classes;	
{  
u2 inner_class_info_index;
u2 outer_class_info_index;
u2 inner_name_index;
u2 inner_class_access_flags;
} 
classes[number_of_classes];
}
The Synthetic attribute6 is a fixed-length attribute in the attributes table of ClassFile, field_info, and method_info  structures.

The Synthetic attribute has the following format:

Synthetic_attribute {
u2 attribute_name_index;
u4 attribute_length;
}
The SourceFile attribute is an optional fixed-length attribute in the attributes table of the ClassFile  structure

The SourceFile attribute has the following format:

SourceFile_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 sourcefile_index;
}
The LineNumberTable attribute is an optional variable-length attribute in the attributes table of a Code attribute.

The LineNumberTable attribute has the following format:

LineNumberTable_attribute {	
u2 attribute_name_index;
u4 attribute_length;
u2 line_number_table_length;
{  u2 start_pc;
u2 line_number;
} 
line_number_table[line_number_table_length];}The LocalVariableTable attribute is an optional variable-length attribute of a Code  attribute. 

The LocalVariableTable attribute has the following format:

LocalVariableTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 local_variable_table_length;
{  u2 start_pc;
u2 length;
u2 name_index;
u2 descriptor_index;
u2 index;	} 
local_variable_table[local_variable_table_length];
}
The Deprecated attribute7 is an optional fixed-length attribute in the attributes table of ClassFile (§4.1), field_info (§4.5), and method_info (§4.6)
structures.

The Deprecated attribute has the following format:

Deprecated_attribute {
u2 attribute_name_index;
u4 attribute_length;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值