Class 文件结构

语言无关性
文件结构
魔数
版本
常量
访问符
类、超类、接口
字段
方法
属性


语言无关

其他语言也可以编译到 *.class 文件



u1 表示一个byte整数

u4 表示4个byte整数

文件结构

类型

名称

数量

u4

magic(表示是否class 文件)

1

u2

minor_version(java的版本号)

1

u2

major_version

1

u2

constant_pool_count

1

cp_info

constant_pool(常量池)

constant_pool_count - 1

u2

access_flags(private pulic,protected )

1

u2

this_class

1

u2

super_class(单继承,只有一个超类)

1

u2

interfaces_count

1

u2

interfaces(接口)

interfaces_count

u2

fields_count

1

field_info

fields(字段)

fields_count

u2

methods_count

1

method_info

methods(方法)

methods_count

u2

attribute_count

1

attribute_info

attributes(属性)

attributes_count




常量池 ,应该是class 最为重要的一部分 

n constant_pool_count  u2



CONSTANT_Utf8
tag1  标识别
length u2
bytes[length]


CONSTANT_Integer
  tag3
byte u4

public static final int sid=99;


CONSTANT_String
  tag8
string_index   u2 ( 指向 utf8 的索引(指针) )


public static final String sname="geym";










------------------

accessflag u2 :类的标示符

Flag Name

Value

Interpretation

ACC_PUBLIC

0x0001

public

ACC_FINAL

0x0010

final,不能被继承.

ACC_SUPER

0x0020

是否允许使用invokespecial指令,JDK1.2后,该值为true

ACC_INTERFACE

0x0200

是否是接口

ACC_ABSTRACT

0x0400

抽象类

ACC_SYNTHETIC

0x1000

该类不是由用户代码生成,运行时生成的,没有源码

ACC_ANNOTATION

0x2000

是否为注解

ACC_ENUM

0x4000

是否是枚举


this_class  u2
指向常量池的 Class
super_class  u2
指向常量池的 Class

interface_count u2
接口数量
interfaces
interface_count interfaceu2
每个 interface 是指向 CONSTANT_Class 的索引

field_count
字段数量
fields
field_count field_info
field
access_flags  u2
name_index  u2
descriptor_index  u2
attributes_count  u2
attribute_info attributes[ attributes_count ];

access_flags

Flag Name

Value

Interpretation

ACC_PUBLIC

0x0001

public

ACC_PRIVATE

0x0002

private

ACC_PROTECTED

0x0004

protected

ACC_STATIC

0x0008

static.

ACC_FINAL

0x0010

final

ACC_VOLATILE

0x0040

volatile

ACC_TRANSIENT

0x0080

transient

ACC_SYNTHETIC

0x1000

synthetic; 没有源码,编译器生成

ACC_ENUM

0x4000

枚举类型



descriptor_index的字符串存放位置在哪里?


文件结构- method

methods_count
方法数量
methods
methods_count method_info

method_info
access_flags u2
name_index u2
descriptor_index u2
attributes_count u2
attribute_info attributes[ attributes_count ]

accessflag

Flag Name

Value

Interpretation

ACC_PUBLIC

0x0001

public

ACC_PRIVATE

0x0002

private

ACC_PROTECTED

0x0004

protected

ACC_STATIC

0x0008

static

ACC_FINAL

0x0010

final

ACC_SYNCHRONIZED

0x0020

synchronized

ACC_BRIDGE

0x0040

编译器产生 桥接方法

ACC_VARARGS

0x0080

可变参数

ACC_NATIVE

0x0100

native

ACC_ABSTRACT

0x0400

abstract

ACC_STRICT

0x0800

strictfp

ACC_SYNTHETIC

0x1000

不在源码中,由编译器产生

name_index u2
法名字,常量池 UTF-8 索引
descriptor_index u2
描述符 用于 表达 方法的参数和返回值

方法描述符
void  inc ()   ()V
void  setId ( int (I)V
int  indexOf (char[], int ) ([CI)I

文件结构- attribute



名称

使用者

描述

Deprecated(废弃)

field method

字段、方法、类被废弃

ConstantValue

field

final常量

Code

method

方法的字节码和其他数据

Exceptions

method

方法的异常

LineNumberTable

Code_Attribute

方法行号和字节码映射

LocalVaribleTable

Code_Attribute

方法局部变量表描述

SourceFile

Class file

源文件名

Synthetic

field method

编译器产生的方法或字段



Deprecated
attribute_name_index u2
attribute_length u4
n attribute_name_index
指向包含 Deprecated UTF-8 常量
attribute_length
0




























猜你喜欢

转载自blog.csdn.net/u010325193/article/details/80791305