Skip to content

Commit 6916870

Browse files
committed
Add compare tool utility
1 parent e677122 commit 6916870

File tree

9 files changed

+42
-11
lines changed

9 files changed

+42
-11
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use PHPJava\Exceptions\IllegalJavaClassException;
55
use PHPJava\Exceptions\RuntimeException;
66
use PHPJava\Exceptions\UndefinedOpCodeException;
7+
use PHPJava\Kernel\Resolvers\MethodNameResolver;
78

89
trait PHPMethodCallable
910
{
@@ -24,12 +25,21 @@ public function call(string $name, ...$arguments)
2425
->getMethods()
2526
->callStaticInitializerIfNotInstantiated();
2627

28+
$realPHPMethodName = MethodNameResolver::resolve($name);
29+
30+
/**
31+
* @var \ReflectionMethod $method
32+
*/
2733
$method = $this->findMethod($name);
2834

29-
$this->debugTool->getLogger()->debug('Call method: ' . $name);
35+
$suffix = ($realPHPMethodName !== $name ? ' (Actual calling is the ' . $realPHPMethodName . ' method)' : '');
36+
$this->debugTool->getLogger()->debug(
37+
'Call method: ' . $name . $suffix
38+
);
3039

31-
if ($this->isDynamic() && $this->javaClassInvoker->getClassObject() === null) {
32-
$this->javaClassInvoker->construct();
40+
if ($this->isDynamic() && MethodNameResolver::isConstructorMethod($name)) {
41+
$this->javaClassInvoker->construct(...$arguments);
42+
return $this->javaClassInvoker->getJavaClass();
3343
}
3444

3545
$executed = $method
@@ -38,7 +48,9 @@ public function call(string $name, ...$arguments)
3848
$arguments
3949
);
4050

41-
$this->debugTool->getLogger()->debug('Finish operation: ' . $name);
51+
$this->debugTool->getLogger()->debug(
52+
'Finish operation: ' . $name . $suffix
53+
);
4254

4355
return $executed;
4456
}

src/Core/JVM/Stream/BinaryReader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public function readUnsignedShort(): int
6565

6666
public function readInt(): int
6767
{
68-
return current(unpack('N', $this->read(4)));
68+
$bytes = array_values(unpack('c4', $this->read(4)));
69+
return ($bytes[0] << 24) | ($bytes[1] << 16) | ($bytes[2] << 8) | $bytes[3];
6970
}
7071

7172
public function readShort(): int

src/Kernel/Mnemonics/_invokespecial.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPJava\Core\JavaClassInterface;
66
use PHPJava\Kernel\Filters\Normalizer;
77
use PHPJava\Kernel\Resolvers\MethodNameResolver;
8+
use PHPJava\Utilities\CompareTool;
89
use PHPJava\Utilities\Formatter;
910

1011
final class _invokespecial implements OperationInterface
@@ -33,11 +34,12 @@ public function execute(): void
3334
/**
3435
* @var JavaClassInterface $objectref
3536
*/
36-
$newObject = $objectref = $this->popFromOperandStack();
37+
$objectref = $newObject = $this->popFromOperandStack();
3738
try {
3839
$methodName = $cpInfo[$nameAndTypeIndex->getNameIndex()]->getString();
3940

40-
if ($className !== $objectref->getClassName()) {
41+
// load a class dynamically if not match class name and objectref class
42+
if (!CompareTool::compareClassName($className, $objectref->getClassName())) {
4143
$newObject = JavaClass::load(
4244
$className,
4345
$this->javaClass->getOptions()
@@ -53,7 +55,7 @@ public function execute(): void
5355
);
5456

5557
// Call special method (e.g., <init>, <clinit> and soon)
56-
if (MethodNameResolver::isSpecialMethod($methodName)) {
58+
if (MethodNameResolver::isConstructorMethod($methodName)) {
5759
$result = $objectref;
5860

5961
// Set initialized parent parameters

src/Kernel/Mnemonics/_invokevirtual.php

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

4+
use PHPJava\Core\JavaClassInterface;
45
use PHPJava\Kernel\Filters\Normalizer;
56
use PHPJava\Packages\java\lang\NullPointerException;
67
use PHPJava\Utilities\Formatter;
@@ -35,6 +36,9 @@ public function execute(): void
3536
);
3637
}
3738

39+
/**
40+
* @var JavaClassInterface $invokerClass
41+
*/
3842
$invokerClass = $this->popFromOperandStack();
3943
$methodName = $cpInfo[$cpInfo[$cp->getNameAndTypeIndex()]->getNameIndex()]->getString();
4044

src/Kernel/Mnemonics/_tableswitch.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function execute(): void
2424
$offsets['default'] = $this->readInt();
2525
$lowByte = $this->readInt();
2626
$highByte = $this->readInt();
27+
2728
for ($i = $lowByte; $i <= $highByte; $i++) {
2829
$offsets[$i] = $this->readInt();
2930
}

src/Kernel/Resolvers/MethodNameResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static function resolve(string $name): string
1717
return $name;
1818
}
1919

20-
public static function isSpecialMethod(string $name): bool
20+
public static function isConstructorMethod(string $name): bool
2121
{
2222
$flipped = array_flip(static::PHP_METHOD_MAP);
2323
return isset($flipped[$name]);

src/Kernel/Structures/_Integer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class _Integer implements StructureInterface
1414

1515
public function execute(): void
1616
{
17-
$this->bytes = $this->readInt();
17+
$this->bytes = $this->readUnsignedInt();
1818
}
1919

2020
public function getBytes(): int

src/Utilities/CompareTool.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace PHPJava\Utilities;
3+
4+
class CompareTool
5+
{
6+
public static function compareClassName(string $a, string $b): bool
7+
{
8+
$a = Formatter::convertPHPNamespacesToJava($a);
9+
$b = Formatter::convertPHPNamespacesToJava($b);
10+
return $a === $b;
11+
}
12+
}

tests/fixtures/java/ExtendingClassTest1.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ public static void main(String[] args)
44
{
55
ExtendingClassTest1 test = new ExtendingClassTest1();
66

7-
87
// Expected 99999 from SuperExtendingClassTest
98
System.out.println(test.value);
109
}

0 commit comments

Comments
 (0)