Documentation ¶
Index ¶
- Constants
- type AttributeInfo
- type ClassFile
- func (self *ClassFile) AccessFlags() uint16
- func (self *ClassFile) ClassName() string
- func (self *ClassFile) ConstantPool() ConstantPool
- func (self *ClassFile) Fields() []*MemberInfo
- func (self *ClassFile) InterfaceNames() []string
- func (self *ClassFile) MajorVersion() uint16
- func (self *ClassFile) Methods() []*MemberInfo
- func (self *ClassFile) MinorVersion() uint16
- func (self *ClassFile) SuperClassName() string
- type ClassReader
- type CodeAttribute
- type ConstantClassInfo
- type ConstantDoubleInfo
- type ConstantFieldrefInfo
- type ConstantFloatInfo
- type ConstantInfo
- type ConstantIntegerInfo
- type ConstantInterfaceMethodrefInfo
- type ConstantInvokeDynamicInfo
- type ConstantLongInfo
- type ConstantMemberrefInfo
- type ConstantMethodHandleInfo
- type ConstantMethodTypeInfo
- type ConstantMethodrefInfo
- type ConstantNameAndTypeInfo
- type ConstantPool
- type ConstantStringInfo
- type ConstantUtf8Info
- type ConstantValueAttribute
- type DeprecatedAttribute
- type ExceptionTableEntry
- type ExceptionsAttribute
- type LineNumberInfo
- type LineNumberTableAttribute
- type LocalVariableInfo
- type LocalVariableTableAttribute
- type MarkerAttribute
- type MemberInfo
- type SourceFileAttribute
- type SyntheticAttribute
- type UnparsedAttribute
Constants ¶
const ( 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 CONSTANT_MethodHandle = 15 CONSTANT_MethodType = 16 CONSTANT_InvokeDynamic = 18 )
定义常量池中的各种类型数据的编号
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AttributeInfo ¶
type AttributeInfo interface {
// contains filtered or unexported methods
}
type ClassFile ¶
type ClassFile struct {
// contains filtered or unexported fields
}
ClassFile *
解析 ClassReader 读取到的class文件数据
class文件结构体 构成class文件的基本数据单位是字节,可以把整个class文件当成一个字节流来处理。
为了描述class文件格式,Java虚拟机规范定义了u1、u2和u4三种数据类型来表示1、2和4字节无符号整数, 分别对应Go语言的uint8、uint16和uint32类型。
相同类型的多条数据一般按表(table)的形式存储在class文件中。 表由表头和表项(item)构成,表头是u2或u4整数。假设表头是n,后面就紧跟着n个表项数据。
java的class文件的结构如下:
Class{ 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]; }
关于golang中的访问权限: Go的访问控制非常简单:只有公开和私有两种。 所有首字母大写的类型、结构体、字段、变量、函数、方法等都是公开的,可供其他包使用。 首字母小写则是私有的,只能在包内部使用。
func (*ClassFile) AccessFlags ¶
func (*ClassFile) ConstantPool ¶
func (self *ClassFile) ConstantPool() ConstantPool
func (*ClassFile) Fields ¶
func (self *ClassFile) Fields() []*MemberInfo
func (*ClassFile) InterfaceNames ¶
InterfaceNames InterfaceNames 从常量池中获取接口名,由于接口的数量可能存在多个,所以这里需要一个数组进行存放
func (*ClassFile) MajorVersion ¶
MajorVersion MajorVersion getter
func (*ClassFile) Methods ¶
func (self *ClassFile) Methods() []*MemberInfo
func (*ClassFile) MinorVersion ¶
func (*ClassFile) SuperClassName ¶
SuperClassName SuperClassName从常量池中获取超类名
type ClassReader ¶
type ClassReader struct {
// contains filtered or unexported fields
}
ClassReader * ClassReader 用于读取class文件作格式化处理 读取u1,u2,u4,u8等数据类型
type CodeAttribute ¶
type CodeAttribute struct {
// contains filtered or unexported fields
}
CodeAttribute *
Code_attribute { u2 attribute_name_index; CONSTANT_Utf8_info型常量索引,定值为"Code" u4 attribute_length; length u2 max_stack; 操作数栈的最大深度 u2 max_locals; 局部变量的存储空间(单位:Slot,槽,uint32) u4 code_length; 字节码指令长度 u1 code[code_length]; java源程序编译后生成的字节码指令 u2 exception_table_length; u1 exception_table[exception_table_length]; u2 attributes_count; attribute_info attributes[attributes_count]; }
对上述的一些变量进行详细的说明: max_stack : 在方法执行的任意时刻,操作数栈都不会超过这个深度。虚拟机运行的时候需要根据这个值来分配栈帧(Stack Frame)中的操作栈深度。 max_locals : 方法参数(包括实例方法中的隐藏参数“this”)、显式异常处理程序的参数(Exception Handler Parameter,就是try-catch语句中catch块中所定义的异常)、方法体中 定义的局部变量都需要依赖局部变量表来存放,该空间的使用方法是:重用。javac会根据变量的作用域来重用该控件,详细一点就是:如果槽中的变量已经不在当前的作用域中,那么便可以进行覆盖。
func (*CodeAttribute) Code ¶
func (self *CodeAttribute) Code() []byte
func (*CodeAttribute) ExceptionTable ¶
func (self *CodeAttribute) ExceptionTable() []*ExceptionTableEntry
func (*CodeAttribute) MaxLocals ¶
func (self *CodeAttribute) MaxLocals() uint
func (*CodeAttribute) MaxStack ¶
func (self *CodeAttribute) MaxStack() uint
type ConstantClassInfo ¶
type ConstantClassInfo struct {
// contains filtered or unexported fields
}
ConstantClassInfo class_info *
Class_info{ tag u1 index u2 指向全限定名常量的索引 }
type ConstantDoubleInfo ¶
type ConstantDoubleInfo struct {
// contains filtered or unexported fields
}
ConstantDoubleInfo 64双精度浮点数引用
func (*ConstantDoubleInfo) Value ¶
func (self *ConstantDoubleInfo) Value() float64
type ConstantFieldrefInfo ¶
type ConstantFieldrefInfo struct{ ConstantMemberrefInfo }
type ConstantFloatInfo ¶
type ConstantFloatInfo struct {
// contains filtered or unexported fields
}
ConstantFloatInfo 32位浮点数引用
func (*ConstantFloatInfo) Value ¶
func (self *ConstantFloatInfo) Value() float32
type ConstantInfo ¶
type ConstantInfo interface {
// contains filtered or unexported methods
}
type ConstantIntegerInfo ¶
type ConstantIntegerInfo struct {
// contains filtered or unexported fields
}
ConstantIntegerInfo 32位整型数引用
func (*ConstantIntegerInfo) Value ¶
func (self *ConstantIntegerInfo) Value() int32
type ConstantInterfaceMethodrefInfo ¶
type ConstantInterfaceMethodrefInfo struct{ ConstantMemberrefInfo }
type ConstantInvokeDynamicInfo ¶
type ConstantInvokeDynamicInfo struct {
// contains filtered or unexported fields
}
ConstantInvokeDynamicInfo
CONSTANT_InvokeDynamic_info { u1 tag; u2 bootstrap_method_attr_index; u2 name_and_type_index; }
type ConstantLongInfo ¶
type ConstantLongInfo struct {
// contains filtered or unexported fields
}
ConstantLongInfo 64长整型引用
func (*ConstantLongInfo) Value ¶
func (self *ConstantLongInfo) Value() int64
type ConstantMemberrefInfo ¶
type ConstantMemberrefInfo struct {
// contains filtered or unexported fields
}
ConstantMemberrefInfo 包括方法成员和实例成员,接口 结构如下: *
_CONSTANT_Fieldref/Methodref_info{ tag u1 index u2 指向成员所在类或者接口的Class_info索引 index u2 指向成员的NameAndType索引 }
func (*ConstantMemberrefInfo) ClassName ¶
func (self *ConstantMemberrefInfo) ClassName() string
func (*ConstantMemberrefInfo) NameAndDescriptor ¶
func (self *ConstantMemberrefInfo) NameAndDescriptor() (string, string)
type ConstantMethodHandleInfo ¶
type ConstantMethodHandleInfo struct {
// contains filtered or unexported fields
}
ConstantMethodHandleInfo
CONSTANT_MethodHandle_info { u1 tag; u1 reference_kind; u2 reference_index; }
type ConstantMethodTypeInfo ¶
type ConstantMethodTypeInfo struct {
// contains filtered or unexported fields
}
ConstantMethodTypeInfo
CONSTANT_MethodType_info { u1 tag; u2 descriptor_index; }
type ConstantMethodrefInfo ¶
type ConstantMethodrefInfo struct{ ConstantMemberrefInfo }
type ConstantNameAndTypeInfo ¶
type ConstantNameAndTypeInfo struct {
// contains filtered or unexported fields
}
ConstantNameAndTypeInfo 该字段的结构如下: *
ConstantNameAndType_info{ tag u1 index u2 该字段的方法名称常量索引 index u2 该字段的描述符号常量索引 }
1)类型描述符。
①基本类型byte、short、char、int、long、float和double的描述符 是单个字母,分别对应B、S、C、I、J、F和D。注意,long的描述符是J 而不是L。 ②引用类型的描述符是L+类的完全限定名+分号。 ③数组类型的描述符是[+数组元素类型描述符。
2)字段描述符就是字段类型的描述符。 3)方法描述符是(分号分隔的参数类型描述符)+返回值类型描述符,其中void返回值由单个字母V表示。
标识字符 含义 B 基本类型byte C 基本类型char D 基本类型double F 基本类型float I 基本类型int J 基本类型long S 基本类型short Z 基本类型boolean V 特殊类型void L 对象类型,如Ljava/lang/Object; [ 数组类型,多个维度则有多个[
type ConstantPool ¶
type ConstantPool []ConstantInfo
ConstantPool * 常量池占据了class文件很大一部分数据,里面存放着各式各样 的常量信息,包括数字和字符串常量、类和接口名、字段和方法 名等等. 于常量池中常量的数量是不固定的,所以在常量池的入口需要放置一项u2类型的数据,代表常 量池容量计数值(constant_pool_count)。
常量池中每一项常量都是一个表.
常量表类型 标志值 描述 CONSTANT_Utf8 1 UTF-8编码的Unicode字符串 CONSTANT_Integer 3 int类型的字面值 CONSTANT_Float 4 float类型的字面值 CONSTANT_Long 5 long类型的字面值 CONSTANT_Double 6 double类型的字面值 CONSTANT_Class 7 对一个类或者是接口的符号引用 CONSTANT_String 8 String类型的字面值的引用 CONSTANT_Fieldref 9 对一个字段的符号 CONSTANT_Methodref 10 对一个类中方法的符号应用 CONSTANT_InterfaceMethodref 11 对一个接口中方法的符号引用 CONSTANT_NameAndType 12 对一个字段或方法的部分符号引用 CONSTANT_MethodHandle 15 对方法句柄的引用 CONSTANT_MethodType 16 对方法类型的引用 CONSTANT_Dynamic 17 对动态常量的引用 CONSTANT_InvokeDynamic 18 动态调用引用 ...
可以把常量池中的常量分为两类:字面量(literal)和符号引用 (symbolic reference)。字面量包括数字常量和字符串常量,符号引 用包括类和接口名、字段和方法信息等。除了字面量,其他常量都 是通过索引直接或间接指向CONSTANT_Utf8_info常量.
type ConstantStringInfo ¶
type ConstantStringInfo struct {
// contains filtered or unexported fields
}
ConstantStringInfo 字符串字面量常量 结构与Class_info一致 因此,方法如出一辙
func (*ConstantStringInfo) String ¶
func (self *ConstantStringInfo) String() string
type ConstantUtf8Info ¶
type ConstantUtf8Info struct {
// contains filtered or unexported fields
}
ConstantUtf8Info * UTF-8编码字符串 UTF-8_info结构:
UTF-8_info{ tag u1 length u2 bytes u1 }
用来存放常量池中用utf-8进行编码的字符,比如class类名,通过index索引指向该字符数组中的某个值
type ConstantValueAttribute ¶
type ConstantValueAttribute struct {
// contains filtered or unexported fields
}
ConstantValueAttribute *
ConstantValue_attribute { u2 attribute_name_index; 数量1 u4 attribute_length; 数量1 u2 constantvalue_index; 数量1 }
ConstantValue是一个定长属性。 attribute_name_index: attribute_length:固定为2 constantvalue_index:数据项代表了常量池中一个字面量常量的引用
long CONSTANT_Long_info, float CONSTANT_Float_info, double CONSTANT_Double_info, boolean,byte,char,short,int CONSTANT_Integer_info, java.lang.String CONSTANT_String_info.
ConstantValue属性的作用是通知虚拟机自动为静态变量赋值。 只有被static关键字修饰的变量(类变量)才可以使用这项属性。
而对于类变量,则有两种方式可以选择: 1. 在类构造器<clinit>()方法中 2. 使用ConstantValue属性。
目前的甲骨文的做法是: 对于被final与static同时修饰的基本数据类型或者java.lang.String,用ConstantValue属性进行赋值; 而对于其他的静态变量使用类构造器<clinit>()进行赋值。
由于ConstantValue属性的属性值只是常量池的一个索引,由于Class文件格式的常量类型中只有与基本属性和字符串相对应的字面量,所以ConstantValue只支持基本数据类型的常量。
func (*ConstantValueAttribute) ConstantValueIndex ¶
func (self *ConstantValueAttribute) ConstantValueIndex() uint16
type DeprecatedAttribute ¶
type DeprecatedAttribute struct{ MarkerAttribute }
DeprecatedAttribute Deprecated关键字用于标注不建议使用的类、方法等
type ExceptionTableEntry ¶
type ExceptionTableEntry struct {
// contains filtered or unexported fields
}
ExceptionTableEntry *
exception_table{ u2 start_pc; u2 end_pc; u2 handler_pc; u2 catch_type; }
异常表: 如果当字节码从第start_pc行[1]到第end_pc行之间(不含第end_pc行)出现了类型为catch_type或者其子类的异常 (catch_type为指向一个CONSTANT_Class_info型常量的索引),则转到第handler_pc行继续处理。 当catch_type的值为0时,代表任意异常情况都需要转到handler_pc处进行处理。
异常表实际上是Java代码的一部分,尽管字节码中有最初为处理异常而设计的跳转指令,但《Java 虚拟机规范》中明确要求Java语言的编译器应当选择使用异常表而不是通过跳转指令来实现Java异常及 finally处理机制
func (*ExceptionTableEntry) CatchType ¶
func (self *ExceptionTableEntry) CatchType() uint16
func (*ExceptionTableEntry) EndPc ¶
func (self *ExceptionTableEntry) EndPc() uint16
func (*ExceptionTableEntry) HandlerPc ¶
func (self *ExceptionTableEntry) HandlerPc() uint16
func (*ExceptionTableEntry) StartPc ¶
func (self *ExceptionTableEntry) StartPc() uint16
type ExceptionsAttribute ¶
type ExceptionsAttribute struct {
// contains filtered or unexported fields
}
ExceptionsAttribute *
Exceptions_attribute { u2 attribute_name_index; u4 attribute_length; u2 number_of_exceptions; u2 exception_index_table[number_of_exceptions]; }
Exceptions属性的作用是列举出方法中可能抛出的受查异常(Checked Exceptions),也 就是方法描述时在throws关键字后面列举的异常。 此属性中的number_of_exceptions项表示方法可能抛出number_of_exceptions种受查异常, 每一种受查异常使用一个exception_index_table项表示; exception_index_table是一个指向常量池中CONSTANT_Class_info型常量的索引,代表了该受查异常的类型。
func (*ExceptionsAttribute) ExceptionIndexTable ¶
func (self *ExceptionsAttribute) ExceptionIndexTable() []uint16
type LineNumberInfo ¶
type LineNumberInfo struct {
// contains filtered or unexported fields
}
LineNumberInfo * 字节码行号与Java源码行号对应关系:
line_number_info{ u2 start_pc; 字节码行号 u2 line_number; Java源码行号 }
type LineNumberTableAttribute ¶
type LineNumberTableAttribute struct {
// contains filtered or unexported fields
}
LineNumberTableAttribute *
LineNumberTable_attribute { u2 attribute_name_index; u4 attribute_length; u2 line_number_table_length; line_number_info line_number_table[line_number_table_length]; }
LineNumberTable属性用于描述Java源码行号与字节码行号(字节码的偏移量)之间的对应关系。 它并不是运行时必需的属性,但默认会生成到Class文件之中,可以在Javac中使用-g:none或-g:lines 选项来取消或要求生成这项信息。如果选择不生成LineNumberTable属性,对程序运行产生的最主要影 响就是当抛出异常时,堆栈中将不会显示出错的行号,并且在调试程序的时候,也无法按照源码行来 设置断点。
type LocalVariableInfo ¶
type LocalVariableInfo struct {
// contains filtered or unexported fields
}
LocalVariableInfo *
local_variable_info{ u2 start_pc u2 length u2 name_index u2 descriptor u2 index }
start_pc和length属性分别代表了这个局部变量的生命周期开始的字节码偏移量及其作用范围覆盖的长度, 两者结合起来就是这个局部变量在字节码之中的作用域范围。 name_index和descriptor_index都是指向常量池中CONSTANT_Utf8_info型常量的索引, 分别代表了局部变量的名称以及这个局部变量的描述符。 index是这个局部变量在栈帧的局部变量表中变量槽的位置。 当这个变量数据类型是64位类型时(double和long),它占用的变量槽为index和index+1两个。
type LocalVariableTableAttribute ¶
type LocalVariableTableAttribute struct {
// contains filtered or unexported fields
}
LocalVariableTableAttribute *
local_variable_table{ u2 attribute_name_index; u4 attribute_length; u2 local_variable_table_length local_variable_info local_variable_info[local_variable_table_length] }
LocalVariableTable属性用于描述栈帧中局部变量表的变量与Java源码中定义的变量之间的关系,它 也不是运行时必需的属性,但默认会生成到Class文件之中,可以在Javac中使用-g:none或-g:vars选项 来取消或要求生成这项信息。如果没有生成这项属性,最大的影响就是当其他人引用这个方法时,所 有的参数名称都将会丢失,譬如IDE将会使用诸如arg0、arg1之类的占位符代替原有的参数名,这对程 序运行没有影响,但是会对代码编写带来较大不便,而且在调试期间无法根据参数名称从上下文中获 得参数值。
type MarkerAttribute ¶
type MarkerAttribute struct{}
MarkerAttribute DeprecatedAttribute 与 SyntheticAttribute 通过继承该类实现
type MemberInfo ¶
type MemberInfo struct {
// contains filtered or unexported fields
}
MemberInfo * 实例成员的信息类 class文件中关于实例成员与方法成员的结构如下:
field_info { u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; }
func (*MemberInfo) AccessFlags ¶
func (self *MemberInfo) AccessFlags() uint16
func (*MemberInfo) CodeAttribute ¶
func (self *MemberInfo) CodeAttribute() *CodeAttribute
func (*MemberInfo) ConstantValueAttribute ¶
func (self *MemberInfo) ConstantValueAttribute() *ConstantValueAttribute
func (*MemberInfo) Descriptor ¶
func (self *MemberInfo) Descriptor() string
Descriptor Descriptor()从常量池查找字段或方法描述符
type SourceFileAttribute ¶
type SourceFileAttribute struct {
// contains filtered or unexported fields
}
SourceFileAttribute * SourceFile属性的结构如下:
SourceFile_attribute { u2 attribute_name_index; u4 attribute_length; u2 sourcefile_index; }
func (*SourceFileAttribute) FileName ¶
func (self *SourceFileAttribute) FileName() string
type SyntheticAttribute ¶
type SyntheticAttribute struct{ MarkerAttribute }
SyntheticAttribute Synthetic属性用来标记源文件中不存在、由编译器生成的类成 员,引入Synthetic属性主要是为了支持嵌套类和嵌套接口。
type UnparsedAttribute ¶
type UnparsedAttribute struct {
// contains filtered or unexported fields
}
Source Files ¶
- attr_code.go
- attr_constant_value.go
- attr_exception.go
- attr_line_number_table.go
- attr_local_variable_table.go
- attr_markers.go
- attr_source_file.go
- attr_unparsed.go
- attribute_info.go
- class_reader.go
- classfile.go
- constant_info.go
- constant_pool.go
- cp_class.go
- cp_invoke_dynamic.go
- cp_member_ref.go
- cp_name_and_type.go
- cp_numeric.go
- cp_string.go
- cp_utf8.go
- member_info.go