Skip to content

Commit f4daacf

Browse files
committed
Remove depended ClassResolver::resolve on all files
1 parent 4889930 commit f4daacf

File tree

10 files changed

+46
-204
lines changed

10 files changed

+46
-204
lines changed

src/Core/JVM/Invoker/Extended/JavaMethodCallable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ function () use ($name, $arguments) {
8383
);
8484

8585
try {
86+
/**
87+
* @var CodeAttribute $codeAttribute
88+
*/
8689
$codeAttribute = AttributionResolver::resolve(
8790
$method->getAttributes(),
8891
CodeAttribute::class

src/Core/JVM/Invoker/Extended/PHPMethodFindable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
trait PHPMethodFindable
77
{
8-
public function findMethod(string $name): \ReflectionMethod
8+
protected function findMethod(string $name): \ReflectionMethod
99
{
1010
if (!isset($this->methods[$name])) {
1111
throw new NoSuchMethodException(

src/Core/JavaArchive.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,6 @@ public function __construct(string $jarFile, array $options = [])
8282

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

85-
if (!(($this->options['class_resolver'] ?? null) instanceof ClassResolver)) {
86-
$this->options['class_resolver'] = new ClassResolver(
87-
$this->options
88-
);
89-
}
9085
// Add resolving path
9186
ClassResolver::add(
9287
[
@@ -150,7 +145,7 @@ function ($fileName) {
150145
$value = $realpath;
151146

152147
switch ($fileType = FileTypeResolver::resolve($resolvePath)) {
153-
case ClassResolver::RESOLVED_TYPE_CLASS:
148+
case ClassResolver::RESOURCE_TYPE_CLASS:
154149
$value = new JavaCompiledClass(
155150
new Stream\Reader\FileReader($value),
156151
$this->options

src/Core/JavaCompiledClass.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,6 @@ public function __construct(ReaderInterface $reader, array $options = [])
125125
// options
126126
$this->options = $options;
127127

128-
if (!(($this->options['class_resolver'] ?? null) instanceof ClassResolver)) {
129-
$this->options['class_resolver'] = new ClassResolver(
130-
$this->options
131-
);
132-
}
133-
134128
ClassResolver::add([
135129
[ClassResolver::RESOURCE_TYPE_FILE, dirname($reader->getFileName())],
136130
[ClassResolver::RESOURCE_TYPE_FILE, getcwd()],

src/Core/JavaSimpleClass.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ public function __construct(ReaderInterface $reader, array $options = [])
7272
// options
7373
$this->options = $options;
7474

75-
if (!(($this->options['class_resolver'] ?? null) instanceof ClassResolver)) {
76-
$this->options['class_resolver'] = new ClassResolver(
77-
$this->options
78-
);
79-
}
80-
8175
ClassResolver::add([
8276
[ClassResolver::RESOURCE_TYPE_FILE, dirname($reader->getFileName())],
8377
[ClassResolver::RESOURCE_TYPE_FILE, getcwd()],

src/Kernel/Internal/Lambda.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
<?php
22
namespace PHPJava\Kernel\Internal;
33

4+
use PHPJava\Core\JavaClass;
45
use PHPJava\Core\JavaClassInterface;
6+
use PHPJava\Kernel\Structures\_MethodInfo;
57

6-
class Lambda
8+
class Lambda implements JavaClassInterface
79
{
8-
/**
9-
* @var JavaClassInterface
10-
*/
11-
private $javaClass;
12-
1310
/**
1411
* @var string
1512
*/
@@ -26,35 +23,43 @@ class Lambda
2623
private $class;
2724

2825
/**
29-
* @var string
26+
* @var _MethodInfo|\ReflectionMethod
3027
*/
31-
private $resourceType;
28+
private $methodEntity;
3229

3330
/**
3431
* @var JavaClassInterface|string
3532
*/
3633
private $classObject;
3734

38-
public function __construct(JavaClassInterface $javaClass, string $name, string $descriptor, string $class)
35+
public function __construct(JavaClassInterface $javaClass, string $invokedName, string $name, string $descriptor, string $class)
3936
{
40-
$this->javaClass = $javaClass;
4137
$this->name = $name;
4238
$this->descriptor = $descriptor;
4339
$this->class = $class;
4440

45-
[$this->resourceType, $this->classObject] = $javaClass
46-
->getOptions('class_resolver')
47-
->resolve($this->class);
41+
$this->methodEntity = [
42+
$name,
43+
$invokedName,
44+
$javaClass,
45+
];
46+
47+
$this->classObject = JavaClass::load(
48+
$class,
49+
$javaClass->getOptions()
50+
);
4851
}
4952

50-
public function __invoke(...$arguments)
53+
public function __invoke(string $name, ...$arguments)
5154
{
52-
return $this->javaClass
55+
[$actualMethodName, $entityMethodName, $referredClassObject] = $this->methodEntity;
56+
57+
return $referredClassObject
5358
->getInvoker()
5459
->getStatic()
5560
->getMethods()
5661
->call(
57-
$this->name,
62+
$actualMethodName,
5863
...$arguments
5964
);
6065
}

src/Kernel/Mnemonics/_invokeinterface.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<?php
22
namespace PHPJava\Kernel\Mnemonics;
33

4-
use PHPJava\Exceptions\NoSuchCodeAttributeException;
54
use PHPJava\Kernel\Internal\Lambda;
65
use PHPJava\Kernel\Resolvers\TypeResolver;
76
use PHPJava\Kernel\Types\Type;
8-
use PHPJava\Packages\java\lang\_String;
97
use PHPJava\Utilities\Formatter;
108

119
final class _invokeinterface implements OperationInterface
@@ -36,22 +34,13 @@ public function execute(): void
3634

3735
$objectref = $this->popFromOperandStack();
3836

39-
try {
40-
if ($objectref instanceof Lambda) {
41-
$result = $objectref(...$arguments);
42-
} else {
43-
$result = $objectref->getInvoker()->getDynamic()->getMethods()->call(
44-
$name,
45-
...$arguments
46-
);
47-
}
48-
} catch (NoSuchCodeAttributeException $e) {
49-
if ($signature[0]['class_name'] === 'java/lang/Class') {
50-
$result = $objectref->getClass();
51-
}
52-
if ($signature[0]['class_name'] === 'java/lang/String') {
53-
$result = new _String();
54-
}
37+
if ($objectref instanceof Lambda) {
38+
$result = $objectref($name, ...$arguments);
39+
} else {
40+
$result = $objectref->getInvoker()->getDynamic()->getMethods()->call(
41+
$name,
42+
...$arguments
43+
);
5544
}
5645

5746
if ($signature[0]['type'] !== 'void') {
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace PHPJava\Kernel\Mnemonics;
33

4+
use PHPJava\Core\JavaClass;
5+
46
final class _putstatic implements OperationInterface
57
{
68
use \PHPJava\Kernel\Core\Accumulator;
@@ -11,24 +13,18 @@ public function execute(): void
1113
$cpInfo = $this->getConstantPool();
1214

1315
$cp = $cpInfo[$this->readUnsignedShort()];
14-
1516
$class = $cpInfo[$cp->getNameAndTypeIndex()];
17+
1618
$className = $cpInfo[$cpInfo[$cp->getClassIndex()]->getClassIndex()]->getString();
1719
$fieldName = $cpInfo[$class->getNameIndex()]->getString();
1820

19-
$classObject = $this->javaClass;
20-
if ($this->javaClass->getClassName() !== $className) {
21-
[$resourceType, $classObject] = $this->javaClass->getOptions('class_resolver')
22-
->resolve(
23-
$className,
24-
$this->javaClass,
25-
false
26-
);
27-
}
28-
29-
$classObject->getInvoker()->getStatic()->getFields()->set(
30-
$fieldName,
31-
$this->popFromOperandStack()
32-
);
21+
JavaClass::load($className, $this->javaClass->getOptions(), false)
22+
->getInvoker()
23+
->getStatic()
24+
->getFields()
25+
->set(
26+
$fieldName,
27+
$this->popFromOperandStack()
28+
);
3329
}
3430
}
Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
<?php
22
namespace PHPJava\Kernel\Resolvers;
33

4-
use PHPJava\Core\JavaArchive;
54
use PHPJava\Core\JavaClass;
6-
use PHPJava\Core\JavaClassInterface;
7-
use PHPJava\Core\JavaCompiledClass;
8-
use PHPJava\Core\JavaGenericClassInterface;
95
use PHPJava\Core\JVM\Parameters\Runtime;
10-
use PHPJava\Core\Stream\Reader\FileReader;
11-
use PHPJava\Packages\java\lang\ClassNotFoundException;
126

137
class ClassResolver
148
{
@@ -20,10 +14,6 @@ class ClassResolver
2014
const RESOURCE_TYPE_CLASS = 'RESOURCE_TYPE_CLASS';
2115
const RESOURCE_TYPE_INNER_CLASS = 'RESOURCE_TYPE_INNER_CLASS';
2216

23-
// resolved types
24-
const RESOLVED_TYPE_CLASS = 'RESOLVED_TYPE_CLASS';
25-
const RESOLVED_TYPE_PACKAGES = 'RESOLVED_TYPE_PACKAGES';
26-
2717
/**
2818
* @var array
2919
*/
@@ -39,110 +29,6 @@ class ClassResolver
3929
*/
4030
private $options = [];
4131

42-
public function __construct(array $options)
43-
{
44-
$this->options = $options;
45-
}
46-
47-
public function resolve(string $javaPath, JavaClassInterface $class = null, bool $instantiation = true): array
48-
{
49-
$javaPath = str_replace('/', '.', $javaPath);
50-
$namespaces = explode('.', $javaPath);
51-
$buildClassPath = [];
52-
foreach ($namespaces as $namespace) {
53-
$buildClassPath[] = static::MAPS[$namespace] ?? $namespace;
54-
}
55-
56-
// resolve something approaching
57-
$relativePath = implode('/', $namespaces);
58-
59-
foreach (static::$resolves as [$resourceType, $value]) {
60-
switch ($resourceType) {
61-
case static::RESOURCE_TYPE_INNER_CLASS:
62-
// TODO: Implement here
63-
break;
64-
case static::RESOURCE_TYPE_JAR:
65-
/**
66-
* @var JavaArchive $value
67-
*/
68-
try {
69-
return $this->resolvedPaths[] = [
70-
static::RESOLVED_TYPE_CLASS,
71-
$value->getClassByName($relativePath),
72-
];
73-
} catch (ClassNotFoundException $e) {
74-
}
75-
break;
76-
case static::RESOURCE_TYPE_FILE:
77-
$path = realpath($value . '/' . $relativePath . '.class');
78-
if ($path === false) {
79-
break;
80-
}
81-
if (!$instantiation) {
82-
foreach ($this->resolvedPaths as [$resolvedPath, $class]) {
83-
if ($relativePath === $class->getClassName()) {
84-
return [$resolvedPath, $class];
85-
}
86-
}
87-
}
88-
/**
89-
* @var JavaClass $initiatedClass
90-
*/
91-
if (is_file($path)) {
92-
$initiatedClass = new JavaClass(
93-
new JavaCompiledClass(
94-
new FileReader($path),
95-
$this->options
96-
)
97-
);
98-
99-
return $this->resolvedPaths[] = [
100-
static::RESOLVED_TYPE_CLASS,
101-
$initiatedClass,
102-
];
103-
}
104-
break;
105-
case static::RESOURCE_TYPE_CLASS:
106-
if (!$instantiation) {
107-
foreach ($this->resolvedPaths as [$resolvedPath, $class]) {
108-
if ($value->getClassName() === $class->getClassName()) {
109-
return [$resolvedPath, $class];
110-
}
111-
}
112-
}
113-
114-
/**
115-
* @var JavaGenericClassInterface $value
116-
*/
117-
try {
118-
return $this->resolvedPaths[] = [
119-
static::RESOLVED_TYPE_CLASS,
120-
new JavaClass($value),
121-
];
122-
} catch (ClassNotFoundException $e) {
123-
}
124-
break;
125-
}
126-
}
127-
128-
$className = Runtime::PHP_PACKAGES_DIRECTORY . '\\' . implode('\\', $buildClassPath);
129-
130-
if (!class_exists($className)) {
131-
throw new ClassNotFoundException(
132-
str_replace(
133-
[Runtime::PHP_PACKAGES_DIRECTORY . '\\', '\\'],
134-
['', '.'],
135-
$className
136-
) . ' class does not exist.'
137-
);
138-
}
139-
140-
return [
141-
static::RESOLVED_TYPE_PACKAGES,
142-
$className,
143-
];
144-
}
145-
14632
public static function getClassPaths(): array
14733
{
14834
return static::$resolves;
@@ -161,24 +47,4 @@ public static function add($valuesOrResourceType = self::RESOURCE_TYPE_FILE, $va
16147
}
16248
static::$resolves[] = [$valuesOrResourceType, $value];
16349
}
164-
165-
public static function resolveNameByPath($path): string
166-
{
167-
$names = explode(
168-
'.',
169-
str_replace(
170-
[Runtime::PHP_PACKAGES_DIRECTORY . '\\', '\\'],
171-
['', '.'],
172-
'\\' . ltrim(get_class($path))
173-
)
174-
);
175-
176-
$string = [];
177-
178-
foreach ($names as $name) {
179-
$string[] = array_flip(static::MAPS)[$name] ?? $name;
180-
}
181-
182-
return implode('.', $string);
183-
}
18450
}

0 commit comments

Comments
 (0)