javaclassparser

package
v1.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2024 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CONSTANT_Class              = 7
	CONSTANT_String             = 8
	CONSTANT_Fieldref           = 9
	CONSTANT_Methodref          = 10
	CONSTANT_InterfaceMethodref = 11
	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
)

* 常量数据结构如下

cp_info {
	u1 tag; -> 用来区分常量类型
	u2 Info[];
}
View Source
const (
	ClassObjectType              = "ClassObject"
	MemberInfoType               = "MemberInfo"
	ConstantInteger              = "ConstantInteger"
	ConstantFloat                = "ConstantFloat"
	ConstantLong                 = "ConstantLong"
	ConstantDouble               = "ConstantDouble"
	ConstantUtf8                 = "ConstantUtf8"
	ConstantString               = "ConstantString"
	ConstantClass                = "ConstantClass"
	ConstantFieldref             = "ConstantFieldref"
	ConstantMethodref            = "ConstantMethodref"
	ConstantInterfaceMethodref   = "ConstantInterfaceMethodref"
	ConstantNameAndType          = "ConstantNameAndType"
	ConstantMethodType           = "ConstantMethodType"
	ConstantMethodHandle         = "ConstantMethodHandle"
	ConstantInvokeDynamic        = "ConstantInvokeDynamic"
	CodeAttributeType            = "CodeAttribute"
	ConstantValueAttributeType   = "ConstantValueAttribute"
	DeprecatedAttributeType      = "DeprecatedAttribute"
	ExceptionsAttributeType      = "ExceptionsAttribute"
	LineNumberTableAttributeType = "LineNumberTableAttribute"
	SourceFileAttributeType      = "SourceFileAttribute"
	SyntheticAttributeType       = "SyntheticAttribute"
	UnparsedAttributeType        = "UnparsedAttribute"
)

Variables

View Source
var ValueTypeError = utils.Error("error value type")

Functions

func AddVerboseAndType

func AddVerboseAndType(classObj *ClassObject, obj interface{})

func Bcel2bytes added in v1.2.3

func Bcel2bytes(becl string) ([]byte, error)

func GetMap

func GetMap() ([]int, []int)

func Interface2Uint64

func Interface2Uint64(v interface{}) (uint64, error)

Types

type AttributeInfo

type AttributeInfo interface {
	// contains filtered or unexported methods
}

* 属性表,储存了方法的字节码等信息

attribute_info {
	u2 attribute_name_index;
	u4 attribute_length;
	u1 Info[attribute_length];
}

type ClassObject

type ClassObject struct {
	//魔数 class的魔术 -> 0xCAFEBABE
	Type               string
	Magic              uint32
	MinorVersion       uint16
	MajorVersion       uint16
	ConstantPool       []ConstantInfo
	AccessFlags        uint16
	AccessFlagsVerbose []string
	ThisClass          uint16
	ThisClassVerbose   string
	SuperClass         uint16
	SuperClassVerbose  string
	Interfaces         []uint16
	InterfacesVerbose  []string
	Fields             []*MemberInfo
	Methods            []*MemberInfo
	Attributes         []AttributeInfo
}

func Parse

func Parse(classData []byte) (cf *ClassObject, err error)

func ParseFromBCEL

func ParseFromBCEL(data string) (cf *ClassObject, err error)

func ParseFromBase64

func ParseFromBase64(base string) (cf *ClassObject, err error)

func ParseFromFile

func ParseFromFile(path string) (cf *ClassObject, err error)

func ParseFromJson

func ParseFromJson(jsonData string) (cf *ClassObject, err error)

func (*ClassObject) Bcel

func (this *ClassObject) Bcel() (string, error)

func (*ClassObject) Bytes

func (this *ClassObject) Bytes() []byte
func (this *ClassObject) MarshalJSON() ([]byte, error) {
	js := CLassObjectJson{
		Version: fmt.Sprintf("%d.%d", this.MajorVersion, this.MinorVersion),
	}
	return json.MarshalIndent(js, "", " ")
}

func (*ClassObject) Dump

func (this *ClassObject) Dump() (string, error)

func (*ClassObject) FindConstStringFromPool

func (this *ClassObject) FindConstStringFromPool(v string) *ConstantUtf8Info

查找

func (*ClassObject) FindFields

