Skip to content

Commit

Permalink
Merge pull request #11 from kekeon/master
Browse files Browse the repository at this point in the history
parser constant pool
  • Loading branch information
ciscoxll authored Dec 18, 2018
2 parents d736638 + bf5da49 commit 08d693f
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 27 deletions.
5 changes: 5 additions & 0 deletions jvm.com/classfile/attribute-info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package classfile

type AttributeInfo interface {
readInfo(reader *ClassReader)
}
18 changes: 18 additions & 0 deletions jvm.com/classfile/constant-info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package classfile

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
)
94 changes: 94 additions & 0 deletions jvm.com/classfile/constant-pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package classfile

type ConstantInfo interface {
readInfo(read *ClassReader)
}

type ConstantPool []ConstantInfo

func readConstantPool(reader *ClassReader) ConstantPool {
cpCount := int(reader.readUint16())

cp := make([]ConstantInfo, cpCount)

for i := 1; i < cpCount; i++ {
cp[i] = readConstantInfo(reader, cp)

switch cp[i].(type) {
case *ConstantLongInfo, *ConstantDoubleInfo:

i++
}
}

return cp
}

func (self ConstantPool) getConstantInfo(index uint16) ConstantInfo {
if cpInfo := self[index]; cpinfo != nil {
return cpInfo
}

panic("Invalid constant pool index")
}

func (self *ConstantPool) getNameAndType(index uint16) (string, string) {
ntInfo := self.getConstantInfo(index).(*ConstantNameAndTypeInfo)
name := self.getUtf8(ntInfo.nameIndex)
_type := self.getUtf8(ngInfo.descriptorIndex)

return name, _type
}

func (self *ConstantPool) getClassName(index uint16) string {
return self.getUtf8(classInfo.nameIndex)
}

func (self *ConstantPool) getUtf8(index uint16) string {
utf8Info := self.getContantInfo(index).(*ConstantUtf8Info)

return utf8Info.str
}

func readConstantInfo(reader *ClassReader, cp ConstantPool) ConstantInfo {
tag := reader.readUint8()
c := newConstantInfo(tag, cp)
reader.readInfo(reader)

return c
}

func newConstantInfo(tag uint8, cp ConstantPool) ConstantInfo {
switch tag {
case CONSTANT_Integer:
return &ConstantIntegerInfo{}
case CONSTANT_FLOAD:
return &ConstantFloatInfo{}
case CONSTANT_lONG:
return &ConstantLongInfo{}
case CONSTANT_DOUBLE:
return &ConstantDouble{}
case CONSTANT_Utf8:
return &ConstantUft8Info{}
case CONSTANT_String:
return &ConstantStringInfo{cp: cp}
case CONSTANT_Class:
return &ConstantClassInfo{cp: cp}
case CONSTANT_Fieldref:
return &ConstantFieldrefInfo{ConstantMemberrefInfo{cp: cp}}
case CONSTANT_Methodref:
return &ConstantMethodrefInfo{ConstantMemberrefInfo{cp: cp}}
case CONSTANT_InterfaceMethodref:
return &ConstantInterfaceMethodrefInfo{ConstantMemberrefInfo{cp: cp}}
case CONSTANT_NameAndType:
return &ConstantNameAndTypeInfo{}
case CONSTANT_MethodType:
return &ConstantMethodTypeInfo{}
case CONSTANT_MethodHandle:
return &ConstantMethodHandleInfo{}
case CONSTANT_InvokeDynamic:
return &ConstantInvokeDynamicInfo{}
default:
panic("java.lang.ClassFormatError: constant pool tag!")
}
}
27 changes: 0 additions & 27 deletions jvm.com/classfile/constant_pool.go

This file was deleted.

40 changes: 40 additions & 0 deletions jvm.com/classfile/cp-numeric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package classfile

import "math"

type ConstantIntegerInfo struct {
val int32
}

type ConstantFloatInfo struct {
val float32
}

type ConstantLongInfo struct {
val int64
}

type ConstantDoubleInfo struct {
val float64
}

func (self *ConstantIntegerInfo) readInfo(reader *ClassReader) {
bytes := reader.readUint32()
self.val = int32(bytes)
}

func (self *ConstantFloatInfo) readInfo(reader *ClassReader) {
bytes := reader.readUint32()
self.val = math.Float32frombits(bytes)
}

func (self *ConstantLongInfo) readInfo(reader *ClassReader) {
bytes := reader.readUint64()

self.val = int64(bytes)
}

func (self *ConstantDoubleInfo) readInfo(reader *ClassReader) {
bytes := reader.readUint64()
self.val = math.Float64frombits(bytes)
}
40 changes: 40 additions & 0 deletions jvm.com/classfile/member-info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package classfile

type MemberInfo struct {
cp ConstantPool
accessFlags uint16
nameIndex uint16
descriptorIndex uint16
attributes []AttributeInfo
}

func readMemberInfo(reader *ClassReader, cp ConstantPool) []*MemberInfo {
memberCount := reader.readUint16()
members := make([]*MemberInfo, memberCount)

for i := range members {
members[i] = readMember(reader, cp)
}

return members
}

func readMember(reader *ClassReader, cp ConstantPool) *MemberInfo {
return &MemberInfo{
cp: cp,
accessFlags: reader.readUint16(),
nameIndex: reader.readUint16(),
attributes: readAttributes(reader, cp),
}
}

func (self *MemberInfo) AccessFlags() uint16 {
}

func (self *MemberInfo) Name() string {
return self.cp.getUtf8(self.descriptorIndex)
}

func (self *MemberInfo) Descriptor() string {
return self.cp.getUtf8(self.descriptorIndex)
}

0 comments on commit 08d693f

Please sign in to comment.