Skip to content

Commit b276c67

Browse files
committed
Implement the intern
1 parent fd59c56 commit b276c67

4 files changed

Lines changed: 58 additions & 25 deletions

File tree

src/Core/JVM/ConstantPool.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class ConstantPool implements \ArrayAccess, \Countable, \IteratorAggregate
2525
{
2626
private $entries = [];
2727
private $reader;
28-
private $isWritable = false;
2928

3029
/**
3130
* @param ReaderInterface $reader
@@ -52,16 +51,6 @@ public function __construct(ReaderInterface $reader, int $entries)
5251
}
5352
}
5453

55-
/**
56-
* @param bool $enable
57-
* @return ConstantPool
58-
*/
59-
public function enableWrite(bool $enable): self
60-
{
61-
$this->isWritable = $enable;
62-
return $this;
63-
}
64-
6554
/**
6655
* @return StructureInterface[]
6756
*/
@@ -139,10 +128,6 @@ public function count()
139128
*/
140129
public function offsetSet($offset, $value)
141130
{
142-
if ($this->isWritable) {
143-
$this->entries[$offset] = $value;
144-
return;
145-
}
146131
throw new ReadOnlyException('You cannot rewrite datum. The Constant Pool is read-only.');
147132
}
148133

@@ -152,10 +137,6 @@ public function offsetSet($offset, $value)
152137
*/
153138
public function offsetUnset($offset)
154139
{
155-
if ($this->isWritable) {
156-
unset($this->entries[$offset]);
157-
return;
158-
}
159140
throw new ReadOnlyException('You cannot rewrite datum. The Constant Pool is read-only.');
160141
}
161142

src/Core/JavaClass.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use PHPJava\Core\JVM\AttributePool;
55
use PHPJava\Core\JVM\FieldPool;
66
use PHPJava\Core\JVM\InterfacePool;
7-
use PHPJava\Core\JVM\Intern\StringIntern;
87
use PHPJava\Core\JVM\MethodPool;
98
use PHPJava\Core\JVM\ConstantPool;
109
use PHPJava\Core\JVM\Parameters\GlobalOptions;

src/Kernel/Structures/_Utf8.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace PHPJava\Kernel\Structures;
33

44
use PHPJava\Exceptions\NotImplementedException;
5+
use PHPJava\Exceptions\ReadOnlyException;
56
use PHPJava\Utilities\BinaryTool;
67

78
class _Utf8 implements StructureInterface
@@ -12,6 +13,7 @@ class _Utf8 implements StructureInterface
1213

1314
private $length = 0;
1415
private $string = '';
16+
private $isWritable = false;
1517

1618
/**
1719
* @var \PHPJava\Packages\java\lang\_String $stringObject
@@ -32,6 +34,31 @@ public function getLength()
3234
return $this->length;
3335
}
3436

37+
/**
38+
* @param bool $enable
39+
* @return _Utf8
40+
*/
41+
public function enableWrite(bool $enable): self
42+
{
43+
$this->isWritable = $enable;
44+
return $this;
45+
}
46+
47+
/**
48+
* @param string $string
49+
* @return _Utf8
50+
* @throws ReadOnlyException
51+
*/
52+
public function setString(string $string): self
53+
{
54+
if ($this->isWritable) {
55+
$this->string = $string;
56+
return $this;
57+
}
58+
59+
throw new ReadOnlyException('You cannot overwrite constant.');
60+
}
61+
3562
public function getString()
3663
{
3764
return $this->string;
@@ -42,6 +69,12 @@ public function __toString(): string
4269
return $this->getString();
4370
}
4471

72+
public function setStringObject(\PHPJava\Packages\java\lang\_String $stringObject): self
73+
{
74+
$this->stringObject = $stringObject;
75+
return $this;
76+
}
77+
4578
public function getStringObject(): \PHPJava\Packages\java\lang\_String
4679
{
4780
return $this->stringObject;

src/Packages/java/lang/_String.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace PHPJava\Packages\java\lang;
33

4+
use PHPJava\Core\JVM\ConstantPool;
45
use PHPJava\Exceptions\NotImplementedException;
6+
use PHPJava\Exceptions\ReadOnlyException;
57
use PHPJava\Packages\java\lang\IndexOutOfBoundsException;
68
use PHPJava\Kernel\Structures\_Utf8;
79
use PHPJava\Kernel\Types\_Char;
@@ -344,14 +346,32 @@ public function indexOf($a = null, $b = null)
344346
/**
345347
* Returns a canonical representation for the string object.
346348
*
347-
* @param mixed $a
349+
* @native ConstantPool
350+
* @param ConstantPool $cp
348351
* @return mixed
349-
* @throws NotImplementedException
352+
* @throws ReadOnlyException
350353
* @see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/package-summary.html#intern
351354
*/
352-
public function intern($a = null)
353-
{
354-
throw new NotImplementedException(__METHOD__);
355+
public function intern(ConstantPool $cp)
356+
{
357+
// Find same string in the Constant Pool
358+
foreach ($cp as $key => $value) {
359+
if (!($value instanceof _Utf8)) {
360+
continue;
361+
}
362+
/**
363+
* @var _Utf8 $value
364+
*/
365+
if ((string) $value === (string) $this->object) {
366+
$this->object = $value
367+
->enableWrite(true)
368+
->setString((string) $this->object)
369+
->setStringObject($this)
370+
->enableWrite(false);
371+
break;
372+
}
373+
}
374+
return $this;
355375
}
356376

357377
/**

0 commit comments

Comments
 (0)