-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstant_pool.go
220 lines (177 loc) · 3.81 KB
/
constant_pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package parser
import (
"errors"
"fmt"
)
type ConstantPool struct {
Constants []Constant
}
var ErrNotFoundConstant = errors.New("not found constant")
func (c *ConstantPool) LookupUtf8(index uint16) *ConstantUtf8 {
i := int(index) - 1
if i < 0 {
return nil
} else if i > len(c.Constants) {
return nil
}
found := c.Constants[i]
if utf8, ok := found.(*ConstantUtf8); ok {
return utf8
}
return nil
}
func (c *ConstantPool) GetConstantUtf8(index uint16) (*ConstantUtf8, error) {
i := int(index) - 1
if i < 0 || i > len(c.Constants) {
return nil, ErrNotFoundConstant
}
clazz, ok := c.Constants[i].(*ConstantUtf8)
if !ok {
return nil, fmt.Errorf("unexpected constant. expected:ConstantUtf8, actual: %T", c.Constants[i])
}
return clazz, nil
}
func (c *ConstantPool) GetClassInfo(index uint16) (*ConstantClass, error) {
i := int(index) - 1
if i < 0 || i > len(c.Constants) {
return nil, ErrNotFoundConstant
}
clazz, ok := c.Constants[i].(*ConstantClass)
if !ok {
return nil, fmt.Errorf("unexpected constant. expected:ConstantClass, actual: %T", c.Constants[i])
}
return clazz, nil
}
func (c *ConstantPool) GetClassName(classNameIndex uint16) (string, error) {
i := int(classNameIndex)
if i < 1 || i > len(c.Constants) {
return "", ErrNotFoundConstant
}
clazz, err := c.GetClassInfo(classNameIndex)
if err != nil {
return "", err
}
name, err := c.GetConstantUtf8(clazz.NameIndex)
if err != nil {
return "", err
}
return name.String(), nil
}
type Constant interface {
Name() string
}
type ConstantClass struct {
NameIndex uint16
}
func (c *ConstantClass) Name() string {
return "Class"
}
type ConstantFieldref struct {
ClassIndex uint16
NameAndTypeIndex uint16
}
func (c *ConstantFieldref) Name() string {
return "Fieldref"
}
type ConstantMethodref struct {
ClassIndex uint16
NameAndTypeIndex uint16
}
func (c *ConstantMethodref) Name() string {
return "Methodref"
}
type ConstantInterfaceMethodref struct {
ClassIndex uint16
NameAndTypeIndex uint16
}
func (c *ConstantInterfaceMethodref) Name() string {
return "InterfaceMethodref"
}
type ConstantString struct {
StringIndex uint16
}
func (c *ConstantString) Name() string {
return "String"
}
type ConstantInteger struct {
Bytes uint32
}
func (c *ConstantInteger) Name() string {
return "Integer"
}
type ConstantFloat struct {
Bytes uint32
}
func (c *ConstantFloat) Name() string {
return "Float"
}
type ConstantLong struct {
HighBytes uint32
LowBytes uint32
}
func (c *ConstantLong) Name() string {
return "Long"
}
type ConstantDouble struct {
HighBytes uint32
LowBytes uint32
}
func (c *ConstantDouble) Name() string {
return "Double"
}
type ConstantNameAndType struct {
NameIndex uint16
DescriptorIndex uint16
}
func (c *ConstantNameAndType) Name() string {
return "NameAndType"
}
type ConstantUtf8 struct {
Length uint16
Bytes []byte
}
func (c *ConstantUtf8) Name() string {
return "Utf8"
}
func (c *ConstantUtf8) String() string {
return string(c.Bytes)
}
type ConstantMethodHandle struct {
ReferenceKind uint8
ReferenceIndex uint16
}
func (c *ConstantMethodHandle) Name() string {
return "MethodHandle"
}
type ConstantMethodType struct {
DescriptorIndex uint16
}
func (c *ConstantMethodType) Name() string {
return "MethodType"
}
type ConstantDynamic struct {
BootstrapMethodAttrIndex uint16
NameAndTypeIndex uint16
}
func (c *ConstantDynamic) Name() string {
return "Dynamic"
}
type ConstantInvokeDynamic struct {
BootstrapMethodAttrIndex uint16
NameAndTypeIndex uint16
}
func (c *ConstantInvokeDynamic) Name() string {
return "InvokeDynamic"
}
type ConstantModule struct {
NameIndex uint16
}
func (c *ConstantModule) Name() string {
return "Module"
}
type ConstantPackage struct {
NameIndex uint16
}
func (c *ConstantPackage) Name() string {
return "Package"
}