func (this *ClassObject) FindFields(v string) *MemberInfo

func (*ClassObject) FindMethods

func (this *ClassObject) FindMethods(v string) *MemberInfo

func (*ClassObject) GetClassName

func (this *ClassObject) GetClassName() string

获取

func (*ClassObject) GetInterfacesName

func (this *ClassObject) GetInterfacesName() []string

func (*ClassObject) GetSupperClassName

func (this *ClassObject) GetSupperClassName() string

func (*ClassObject) Json

func (this *ClassObject) Json() (string, error)

func (*ClassObject) SetClassName

func (this *ClassObject) SetClassName(name string) error

SetClassName 修改类名

func (*ClassObject) SetMethodName added in v1.3.0

func (this *ClassObject) SetMethodName(old, name string) error

SetMethodName 设置函数名

func (*ClassObject) SetSourceFileName added in v1.3.0

func (this *ClassObject) SetSourceFileName(name string) error

SetSourceFileName 设置文件名

func (*ClassObject) ToBytesByCustomStringChar added in v1.3.2

func (this *ClassObject) ToBytesByCustomStringChar(charLength int) []byte

type ClassObjectBuilder

type ClassObjectBuilder struct {
	Errors []error
	// contains filtered or unexported fields
}

func NewClassObjectBuilder

func NewClassObjectBuilder(c *ClassObject) *ClassObjectBuilder

func (*ClassObjectBuilder) GetErrors

func (c *ClassObjectBuilder) GetErrors() []error

func (*ClassObjectBuilder) GetObject

func (c *ClassObjectBuilder) GetObject() *ClassObject

func (*ClassObjectBuilder) NewError

func (c *ClassObjectBuilder) NewError(msg string)

func (*ClassObjectBuilder) SetParam added in v1.2.4

func (c *ClassObjectBuilder) SetParam(k, v string) *ClassObjectBuilder

func (*ClassObjectBuilder) SetValue

func (c *ClassObjectBuilder) SetValue(old, new string) *ClassObjectBuilder

type ClassObjectDumper

type ClassObjectDumper struct {
	// contains filtered or unexported fields
}

func NewClassObjectDumper

func NewClassObjectDumper(obj *ClassObject) *ClassObjectDumper

func (*ClassObjectDumper) DumpClass

func (c *ClassObjectDumper) DumpClass() (string, error)

func (*ClassObjectDumper) DumpFields

func (c *ClassObjectDumper) DumpFields() ([]string, error)

func (*ClassObjectDumper) DumpMethods

func (c *ClassObjectDumper) DumpMethods() ([]string, error)

type ClassParser

type ClassParser struct {
	// contains filtered or unexported fields
}

func NewClassParser

func NewClassParser(data []byte) *ClassParser

func (*ClassParser) Parse

func (this *ClassParser) Parse() (*ClassObject, error)

type ClassReader

type ClassReader struct {
	// contains filtered or unexported fields
}

* jvm中定义了u1,u2,u4来表示1,2,4字节的 无 符号整数 相同类型的多条数据一般按表的形式存储在class文件中,由表头和表项构成,表头是u2或者u4整数。 假设表头为10,后面就紧跟着10个表项数据

func NewClassReader

func NewClassReader(data []byte) *ClassReader

type CodeAttribute

type CodeAttribute struct {
	Type           string
	AttrLen        uint32
	MaxStack       uint16
	MaxLocals      uint16
	Code           []byte
	ExceptionTable []*ExceptionTableEntry
	Attributes     []AttributeInfo
}

*

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 handle_pc;
		u2 catch_type;
	} exception_table[exception_table_length];
	u2 attributes_count;
	attribute_info Attributes[attributes_count]
}

type ConstantClassInfo

type ConstantClassInfo struct {
	Type             string
	ConstantType     string
	NameIndex        uint16
	NameIndexVerbose string
}

*

CONSTANT_CLASS_INFO {
	u1 tag;
	u2 name_index;
}

type ConstantDoubleInfo

type ConstantDoubleInfo struct {
	Type  string
	Value float64
}

* 常量池中double 同样特殊 八字节

CONSTANT_DOUBLE_INFO {
	u1 tag;
	u4 high_bytes;
	u4 low_bytes;
}

type ConstantFieldrefInfo

