Skip to content

Commit ccbf9fc

Browse files
committed
WIP
1 parent e135cd0 commit ccbf9fc

10 files changed

Lines changed: 87 additions & 20 deletions

File tree

src/Core/JVM/Field/FieldGettable.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ trait FieldGettable
1515
public function get(string $name)
1616
{
1717
if (!isset($this->fields[$name])) {
18-
debug_print_backtrace();
1918
throw new NoSuchFieldException('Get to undefined Field ' . $name);
2019
}
2120
if ($this->fields[$name] instanceof _String) {

src/Core/JVM/Invoker/Invokable.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public function call(string $name, ...$arguments)
9898
);
9999
}
100100

101+
101102
// Find same method
102103
$convertedPassedArguments = Formatter::buildArgumentsSignature(
103104
array_map(
@@ -108,6 +109,8 @@ function ($argument) {
108109
)
109110
);
110111

112+
$this->debugTool->getLogger()->debug('Passed descriptor is ' . $convertedPassedArguments);
113+
111114
$method = null;
112115

113116
foreach ($methodReferences as $methodReference) {
@@ -131,14 +134,10 @@ function ($argument) {
131134
*/
132135
$methodSignature = Formatter::buildArgumentsSignature($formattedArguments);
133136

134-
// if ($name === 'checkParameterIsNotNull') {
135-
// var_dump($arguments);
136-
// var_dump($convertedPassedArguments);
137-
// var_dump($methodSignature);
138-
// }
137+
$this->debugTool->getLogger()->debug('Find descriptor for ' . $methodSignature);
139138

140139
if (!($this->options['validation']['method']['arguments_count_only'] ?? GlobalOptions::get('validation.method.arguments_count_only') ?? Runtime::VALIDATION_METHOD_ARGUMENTS_COUNT_ONLY)) {
141-
if ($methodSignature === $convertedPassedArguments) {
140+
if (TypeResolver::compare($methodSignature, $convertedPassedArguments)) {
142141
$method = $methodReference;
143142
break;
144143
}

src/Core/JVM/Parameters/Runtime.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ final class Runtime
2020
const LOG_PATH = 'php://stdout';
2121
const LOG_LEVEL = Logger::EMERGENCY;
2222

23+
const PHP_IMITATION_MAPS = [
24+
'String' => '_String',
25+
'Object' => '_Object',
26+
];
27+
28+
const PHP_IMITATION_DIRECTORY = 'PHPJava\\Imitation';
2329
}

src/Core/JavaArchive.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPJava\Core\JVM\Parameters\Runtime;
66
use PHPJava\Exceptions\UndefinedEntrypointException;
77
use PHPJava\Imitation\java\io\FileNotFoundException;
8+
use PHPJava\Imitation\java\lang\_Object;
89
use PHPJava\Imitation\java\lang\ClassNotFoundException;
910
use PHPJava\Utilities\ClassResolver;
1011
use PHPJava\Utilities\DebugTool;
@@ -140,11 +141,12 @@ function ($fileName) {
140141
}
141142

142143
/**
144+
* @param mixed ...$arguments
143145
* @return mixed
144146
* @throws ClassNotFoundException
145147
* @throws UndefinedEntrypointException
146148
*/
147-
public function execute()
149+
public function execute(...$arguments)
148150
{
149151
$this->debugTool->getLogger()->info('Call to entrypoint: ' . $this->getEntryPointName());
150152
if ($this->getEntryPointName() === null) {
@@ -157,7 +159,7 @@ public function execute()
157159
->getMethods()
158160
->call(
159161
static::DEFAULT_ENTRYPOINT_NAME,
160-
[]
162+
...$arguments
161163
);
162164
}
163165

src/Core/JavaClassDeferredLoader.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public function __call($name, $arguments)
2727

2828
private function initializeIfNotInitiated()
2929
{
30+
if (isset($this->javaClass)) {
31+
return $this->javaClass;
32+
}
3033
$this->javaClass = new JavaClass(
3134
new $this->deferLoadingReaderClass(...$this->arguments),
3235
$this->options

src/Kernel/Mnemonics/_invokestatic.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public function execute(): void
2323
$arguments[] = $this->getStack();
2424
}
2525
krsort($arguments);
26-
2726
$return = null;
2827
switch ($resourceType) {
2928
case ClassResolver::RESOLVED_TYPE_CLASS:

src/Kernel/Mnemonics/_new.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public function execute(): void
1616
$class = $cpInfo[$this->readUnsignedShort()];
1717
$className = $cpInfo[$class->getClassIndex()]->getString();
1818
if ($className === $this->javaClass->getClassName()) {
19-
20-
var_dump($this->javaClass->getInvoker());
2119
// will be called <init>
2220
$this->pushStack($this->javaClass);
2321
return;

src/Utilities/ClassResolver.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
use PHPJava\Core\JavaClass;
66
use PHPJava\Core\JavaClassFileReader;
77
use PHPJava\Core\JavaClassReaderInterface;
8+
use PHPJava\Core\JVM\Parameters\Runtime;
89
use PHPJava\Imitation\java\lang\ClassNotFoundException;
910

1011
class ClassResolver
1112
{
12-
const MAPS = [
13-
'String' => '_String',
14-
'Object' => '_Object',
15-
];
13+
const MAPS = Runtime::PHP_IMITATION_MAPS;
1614

1715
// resource types
1816
const RESOURCE_TYPE_FILE = 'RESOURCE_TYPE_FILE';
@@ -107,4 +105,24 @@ public static function add($valuesOrResourceType = self::RESOURCE_TYPE_FILE, $va
107105
}
108106
static::$resolves[] = [$valuesOrResourceType, $value];
109107
}
108+
109+
public static function resolveNameByPath($path)
110+
{
111+
$names = explode(
112+
'.',
113+
str_replace(
114+
[Runtime::PHP_IMITATION_DIRECTORY . '\\', '\\'],
115+
['', '.'],
116+
get_class($path)
117+
)
118+
);
119+
120+
$string = [];
121+
122+
foreach ($names as $name) {
123+
$string[] = array_flip(static::MAPS)[$name] ?? $name;
124+
}
125+
126+
return implode('.', $string);
127+
}
110128
}

src/Utilities/TypeResolver.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
namespace PHPJava\Utilities;
33

44
use PHPJava\Core\JavaClass;
5+
use PHPJava\Core\JVM\Parameters\Runtime;
56
use PHPJava\Exceptions\TypeException;
6-
use PHPJava\Imitation\java\lang\_String;
7+
use PHPJava\Imitation\java\lang\_Object;
78
use PHPJava\Kernel\Types\Type;
89

910
class TypeResolver
@@ -108,10 +109,10 @@ public static function convertPHPtoJava($arguments, $defaultJavaArgumentType = '
108109
'deep_array' => $deepArray,
109110
];
110111
}
111-
if ($arguments instanceof _String) {
112+
if ($arguments instanceof _Object) {
112113
return [
113114
'type' => 'class',
114-
'class_name' => 'java.lang.String',
115+
'class_name' => ClassResolver::resolveNameByPath($arguments),
115116
'deep_array' => $deepArray,
116117
];
117118
}
@@ -130,4 +131,46 @@ public static function convertPHPtoJava($arguments, $defaultJavaArgumentType = '
130131
'deep_array' => $deepArray,
131132
];
132133
}
134+
135+
public static function compare(string $a, string $b): bool
136+
{
137+
if ($a === $b) {
138+
return true;
139+
}
140+
141+
return Formatter::buildArgumentsSignature(static::getRootClasses($a)) === Formatter::buildArgumentsSignature(static::getRootClasses($b));
142+
}
143+
144+
public static function getRootClasses($class)
145+
{
146+
$result = [];
147+
foreach (Formatter::parseSignature($class) as $signature) {
148+
if ($signature['type'] !== 'class') {
149+
$result[] = $signature;
150+
continue;
151+
}
152+
$path = [];
153+
foreach (explode('.', $signature['class_name']) as $name) {
154+
$path[] = Runtime::PHP_IMITATION_MAPS[$name] ?? $name;
155+
}
156+
$classPath = Runtime::PHP_IMITATION_DIRECTORY . '\\' . implode('\\', $path);
157+
158+
$rootClass = $classPath;
159+
while (($getRootClass = get_parent_class($rootClass)) !== false) {
160+
$rootClass = $getRootClass;
161+
}
162+
$newClassName = explode('.', str_replace([Runtime::PHP_IMITATION_DIRECTORY . '\\', '\\'], ['', '.'], $rootClass));
163+
foreach ($newClassName as $key => $value) {
164+
$newClassName[$key] = array_flip(Runtime::PHP_IMITATION_MAPS)[$value] ?? $value;
165+
}
166+
167+
$newClassName = implode('.', $newClassName);
168+
$result[] = [
169+
'class_name' => $newClassName,
170+
'deep_array' => 0,
171+
] + $signature;
172+
}
173+
174+
return $result;
175+
}
133176
}

tests/JarTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function setUp(): void
1919
public function testCallWithEntryPoint()
2020
{
2121
ob_start();
22-
(new JavaArchive(__DIR__ . '/caches/JarTest.jar'))->execute();
22+
(new JavaArchive(__DIR__ . '/caches/JarTest.jar'))->execute([]);
2323
$result = ob_get_clean();
2424

2525
$this->assertEquals(

0 commit comments

Comments
 (0)