Skip to content

Commit f57b8f6

Browse files
committed
ClassResolver became static to a dynamic
1 parent 0aaf36b commit f57b8f6

9 files changed

Lines changed: 66 additions & 36 deletions

File tree

src/Core/JavaArchive.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ public function __construct(string $jarFile, array $options = [])
5454

5555
$this->manifestData['main-class'] = $options['entrypoint'] ?? Runtime::ENTRYPOINT;
5656

57+
if (!(($this->options['class_resolver'] ?? null) instanceof ClassResolver)) {
58+
$this->options['class_resolver'] = new ClassResolver(
59+
$this->options
60+
);
61+
}
5762
// Add resolving path
58-
ClassResolver::add(
63+
$this->options['class_resolver']->add(
5964
[
6065
[ClassResolver::RESOURCE_TYPE_FILE, dirname($jarFile)],
6166
[ClassResolver::RESOURCE_TYPE_FILE, getcwd()],
@@ -137,7 +142,7 @@ function ($fileName) {
137142
case ClassResolver::RESOURCE_TYPE_FILE:
138143
break;
139144
}
140-
ClassResolver::add($fileType, $value);
145+
$this->options['class_resolver']->add($fileType, $value);
141146
}
142147

143148
$this->debugTool->getLogger()->info('End of jar');

src/Core/JavaClass.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ public function __construct(ReaderInterface $reader, array $options = [])
9999
// options
100100
$this->options = $options;
101101

102+
if (!(($this->options['class_resolver'] ?? null) instanceof ClassResolver)) {
103+
$this->options['class_resolver'] = new ClassResolver(
104+
$this->options
105+
);
106+
}
107+
108+
$this->options['class_resolver']->add([
109+
[ClassResolver::RESOURCE_TYPE_FILE, dirname($reader->getFileName())],
110+
[ClassResolver::RESOURCE_TYPE_FILE, getcwd()]
111+
]);
112+
102113
// Debug tool
103114
$this->debugTool = new DebugTool(
104115
$reader->getJavaPathName(),
@@ -139,7 +150,7 @@ public function __construct(ReaderInterface $reader, array $options = [])
139150
$this->superClassIndex = $reader->getBinaryReader()->readUnsignedShort();
140151

141152
$cpInfo = $this->getConstantPool();
142-
[$resolvedType, $superClass] = ClassResolver::resolve(
153+
[$resolvedType, $superClass] = $this->options['class_resolver']->resolve(
143154
$cpInfo[$cpInfo[$this->superClassIndex]->getClassIndex()]->getString(),
144155
$this
145156
);
@@ -218,6 +229,11 @@ public function __construct(ReaderInterface $reader, array $options = [])
218229
}
219230
}
220231

232+
public function getOptions()
233+
{
234+
return $this->options;
235+
}
236+
221237
public function __destruct()
222238
{
223239
$this->debugTool->getLogger()->info(
@@ -352,9 +368,4 @@ function ($code) use (&$codeCounter, &$debugTraces) {
352368
printf("\n");
353369
}
354370
}
355-
356-
public function getOptions(): array
357-
{
358-
return $this->options;
359-
}
360371
}

src/Core/Stream/Reader/FileReader.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ public function __construct(string $file)
1818
}
1919
$this->handle = fopen($file, 'r');
2020
$this->binaryReader = new BinaryReader($this->handle);
21-
22-
// Add resolving path
23-
ClassResolver::add(
24-
[
25-
[ClassResolver::RESOURCE_TYPE_FILE, dirname($file)],
26-
[ClassResolver::RESOURCE_TYPE_FILE, getcwd()],
27-
]
28-
);
2921
}
3022

3123
public function getBinaryReader(): BinaryReader

src/Kernel/Core/Accumulator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ trait Accumulator
2424
private $localStorage;
2525
private $pointer;
2626
private $stacks = [];
27+
private $options;
2728

2829
public function setParameters(
2930
JavaClassInvoker $javaClassInvoker,
@@ -34,6 +35,7 @@ public function setParameters(
3435
): self {
3536
$this->javaClassInvoker = $javaClassInvoker;
3637
$this->javaClass = $javaClassInvoker->getJavaClass();
38+
$this->options = $this->javaClass->getOptions();
3739
$this->reader = $reader;
3840
$this->localStorage = &$localStorage;
3941
$this->stacks = &$stacks;
@@ -159,4 +161,9 @@ public function getProgramCounter()
159161
{
160162
return $this->pointer;
161163
}
164+
165+
public function getOptions($key)
166+
{
167+
return $this->options[$key] ?? null;
168+
}
162169
}

src/Kernel/Mnemonics/_getstatic.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public function execute(): void
3232
}
3333