type ConstantFieldrefInfo struct {
	Type string
	ConstantMemberrefInfo
}

* 字段符号引用

CONSTANT_FIELDREF_INFO {
	u1 tag;
	u2 class_index;
	u2 name_and_type_index;
}

type ConstantFloatInfo

type ConstantFloatInfo struct {
	Type  string
	Value float32
}

* 常量池中float 四字节

CONSTANT_FLOAT_INFO {
	u1 tag;
	u4 bytes;
}

type ConstantInfo

type ConstantInfo interface {
	// contains filtered or unexported methods
}

* constant info类型的接口

type ConstantIntegerInfo

type ConstantIntegerInfo struct {
	Type string
	//实际上,比int小的boolean、byte、short、char也可以放在里面
	Value int32
}

* 常量池中integer 四字节存储整数常量

CONSTANT_INTEGER_INFO {
	u1 tag;
	u4 bytes;
}

type ConstantInterfaceMethodrefInfo

type ConstantInterfaceMethodrefInfo struct {
	Type string
	ConstantMemberrefInfo
}

* 接口方法符号引用

CONSTANT_INTERFACEMETHODREF_INFO {
	u1 tag;
	u2 class_index;
	u2 name_and_type_index;
}

type ConstantInvokeDynamicInfo

type ConstantInvokeDynamicInfo struct {
	Type                            string
	BootstrapMethodAttrIndex        uint16
	BootstrapMethodAttrIndexVerbose string
	NameAndTypeIndex                uint16
	NameAndTypeIndexVerbose         string
}
CONSTANT_InvokeDynamic_info {
    u1 tag;
    u2 bootstrap_method_attr_index;
    u2 name_and_type_index;
}

type ConstantLongInfo

type ConstantLongInfo struct {
	Type  string
	Value int64
}

* 常量池中long 特殊一些 八字节,分成高8字节和低8字节

CONSTANT_LONG_INFO {
	u1 tag;
	u4 high_bytes;
	u4 low_bytes;
}

type ConstantMemberrefInfo

type ConstantMemberrefInfo struct {
	ClassIndex              uint16
	ClassIndexVerbose       string
	NameAndTypeIndex        uint16
	NameAndTypeIndexVerbose string
}

* ConstantFieldrefInfo、ConstantMethodrefInfo、ConstantInterfaceMethodrefInfo 这三个结构体继承自ConstantMemberrefInfo Go语言没有“继承”的概念,而是通过结构体嵌套的方式实现的

type ConstantMethodHandleInfo

type ConstantMethodHandleInfo struct {
	Type                  string
	ReferenceKind         uint8
	ReferenceKindVerbose  string
	ReferenceIndex        uint16
	ReferenceIndexVerbose string
}
CONSTANT_MethodHandle_info {
    u1 tag;
    u1 reference_kind;
    u2 reference_index;
}

type ConstantMethodTypeInfo

type ConstantMethodTypeInfo struct {
	Type                   string
	DescriptorIndex        uint16
	DescriptorIndexVerbose string
}
CONSTANT_MethodType_info {
    u1 tag;
    u2 descriptor_index;
}

type ConstantMethodrefInfo

type ConstantMethodrefInfo struct {
	Type string
	ConstantMemberrefInfo
}

* 普通(非接口)方法符号引用

CONSTANT_METHODREF_INFO {
	u1 tag;
	u2 class_index;
	u2 name_and_type_index;
}

type ConstantNameAndTypeInfo

type ConstantNameAndTypeInfo struct {
	Type string
	//字段或方法名 指向一个CONSTANT_UTF8_INFO
	NameIndex        uint16
	NameIndexVerbose string
	//字段或方法的描述符 指向一个CONSTANT_UTF8_INFO
	DescriptorIndex        uint16
	DescriptorIndexVerbose string
}

* 给出字段或方法的名称和描述符

CONSTANT_NAMEANDTYPE_INFO {
	u1 tag;
	u2 name_index;
	u2 descriptor_index
}

type ConstantStringInfo

type ConstantStringInfo struct {
	Type               string
	StringIndex        uint16
	StringIndexVerbose string
}

* string info本身不存储字符串,只存了常量池索引,这个索引指向一个CONSTANT_UTF8_INFO。

CONSTANT_STRING_INFO {
	u1 tag;
	u2 string_index;
}

