Skip to content

Commit 0aaf36b

Browse files
committed
Use ArrayAccess instead of getEntries on ConstantPool
1 parent d1e7df0 commit 0aaf36b

17 files changed

+44
-17
lines changed

src/Core/JVM/ConstantPool.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use PHPJava\Core\Stream\Reader\ReaderInterface;
55
use PHPJava\Exceptions\ReadEntryException;
6+
use PHPJava\Exceptions\ReadOnlyException;
67
use PHPJava\Kernel\Maps\ConstantPoolTag;
78
use PHPJava\Kernel\Structures\_Class;
89
use PHPJava\Kernel\Structures\_Double;
@@ -19,7 +20,7 @@
1920
use PHPJava\Kernel\Structures\_Utf8;
2021
use PHPJava\Kernel\Structures\StructureInterface;
2122

22-
class ConstantPool
23+
class ConstantPool implements \ArrayAccess
2324
{
2425
private $entries = [];
2526
private $reader;
@@ -90,4 +91,24 @@ private function read($entryTag): ?StructureInterface
9091
}
9192
throw new ReadEntryException('Entry tag ' . sprintf('0x%04X', $entryTag) . ' is not defined.');
9293
}
94+
95+
public function offsetExists($offset)
96+
{
97+
return isset($this->entries[$offset]);
98+
}
99+
100+
public function offsetGet($offset)
101+
{
102+
return $this->entries[$offset];
103+
}
104+
105+
public function offsetSet($offset, $value)
106+
{
107+
throw new ReadOnlyException('You cannot rewrite datum. The Constant Pool is read-only.');
108+
}
109+
110+
public function offsetUnset($offset)
111+
{
112+
throw new ReadOnlyException('You cannot rewrite datum. The Constant Pool is read-only.');
113+
}
93114
}

src/Core/JavaClass.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function __construct(ReaderInterface $reader, array $options = [])
138138
// read super class
139139
$this->superClassIndex = $reader->getBinaryReader()->readUnsignedShort();
140140

141-
$cpInfo = $this->getConstantPool()->getEntries();
141+
$cpInfo = $this->getConstantPool();
142142
[$resolvedType, $superClass] = ClassResolver::resolve(
143143
$cpInfo[$cpInfo[$this->superClassIndex]->getClassIndex()]->getString(),
144144
$this
@@ -283,7 +283,7 @@ public function getSuperClass()
283283

284284
public function debug(): void
285285
{
286-
$cpInfo = $this->getConstantPool()->getEntries();
286+
$cpInfo = $this->getConstantPool();
287287
foreach ($this->debugTraces as $debugTraces) {
288288
printf("[method]\n");
289289
printf(Formatter::beatifyMethodFromConstantPool($debugTraces['method'], $this->getConstantPool()) . "\n");

src/Core/JavaClassInvoker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct(JavaClass $javaClass, array $options)
5353
{
5454
$this->javaClass = $javaClass;
5555
$this->options = $options;
56-
$cpInfo = $javaClass->getConstantPool()->getEntries();
56+
$cpInfo = $javaClass->getConstantPool();
5757

5858
foreach ($javaClass->getMethods() as $methodInfo) {
5959
/**
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace PHPJava\Exceptions;
3+
4+
class ReadOnlyException extends \Exception
5+
{
6+
}

src/Kernel/Attributes/AttributeInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function execute(): void
2121
{
2222
$this->attributeNameIndex = $this->readUnsignedShort();
2323
$this->attributeLength = $this->readUnsignedInt();
24-
$cpInfo = $this->getConstantPool()->getEntries();
24+
$cpInfo = $this->getConstantPool();
2525
$currentOffset = $this->getOffset();
2626

2727
$attributeName = $cpInfo[$this->attributeNameIndex]->getString();

src/Kernel/Mnemonics/_athrow.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class _athrow implements OperationInterface
1111

1212
public function execute(): void
1313
{
14-
$cpInfo = $this->getConstantPool()->getEntries();
14+
$cpInfo = $this->getConstantPool();
1515

1616
$objectref = $this->popFromOperandStack();
1717

src/Kernel/Mnemonics/_getfield.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class _getfield implements OperationInterface
1111

1212
public function execute(): void
1313
{
14-
$cpInfo = $this->getConstantPool()->getEntries();
14+
$cpInfo = $this->getConstantPool();
1515

1616
$cp = $cpInfo[$this->readUnsignedShort()];
1717

src/Kernel/Mnemonics/_getstatic.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class _getstatic implements OperationInterface
1414

1515
public function execute(): void
1616
{
17-
$cpInfo = $this->getConstantPool()->getEntries();
17+
$cpInfo = $this->getConstantPool();
1818

1919
$cp = $cpInfo[$this->readUnsignedShort()];
2020
$class = $cpInfo[$cpInfo[$cp->getClassIndex()]->getClassIndex()]->getString();

src/Kernel/Mnemonics/_invokespecial.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class _invokespecial implements OperationInterface
1414

1515
public function execute(): void
1616
{
17-
$cpInfo = $this->getConstantPool()->getEntries();
17+
$cpInfo = $this->getConstantPool();
1818
$cp = $cpInfo[$this->readUnsignedShort()];
1919
$nameAndTypeIndex = $cpInfo[$cp->getNameAndTypeIndex()];
2020
$signature = $cpInfo[$nameAndTypeIndex->getDescriptorIndex()]->getString();

src/Kernel/Mnemonics/_invokestatic.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class _invokestatic implements OperationInterface
1313

1414
public function execute(): void
1515
{
16-
$cpInfo = $this->getConstantPool()->getEntries();
16+
$cpInfo = $this->getConstantPool();
1717
$cp = $cpInfo[$this->readUnsignedShort()];
1818
$methodName = $cpInfo[$cpInfo[$cp->getNameAndTypeIndex()]->getNameIndex()]->getString();
1919
$signature = Formatter::parseSignature($cpInfo[$cpInfo[$cp->getNameAndTypeIndex()]->getDescriptorIndex()]->getString());

0 commit comments

Comments
 (0)