-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from kekeon/master
parser constant pool
- Loading branch information
Showing
6 changed files
with
197 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package classfile | ||
|
||
type AttributeInfo interface { | ||
readInfo(reader *ClassReader) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |