-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathConstantPool.php
More file actions
167 lines (152 loc) Β· 5.2 KB
/
ConstantPool.php
File metadata and controls
167 lines (152 loc) Β· 5.2 KB
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
<?php
declare(strict_types=1);
namespace PHPJava\Core\JVM;
use PHPJava\Core\Stream\Reader\ReaderInterface;
use PHPJava\Exceptions\ReadEntryException;
use PHPJava\Exceptions\ReadOnlyException;
use PHPJava\Exceptions\RuntimeException;
use PHPJava\Kernel\Maps\ConstantPoolTag;
use PHPJava\Kernel\Structures\ClassInfo;
use PHPJava\Kernel\Structures\DoubleInfo;
use PHPJava\Kernel\Structures\FieldrefInfo;
use PHPJava\Kernel\Structures\FloatInfo;
use PHPJava\Kernel\Structures\FreezableInterface;
use PHPJava\Kernel\Structures\IntegerInfo;
use PHPJava\Kernel\Structures\InterfaceMethodrefInfo;
use PHPJava\Kernel\Structures\InvokeDynamicInfo;
use PHPJava\Kernel\Structures\LongInfo;
use PHPJava\Kernel\Structures\MethodHandleInfo;
use PHPJava\Kernel\Structures\MethodrefInfo;
use PHPJava\Kernel\Structures\MethodTypeInfo;
use PHPJava\Kernel\Structures\NameAndTypeInfo;
use PHPJava\Kernel\Structures\StringInfo;
use PHPJava\Kernel\Structures\StructureInterface;
use PHPJava\Kernel\Structures\Utf8Info;
class ConstantPool implements \ArrayAccess, \Countable, \IteratorAggregate
{
/**
* @var StructureInterface[]
*/
private $entries = [];
/**
* @var ReaderInterface
*/
private $reader;
/**
* @throws ReadEntryException
*/
public function __construct(ReaderInterface $reader, int $entries)
{
$this->reader = $reader;
for ($i = 1; $i < $entries; $i++) {
$this->entries[$i] = $this->read(
$reader->getReader()->readUnsignedByte()
);
// execute
$this->entries[$i]->execute();
// Java's Long and Double problem.
if ($this->entries[$i] instanceof LongInfo ||
$this->entries[$i] instanceof DoubleInfo) {
$i++;
}
}
}
/**
* @return StructureInterface[]
*/
public function getEntries(): array
{
return $this->entries;
}
/**
* @throws ReadEntryException
*/
private function read(int $entryTag): ?StructureInterface
{
switch ($entryTag) {
case ConstantPoolTag::CONSTANT_Class:
return new ClassInfo($this->reader);
case ConstantPoolTag::CONSTANT_Fieldref:
return new FieldrefInfo($this->reader);
case ConstantPoolTag::CONSTANT_Methodref:
return new MethodrefInfo($this->reader);
case ConstantPoolTag::CONSTANT_String:
return new StringInfo($this->reader);
case ConstantPoolTag::CONSTANT_Integer:
return new IntegerInfo($this->reader);
case ConstantPoolTag::CONSTANT_Float:
return new FloatInfo($this->reader);
case ConstantPoolTag::CONSTANT_Long:
return new LongInfo($this->reader);
case ConstantPoolTag::CONSTANT_Double:
return new DoubleInfo($this->reader);
case ConstantPoolTag::CONSTANT_NameAndType:
return new NameAndTypeInfo($this->reader);
case ConstantPoolTag::CONSTANT_Utf8:
return new Utf8Info($this->reader);
case ConstantPoolTag::CONSTANT_InterfaceMethodref:
return new InterfaceMethodrefInfo($this->reader);
case ConstantPoolTag::CONSTANT_InvokeDynamic:
return new InvokeDynamicInfo($this->reader);
case ConstantPoolTag::CONSTANT_MethodHandle:
return new MethodHandleInfo($this->reader);
case ConstantPoolTag::CONSTANT_MethodType:
return new MethodTypeInfo($this->reader);
case ConstantPoolTag::CONSTANT_Module:
case ConstantPoolTag::CONSTANT_Package:
throw new ReadEntryException('Entry tag ' . sprintf('0x%04X', $entryTag) . ' is not implemented.');
}
throw new ReadEntryException('Entry tag ' . sprintf('0x%04X', $entryTag) . ' is not defined.');
}
/**
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->entries[$offset]);
}
/**
* @throws RuntimeException
* @return StructureInterface
*/
public function offsetGet($offset)
{
if ($this->entries[$offset] instanceof FreezableInterface) {
$this->entries[$offset]->freeze();
}
if (!array_key_exists($offset, $this->entries)) {
throw new RuntimeException(
'Cannot refer to an entry on the Constant Pool (index: ' . $offset . ')'
);
}
return $this->entries[$offset];
}
/**
* @return int
*/
public function count()
{
return count($this->entries);
}
/**
* @throws ReadOnlyException
*/
public function offsetSet($offset, $value)
{
throw new ReadOnlyException('You cannot rewrite datum. The Constant Pool is read-only.');
}
/**
* @throws ReadOnlyException
*/
public function offsetUnset($offset)
{
throw new ReadOnlyException('You cannot rewrite datum. The Constant Pool is read-only.');
}
/**
* @return \ArrayIterator<StructureInterface>
*/
public function getIterator()
{
return new \ArrayIterator($this->entries);
}
}