Skip to content

Commit ae14cc4

Browse files
authored
Merge pull request #85 from php-java/update-class-loader
Update class/method resolver
2 parents afb6358 + eb6aea3 commit ae14cc4

7 files changed

Lines changed: 48 additions & 20 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "JVM emulator by PHP",
44
"type": "library",
55
"license": "MIT",
6-
"version": "0.0.6.3-dev",
6+
"version": "0.0.6.4-dev",
77
"authors": [
88
{
99
"name": "memory"

phpcs.xml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88
</rule>
99

1010
<rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
11-
<exclude-pattern>./src/kernel/types</exclude-pattern>
12-
<exclude-pattern>./src/kernel/mnemonics</exclude-pattern>
13-
<exclude-pattern>./src/kernel/structures</exclude-pattern>
14-
<exclude-pattern>./src/imitation/java/lang/_Object.php</exclude-pattern>
15-
<exclude-pattern>./src/imitation/java/lang/_String.php</exclude-pattern>
16-
<exclude-pattern>./src/imitation/phpjava/extended/_Object</exclude-pattern>
11+
<exclude-pattern>./src/Kernel/Types</exclude-pattern>
12+
<exclude-pattern>./src/Kernel/Mnemonics</exclude-pattern>
13+
<exclude-pattern>./src/Kernel/Structures</exclude-pattern>
14+
<exclude-pattern>./src/Imitation/PHPJava/Extended/_Object.php</exclude-pattern>
1715
</rule>
1816

1917
<rule ref="Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase">
2018
<exclude-pattern>./src/kernel/maps</exclude-pattern>
2119
</rule>
2220

21+
22+
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
23+
<exclude-pattern>./src/Imitation/PHPJava/Extended/_Object.php</exclude-pattern>
24+
</rule>
25+
2326
<arg name="colors"/>
2427
<arg value="p"/>
2528

src/Core/JVM/Parameters/Runtime.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ final class Runtime
2727
];
2828

2929
const PHP_IMITATION_DIRECTORY = '\\PHPJava\\Imitation';
30+
31+
const PREFIX_STATIC = 'static_';
3032
}

src/Core/PHPJava.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ final class PHPJava
1212
/**
1313
* As same as composer version.
1414
*/
15-
const VERSION = 0x000063;
15+
const VERSION = 0x000064;
1616
}

src/Imitation/PHPJava/Extended/_Object.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,47 @@ public function __destruct()
1919

2020
public function __call($name, $arguments)
2121
{
22+
$defaultName = '__default_' . $name;
23+
if (method_exists($this, $defaultName)) {
24+
return $this->{$defaultName}(...$arguments);
25+
}
2226
throw new NoSuchMethodException($name . ' does not exist on ' . get_class($this));
2327
}
2428

25-
public function clone(): _Object
29+
public function __default_clone(): _Object
2630
{
2731
return clone $this;
2832
}
2933

30-
public function equals($a = null)
34+
public function __default_equals($a = null)
3135
{
3236
return $this === $a;
3337
}
3438

35-
public function getClass(): self
39+
public function __default_getClass(): self
3640
{
3741
return $this;
3842
}
3943

40-
public function hashCode()
44+
public function __default_hashCode()
4145
{
4246
if (version_compare(PHP_VERSION, '7.2', '<')) {
4347
return crc32(spl_object_hash($this));
4448
}
4549
return spl_object_id($this);
4650
}
4751

48-
public function notify(): void
52+
public function __default_notify(): void
4953
{
5054
// not implemented.
5155
}
5256

53-
public function notifyAll(): void
57+
public function __default_notifyAll(): void
5458
{
5559
// not implemented.
5660
}
5761

58-
public function toString(): string
62+
public function __default_toString()
5963
{
6064
return 'java.lang.Object@PHPJava' . spl_object_hash($this);
6165
}
@@ -65,12 +69,12 @@ public function __toString(): string
6569
return $this->toString();
6670
}
6771

68-
public function wait(int $timeout = null, int $nanos = null): void
72+
public function __default_wait(int $timeout = null, int $nanos = null): void
6973
{
7074
// not implemented.
7175
}
7276

73-
public function finalize(): void
77+
public function __default_finalize(): void
7478
{
7579
}
7680
}

src/Kernel/Mnemonics/_invokestatic.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace PHPJava\Kernel\Mnemonics;
33

4+
use PHPJava\Core\JVM\Parameters\GlobalOptions;
5+
use PHPJava\Core\JVM\Parameters\Runtime;
46
use PHPJava\Exceptions\NotImplementedException;
57
use PHPJava\Utilities\BinaryTool;
68
use PHPJava\Utilities\ClassResolver;
@@ -28,6 +30,8 @@ public function execute(): void
2830
krsort($arguments);
2931
$return = null;
3032

33+
$prefix = $this->getOptions('prefix_static') ?? GlobalOptions::get('prefix_static') ?? Runtime::PREFIX_STATIC;
34+
3135
switch ($resourceType) {
3236
case ClassResolver::RESOLVED_TYPE_CLASS:
3337
/**
@@ -46,7 +50,7 @@ public function execute(): void
4650
$return = forward_static_call_array(
4751
[
4852
$classObject,
49-
"static_{$methodName}"
53+
"{$prefix}{$methodName}"
5054
],
5155
$arguments
5256
);

src/Utilities/TypeResolver.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ public static function compare(string $a, string $b): bool
224224
*/
225225
public static function getExtendedClasses($class): array
226226
{
227+
static $loadedExtendedRoots = [];
227228
$result = [];
228229
foreach (Formatter::parseSignature($class) as $signature) {
229230
if ($signature['type'] !== 'class') {
@@ -236,6 +237,11 @@ public static function getExtendedClasses($class): array
236237
}
237238
$classPath = Runtime::PHP_IMITATION_DIRECTORY . '\\' . implode('\\', $path);
238239

240+
if (isset($loadedExtendedRoots[$classPath])) {
241+
$extendedClasses = $loadedExtendedRoots[$classPath];
242+
continue;
243+
}
244+
239245
// Remove duplicated prefix
240246
$classPath = preg_replace(
241247
'/^(?:' . preg_quote(Runtime::PHP_IMITATION_DIRECTORY, '/') . ')+/',
@@ -250,6 +256,16 @@ public static function getExtendedClasses($class): array
250256
}
251257

252258
$result[] = $extendedClasses;
259+
260+
$loadedExtendedRoots = $extendedClasses;
261+
if (class_exists($classPath)) {
262+
$reflectionClass = new \ReflectionClass($classPath);
263+
preg_match_all('/\@parent\s+([^\r\n]+)/i', $reflectionClass->getDocComment(), $parents);
264+
$roots = array_merge($parents[1], [$classPath]);
265+
if (count($roots) > $extendedClasses) {
266+
$loadedExtendedRoots = $roots;
267+
}
268+
}
253269
}
254270

255271
array_walk_recursive($result, function (&$className) {
@@ -265,8 +281,7 @@ public static function getExtendedClasses($class): array
265281
$newClassName[$key] = array_flip(Runtime::PHP_IMITATION_MAPS)[$value] ?? $value;
266282
}
267283

268-
$newClassName = implode('.', $newClassName);
269-
$className = $newClassName;
284+
$className = $newClassName = implode('.', $newClassName);
270285
});
271286

272287
return $result;

0 commit comments

Comments
 (0)