3434
if (isset($signature[0]['class_name'])) {
35-
[$resourceType, $classObject] = ClassResolver::resolve($signature[0]['class_name']);
35+
[$resourceType, $classObject] = $this->getOptions('class_resolver')
36+
->resolve($signature[0]['class_name']);
3637
if ($resourceType === ClassResolver::RESOLVED_TYPE_CLASS) {
3738
/**
3839
* @var \PHPJava\Core\JavaClass $className

src/Kernel/Mnemonics/_invokestatic.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ public function execute(): void
1818
$methodName = $cpInfo[$cpInfo[$cp->getNameAndTypeIndex()]->getNameIndex()]->getString();
1919
$signature = Formatter::parseSignature($cpInfo[$cpInfo[$cp->getNameAndTypeIndex()]->getDescriptorIndex()]->getString());
2020
$arguments = [];
21-
[$resourceType, $classObject] = ClassResolver::resolve($cpInfo[$cpInfo[$cp->getClassIndex()]->getClassIndex()]->getString());
21+
$this->getOptions('class_resolver')
22+
->resolve($cpInfo[$cpInfo[$cp->getClassIndex()]->getClassIndex()]->getString());
23+
[$resourceType, $classObject] = $this->getOptions('class_resolver')
24+
->resolve($cpInfo[$cpInfo[$cp->getClassIndex()]->getClassIndex()]->getString());
2225
for ($i = 0; $i < $signature['arguments_count']; $i++) {
2326
$arguments[] = $this->popFromOperandStack();
2427
}

src/Kernel/Mnemonics/_invokevirtual.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function execute(): void
3030
}
3131
krsort($arguments);
3232
$invokerClass = $this->popFromOperandStack();
33-
$invokerClassName = ClassResolver::resolve($class);
33+
$invokerClassName = $this->getOptions('class_resolver')->resolve($class);
3434
$methodName = $cpInfo[$cpInfo[$cp->getNameAndTypeIndex()]->getNameIndex()]->getString();
3535

3636
if ($invokerClass instanceof JavaClass) {

src/Kernel/Mnemonics/_new.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function execute(): void
2121
return;
2222
}
2323

24-
[$resourceType, $classObject] = ClassResolver::resolve($className, $this->javaClass);
24+
[$resourceType, $classObject] = $this->getOptions('class_resolver')->resolve($className, $this->javaClass);
2525
if ($resourceType === ClassResolver::RESOLVED_TYPE_CLASS) {
2626
/**
2727
* @var \PHPJava\Core\JavaClass $classObject

src/Utilities/ClassResolver.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ class ClassResolver
2121
const RESOLVED_TYPE_CLASS = 'RESOLVED_TYPE_CLASS';
2222
const RESOLVED_TYPE_IMITATION = 'RESOLVED_TYPE_IMITATION';
2323

24-
private static $resolves = [];
25-
private static $resolvedPaths = [];
24+
private $resolves = [];
25+
private $resolvedPaths = [];
26+
private $options = [];
2627

27-
public static function resolve(string $javaPath, JavaClass $class = null): array
28+
public function __construct(array $options)
29+
{
30+
$this->options = $options;
31+
}
32+
33+
public function resolve(string $javaPath, JavaClass $class = null): array
2834
{
2935
$namespaces = explode('.', str_replace('/', '.', $javaPath));
3036
$buildClassPath = [];
@@ -34,19 +40,22 @@ public static function resolve(string $javaPath, JavaClass $class = null): array
3440

3541
// resolve something approaching
3642
$relativePath = implode('/', $namespaces);
37-
foreach (static::$resolves as [$resourceType, $value]) {
43+
foreach ($this->resolves as [$resourceType, $value]) {
3844
switch ($resourceType) {
3945
case static::RESOURCE_TYPE_FILE:
4046
$path = realpath($value . '/' . $relativePath . '.class');
41-
if (($key = array_search($path, static::$resolvedPaths, true)) !== false) {
42-
return static::$resolvedPaths[$key];
47+
if (($key = array_search($path, $this->resolvedPaths, true)) !== false) {
48+
return $this->resolvedPaths[$key];
4349
}
4450
if (is_file($path)) {
45-
$initiatedClass = new JavaClass(new FileReader($path));
51+
$initiatedClass = new JavaClass(
52+
new FileReader($path),
53+
$this->options
54+
);
4655
if (strpos($relativePath, '$') !== false && $class !== null) {
4756
$initiatedClass->setParentClass($class);
4857
}
49-
return $resolvedPaths[] = [
58+
return $this->resolvedPaths[] = [
5059
static::RESOLVED_TYPE_CLASS,
5160
$initiatedClass,
5261
];
@@ -57,7 +66,7 @@ public static function resolve(string $javaPath, JavaClass $class = null): array
5766
* @var JavaArchive $value
5867
*/
5968
try {
60-
return $resolvedPaths[] = [
69+
return $this->resolvedPaths[] = [
6170
static::RESOLVED_TYPE_CLASS,
6271
$value->getClassByName($relativePath),
6372
];
@@ -69,9 +78,12 @@ public static function resolve(string $javaPath, JavaClass $class = null): array
6978
* @var ReaderInterface $value
7079
*/
7180
try {
72-
return $resolvedPaths[] = [
81+
return $this->resolvedPaths[] = [
7382
static::RESOLVED_TYPE_CLASS,
74-
new JavaClass($value),
83+
new JavaClass(
84+
$value,
85+
$this->options
86+
),
7587
];
7688
} catch (ClassNotFoundException $e) {
7789
}
@@ -97,19 +109,18 @@ public static function resolve(string $javaPath, JavaClass $class = null): array
97109
];
98110
}
99111

100-
public static function add($valuesOrResourceType = self::RESOURCE_TYPE_FILE, $value = null): void
112+
public function add($valuesOrResourceType = self::RESOURCE_TYPE_FILE, $value = null): void
101113
{
102114
if (is_array($valuesOrResourceType)) {
103115
foreach ($valuesOrResourceType as [$resourceType, $value]) {
104-
static::add($resourceType, $value);
116+
$this->add($resourceType, $value);
105117
}
106118
return;
107119
}
108-
109-
if (in_array([$valuesOrResourceType, $value], static::$resolves, true)) {
120+
if (in_array([$valuesOrResourceType, $value], $this->resolves, true)) {
110121
return;
111122
}
112-
static::$resolves[] = [$valuesOrResourceType, $value];
123+
$this->resolves[] = [$valuesOrResourceType, $value];
113124
}
114125

115126
public static function resolveNameByPath($path)

0 commit comments

Comments
 (0)