Skip to content

Commit 81f7b7f

Browse files
committed
Upgrade ClassResolver for Outerclass
1 parent 0620690 commit 81f7b7f

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

src/Core/JavaClassReader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace PHPJava\Core;
33

4+
use PHPJava\Utilities\ClassResolver;
5+
46
class JavaClassReader
57
{
68
private $handle;
@@ -10,6 +12,12 @@ public function __construct(string $file)
1012
{
1113
$this->handle = fopen($file, 'r');
1214
$this->binaryReader = new JVM\Stream\BinaryReader($this->handle);
15+
16+
// Add resolving path
17+
ClassResolver::add(
18+
ClassResolver::RESOURCE_TYPE_FILE,
19+
dirname($file)
20+
);
1321
}
1422

1523
public function getBinaryReader(): JVM\Stream\BinaryReader

src/Utilities/ClassResolver.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,72 @@
11
<?php
22
namespace PHPJava\Utilities;
33

4+
use PHPJava\Core\JavaClass;
5+
use PHPJava\Core\JavaClassReader;
6+
47
class ClassResolver
58
{
69
const MAPS = [
710
'String' => '_String',
811
'Object' => '_Object',
912
];
1013

11-
public static function resolve($javaPath): string
14+
// resource types
15+
const RESOURCE_TYPE_FILE = 'RESOURCE_TYPE_FILE';
16+
17+
// resolved types
18+
const RESOLVED_TYPE_CLASS = 'RESOLVED_TYPE_CLASS';
19+
const RESOLVED_TYPE_IMITATION = 'RESOLVED_TYPE_IMITATION';
20+
21+
private static $resolves = [];
22+
private static $resolvedPaths = [];
23+
24+
public static function resolve($javaPath): array
1225
{
1326
$namespaces = explode('.', str_replace('/', '.', $javaPath));
1427
$buildClassPath = [];
1528
foreach ($namespaces as $namespace) {
1629
$buildClassPath[] = static::MAPS[$namespace] ?? $namespace;
1730
}
18-
return '\\PHPJava\\Imitation\\' . implode('\\', $buildClassPath);
31+
32+
// resolve something approaching
33+
$relativePath = implode('/', $namespaces);
34+
foreach (static::$resolves as [$resourceType, $value]) {
35+
switch ($resourceType) {
36+
case static::RESOURCE_TYPE_FILE:
37+
$path = realpath($value . '/' . $relativePath . '.class');
38+
if (($key = array_search($path, static::$resolvedPaths, true)) !== false) {
39+
return static::$resolvedPaths[$key];
40+
}
41+
if (is_file($path)) {
42+
return $resolvedPaths[] = [
43+
static::RESOLVED_TYPE_CLASS,
44+
new JavaClass(new JavaClassReader($path)),
45+
];
46+
}
47+
break;
48+
}
49+
}
50+
51+
return [
52+
static::RESOLVED_TYPE_IMITATION,
53+
'\\PHPJava\\Imitation\\' . implode('\\', $buildClassPath),
54+
];
55+
}
56+
57+
public static function add($valuesOrResourceType = self::RESOURCE_TYPE_FILE, $value = null): void
58+
{
59+
if (is_array($valuesOrResourceType)) {
60+
foreach ($valuesOrResourceType as [$resourceType, $value]) {
61+
static::add($resourceType, $value);
62+
}
63+
64+
return;
65+
}
66+
67+
if (in_array([$valuesOrResourceType, $value], static::$resolves, true)) {
68+
return;
69+
}
70+
static::$resolves[] = [$valuesOrResourceType, $value];
1971
}
2072
}

0 commit comments

Comments
 (0)