/
constant_pool.go
347 lines (264 loc) · 7.87 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package jclass
import (
"encoding/binary"
"fmt"
"io"
"unicode/utf16"
)
type ConstantPoolInfo struct {
Tag uint8
Info []byte
}
func (i *ConstantPoolInfo) String() string {
switch i.Tag {
case 7:
return fmt.Sprintf("%s", (*ConstantClassInfo)(i))
case 9:
return fmt.Sprintf("%s", (*ConstantFieldrefInfo)(i))
case 10:
return fmt.Sprintf("%s", (*ConstantMethodrefInfo)(i))
case 11:
return fmt.Sprintf("%s", (*ConstantInterfaceMethodrefInfo)(i))
case 8:
return fmt.Sprintf("%s", (*ConstantStringInfo)(i))
case 3:
return fmt.Sprintf("%s", (*ConstantIntegerInfo)(i))
case 4:
return fmt.Sprintf("%s", (*ConstantFloatInfo)(i))
case 5:
return fmt.Sprintf("%s", (*ConstantLongInfo)(i))
case 6:
return fmt.Sprintf("%s", (*ConstantDoubleInfo)(i))
case 12:
return fmt.Sprintf("%s", (*ConstantNameAndTypeInfo)(i))
case 1:
return fmt.Sprintf("%s", (*ConstantUtf8Info)(i))
case 15:
return fmt.Sprintf("%s", (*ConstantMethodHandleInfo)(i))
case 16:
return fmt.Sprintf("%s", (*ConstantMethodTypeInfo)(i))
case 18:
return fmt.Sprintf("%s", (*ConstantInvokeDynamicInfo)(i))
default:
panic(fmt.Errorf("invalid tag: %d", i.Tag))
}
}
func NewConstantPoolInfo(r io.Reader, buf []byte) (*ConstantPoolInfo, []byte, error) {
rs := ConstantPoolInfo{}
_, err := io.ReadFull(r, buf[:1])
if err != nil {
return nil, buf, err
}
rs.Tag = buf[0]
switch rs.Tag {
case 7, 8, 16:
rs.Info, err = readEnoughBytes(r, buf, 2)
if err != nil {
return nil, buf, err
}
case 3, 4, 9, 10, 11, 12, 18:
rs.Info, err = readEnoughBytes(r, buf, 4)
if err != nil {
return nil, buf, err
}
case 5, 6:
rs.Info, err = readEnoughBytes(r, buf, 8)
if err != nil {
return nil, buf, err
}
case 1:
_, err = io.ReadFull(r, buf[:2])
if err != nil {
return nil, buf, err
}
length := binary.BigEndian.Uint16(buf)
bufSize := 2 + length
if len(buf) < int(bufSize) {
newBuf := make([]byte, bufSize)
copy(newBuf[:2], buf)
buf = newBuf
}
_, err = io.ReadFull(r, buf[2:bufSize])
if err != nil {
return nil, buf, err
}
rs.Info = make([]byte, bufSize)
copy(rs.Info, buf)
case 15:
rs.Info, err = readEnoughBytes(r, buf, 3)
if err != nil {
return nil, buf, err
}
default:
panic(fmt.Errorf("invalid constant pool tag: %d", rs.Tag))
}
return &rs, buf, nil
}
// CONSTANT_Class 7
type ConstantClassInfo ConstantPoolInfo
func (i *ConstantClassInfo) NameIndex() uint16 {
return binary.BigEndian.Uint16(i.Info)
}
func (i *ConstantClassInfo) String() string {
return fmt.Sprintf("ConstantClassInfo [NameIndex:%d]", i.NameIndex())
}
// CONSTANT_Fieldref 9
type ConstantFieldrefInfo ConstantPoolInfo
func (i *ConstantFieldrefInfo) ClassIndex() uint16 {
return binary.BigEndian.Uint16(i.Info)
}
func (i *ConstantFieldrefInfo) NameAndTypeIndex() uint16 {
return binary.BigEndian.Uint16(i.Info[2:])
}
func (i *ConstantFieldrefInfo) String() string {
return fmt.Sprintf("ConstantFieldrefInfo [ClassIndex:%d, NameAndTypeIndex:%d]",
i.ClassIndex(), i.NameAndTypeIndex())
}
// CONSTANT_Methodref 10
type ConstantMethodrefInfo ConstantPoolInfo
func (i *ConstantMethodrefInfo) ClassIndex() uint16 {
return binary.BigEndian.Uint16(i.Info)
}
func (i *ConstantMethodrefInfo) NameAndTypeIndex() uint16 {
return binary.BigEndian.Uint16(i.Info[2:])
}
func (i *ConstantMethodrefInfo) String() string {
return fmt.Sprintf("ConstantMethodrefInfo [ClassIndex: %d, NameAndTypeIndex: %d]",
i.ClassIndex(), i.NameAndTypeIndex())
}
// CONSTANT_InterfaceMethodref 11
type ConstantInterfaceMethodrefInfo ConstantPoolInfo
func (i *ConstantInterfaceMethodrefInfo) ClassIndex() uint16 {
return binary.BigEndian.Uint16(i.Info)
}
func (i *ConstantInterfaceMethodrefInfo) NameAndTypeIndex() uint16 {
return binary.BigEndian.Uint16(i.Info[2:])
}
func (i *ConstantInterfaceMethodrefInfo) String() string {
return fmt.Sprintf("ConstantInterfaceMethodrefInfo [ClassIndex: %d, NameAndTypeIndex: %d]",
i.ClassIndex(), i.NameAndTypeIndex())
}
// CONSTANT_String 8
type ConstantStringInfo ConstantPoolInfo
func (i *ConstantStringInfo) StringIndex() uint16 {
return binary.BigEndian.Uint16(i.Info)
}
func (i *ConstantStringInfo) String() string {
return fmt.Sprintf("ConstantStringInfo [StringIndex: %d]", i.StringIndex())
}
// CONSTANT_Integer 3
type ConstantIntegerInfo ConstantPoolInfo
func (i *ConstantIntegerInfo) Integer() int32 {
return uint32Toint32(binary.BigEndian.Uint32(i.Info))
}
func (i *ConstantIntegerInfo) String() string {
return fmt.Sprintf("ConstantIntegerInfo [value: %d]", i.Integer())
}
// CONSTANT_Float 4
type ConstantFloatInfo ConstantPoolInfo
func (i *ConstantFloatInfo) Float() float32 {
return uint32ToFloat32(binary.BigEndian.Uint32(i.Info))
}
func (i *ConstantFloatInfo) String() string {
return fmt.Sprintf("ConstantFloatInfo [value: %.7f]", i.Float())
}
// CONSTANT_Long 5
type ConstantLongInfo ConstantPoolInfo
func (i *ConstantLongInfo) Long() int64 {
return uint64ToInt64(binary.BigEndian.Uint64(i.Info))
}
func (i *ConstantLongInfo) String() string {
return fmt.Sprintf("ConstantLongInfo [value: %d]", i.Long())
}
// CONSTANT_Double 6
type ConstantDoubleInfo ConstantPoolInfo
func (i *ConstantDoubleInfo) Double() float64 {
return uint64ToFloat64(binary.BigEndian.Uint64(i.Info))
}
func (i *ConstantDoubleInfo) String() string {
return fmt.Sprintf("ConstantDoubleInfo [value: %.16f]", i.Double())
}
// CONSTANT_NameAndType 12
type ConstantNameAndTypeInfo ConstantPoolInfo
func (i *ConstantNameAndTypeInfo) NameIndex() uint16 {
return binary.BigEndian.Uint16(i.Info)
}
func (i *ConstantNameAndTypeInfo) DescriptorIndex() uint16 {
return binary.BigEndian.Uint16(i.Info[2:])
}
func (i *ConstantNameAndTypeInfo) String() string {
return fmt.Sprintf("ConstantNameAndTypeInfo [NameIndex: %d, DescriptorIndex: %d]",
i.NameIndex(), i.DescriptorIndex())
}
// CONSTANT_Utf8 1
type ConstantUtf8Info ConstantPoolInfo
func (i *ConstantUtf8Info) Length() uint16 {
return binary.BigEndian.Uint16(i.Info)
}
func (i *ConstantUtf8Info) Bytes() []byte {
return i.Info[2:]
}
func (info *ConstantUtf8Info) Utf8() string {
ch := make([]uint16, 0, 512)
var c, cc uint16
buf := info.Info[2:]
var st int
for i, length := 0, len(buf); i < length; i++ {
c = uint16(buf[i])
switch st {
case 0:
if c < 0x80 {
ch = append(ch, c)
} else if c < 0xE0 && c > 0xBF {
cc = c & 0x1F
st = 1
} else {
cc = c & 0x0F
st = 2
}
case 1:
ch = append(ch, (cc<<6)|(c&0x3F))
st = 0
case 2:
cc = (cc << 6) | (c & 0x3F)
st = 1
}
}
return string(utf16.Decode(ch))
}
func (i *ConstantUtf8Info) String() string {
return fmt.Sprintf("ConstantUtf8Info [len: %d, utf8: %s]",
i.Length(), i.Utf8())
}
// CONSTANT_MethodHandle 15
type ConstantMethodHandleInfo ConstantPoolInfo
func (i *ConstantMethodHandleInfo) ReferenceKind() uint8 {
return i.Info[0]
}
func (i *ConstantMethodHandleInfo) ReferenceIndex() uint16 {
return binary.BigEndian.Uint16(i.Info[1:])
}
func (i *ConstantMethodHandleInfo) String() string {
return fmt.Sprintf("ConstantMethodHandleInfo [ReferenceKind: %d, ReferenceIndex: %d]",
i.ReferenceKind(), i.ReferenceIndex())
}
// CONSTANT_MethodType 16
type ConstantMethodTypeInfo ConstantPoolInfo
func (i *ConstantMethodTypeInfo) DescriptorIndex() uint16 {
return binary.BigEndian.Uint16(i.Info)
}
func (i *ConstantMethodTypeInfo) String() string {
return fmt.Sprintf("ConstantMethodTypeInfo [DescriptorIndex: %d]", i.DescriptorIndex())
}
// CONSTANT_InvokeDynamic 18
type ConstantInvokeDynamicInfo ConstantPoolInfo
func (i *ConstantInvokeDynamicInfo) BootstrapMethodAttrIndex() uint16 {
return binary.BigEndian.Uint16(i.Info)
}
func (i *ConstantInvokeDynamicInfo) NameAndTypeIndex() uint16 {
return binary.BigEndian.Uint16(i.Info[2:])
}
func (i *ConstantInvokeDynamicInfo) String() string {
return fmt.Sprintf("ConstantInvokeDynamicInfo [BootstrapMethodAttrIndex: %d, NameAndTypeIndex: %d]",
i.BootstrapMethodAttrIndex(), i.NameAndTypeIndex())
}