type ConstantUtf8Info

type ConstantUtf8Info struct {
	Type  string
	Value string
}

*

CONSTANT_UTF8_INFO {
	u1 tag;
	u2 Length;
	u1 bytes[Length];
}

type ConstantValueAttribute

type ConstantValueAttribute struct {
	Type                      string
	AttrLen                   uint32
	ConstantValueIndex        uint16
	ConstantValueIndexVerbose string
}

*

CONSTANTVALUE_ATTRIBUTE {
	u2 attribute_name_index;
	u4 attribute_length;
	u2 constantvalue_index;
}

type DeprecatedAttribute

type DeprecatedAttribute struct {
	AttrLen uint32
	MarkerAttribute
}

* 用于支持@Deprecated注解

type ExceptionTableEntry

type ExceptionTableEntry struct {
	StartPc   uint16
	EndPc     uint16
	HandlerPc uint16
	CatchType uint16
}

* 异常表

type ExceptionsAttribute

type ExceptionsAttribute struct {
	Type                string
	AttrLen             uint32
	ExceptionIndexTable []uint16
}

* 记录方法抛出的异常表

EXCEPTIONS_ATTRIBUTE {
	u2 attribute_name_index;
	u4 attribute_length;
	u2 number_of_exceptions;
	u2 exception_index_table[number_of_exceptions];
}

type JavaBufferWriter

type JavaBufferWriter struct {
	// contains filtered or unexported fields
}

func NewJavaBufferWrite

func NewJavaBufferWrite() *JavaBufferWriter

func (*JavaBufferWriter) Bytes

func (j *JavaBufferWriter) Bytes() []byte

func (*JavaBufferWriter) Write

func (j *JavaBufferWriter) Write(v []byte) error

func (*JavaBufferWriter) Write1Byte

func (j *JavaBufferWriter) Write1Byte(v interface{}) error

func (*JavaBufferWriter) Write2Byte

func (j *JavaBufferWriter) Write2Byte(v interface{}) error

func (*JavaBufferWriter) Write4Byte

func (j *JavaBufferWriter) Write4Byte(v interface{}) error

func (*JavaBufferWriter) Write8Byte

func (j *JavaBufferWriter) Write8Byte(v interface{}) error

func (*JavaBufferWriter) WriteHex

func (j *JavaBufferWriter) WriteHex(v interface{}) error

func (*JavaBufferWriter) WriteLString

func (j *JavaBufferWriter) WriteLString(v string) error

func (*JavaBufferWriter) WriteString

func (j *JavaBufferWriter) WriteString(v string) error

type LineNumberTableAttribute

type LineNumberTableAttribute struct {
	Type            string
	AttrLen         uint32
	LineNumberTable []*LineNumberTableEntry
}

* 存放方法的行号信息,是调试信息

LINE_NUMBER_TABLE_ATTRIBUTE {
	u2 attribute_name_index;
	u4 attribute_length;
	u2 line_number_table_length;
	{
		u2 start_pc;
		u2 lint_number;
	} line_number_table[line_number_table_length];
}

type LineNumberTableEntry

type LineNumberTableEntry struct {
	StartPc    uint16
	LineNumber uint16
}

type MarkerAttribute

type MarkerAttribute struct {
	Type string
}

* 上面两个struct的父类,其中没有任何数据

type MemberInfo

type MemberInfo struct {
	Type               string
	AccessFlags        uint16
	AccessFlagsVerbose []string
	NameIndex          uint16
	NameIndexVerbose   string
	//描述符
	DescriptorIndex        uint16
	DescriptorIndexVerbose string
	//属性表
	Attributes []AttributeInfo
}

* 字段/方法

type SourceFileAttribute

type SourceFileAttribute struct {
	Type                   string
	AttrLen                uint32
	SourceFileIndex        uint16
	SourceFileIndexVerbose string
}

源文件属性

type SyntheticAttribute

type SyntheticAttribute struct {
	AttrLen uint32
	MarkerAttribute
}

* 用来标记源文件中不存在的、由编译器生成的类成员,主要为了支持嵌套类(内部类)和嵌套接口

type UnparsedAttribute

type UnparsedAttribute struct {
	Type   string
	Name   string
	Length uint32
	Info   []byte
}

没解析的属性

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL