Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Core/JVM/ConstantPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPJava\Kernel\Structures\_NameAndType;
use PHPJava\Kernel\Structures\_String;
use PHPJava\Kernel\Structures\_Utf8;
use PHPJava\Kernel\Structures\FreezableInterface;
use PHPJava\Kernel\Structures\StructureInterface;

class ConstantPool implements \ArrayAccess, \Countable, \IteratorAggregate
Expand Down Expand Up @@ -110,6 +111,9 @@ public function offsetExists($offset)
*/
public function offsetGet($offset)
{
if ($this->entries[$offset] instanceof FreezableInterface) {
$this->entries[$offset]->freeze();
}
return $this->entries[$offset];
}

Expand Down
8 changes: 8 additions & 0 deletions src/Kernel/Structures/FreezableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace PHPJava\Kernel\Structures;

interface FreezableInterface
{
public function freeze(): void;
}
36 changes: 22 additions & 14 deletions src/Kernel/Structures/_Utf8.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
use PHPJava\Exceptions\ReadOnlyException;
use PHPJava\Utilities\BinaryTool;

class _Utf8 implements StructureInterface
class _Utf8 implements StructureInterface, FreezableInterface
{
use \PHPJava\Kernel\Core\BinaryReader;
use \PHPJava\Kernel\Core\ConstantPool;
use \PHPJava\Kernel\Core\DebugTool;

private $length = 0;
private $string = '';
private $isWritable = false;
private $isWritable = null;
private $isFrozen = false;

/**
* @var \PHPJava\Packages\java\lang\_String $stringObject
Expand All @@ -29,39 +30,46 @@ public function execute(): void
$this->stringObject = new \PHPJava\Packages\java\lang\_String($this);
}

public function getLength()
public function getLength(): int
{
return $this->length;
}

/**
* @param bool $enable
* @return _Utf8
*/
public function enableWrite(bool $enable): self
{
$this->isWritable = $enable;
if (!$this->isFrozen) {
$this->isWritable = $enable;
}
return $this;
}

public function getString()
public function freeze(): void
{
$this->isFrozen = true;
$this->enableWrite(false);
}

public function getString(): string
{
return $this->string;
}

public function __toString(): string
public function getStringObject(): \PHPJava\Packages\java\lang\_String
{
return $this->getString();
return $this->stringObject;
}

public function setStringObject(\PHPJava\Packages\java\lang\_String $stringObject): self
{
$this->stringObject = $stringObject;
if ($this->isWritable) {
$this->stringObject = $stringObject;
$this->freeze();
}
return $this;
}

public function getStringObject(): \PHPJava\Packages\java\lang\_String
public function __toString(): string
{
return $this->stringObject;
return $this->getString();
}
}
3 changes: 1 addition & 2 deletions src/Packages/java/lang/_String.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ public function intern(ConstantPool $cp)
if ((string) $value === (string) $this->object) {
$this->object = $value
->enableWrite(true)
->setStringObject($this)
->enableWrite(false);
->setStringObject($this);
break;
}
}
Expand Down