Skip to content

Commit 9cbbbe6

Browse files
committed
Fix: cannot call same place another class in JAR
1 parent fcf7e88 commit 9cbbbe6

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

src/Core/JavaArchive.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public function getClassByName(string $name): JavaClassInterface
227227
{
228228
$name = str_replace('/', '.', $name);
229229
if (!isset($this->classes[$name])) {
230-
throw new ClassNotFoundException($name . ' does not found on ' . $this->jarFile . '.');
230+
throw new ClassNotFoundException($name . ' does not exist on ' . $this->jarFile . '.');
231231
}
232232
return $this->classes[$name];
233233
}

src/Kernel/Internal/JavaClassDeferredLoader.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@ public function __construct(
2323

2424
public function __call($name, $arguments)
2525
{
26-
$this->initializeIfNotInitiated();
27-
return $this->javaClass->{$name}(...$arguments);
26+
return ($this->initializeIfNotInitiated())->{$name}(...$arguments);
27+
}
28+
29+
public function __invoke(...$arguments): JavaClassInterface
30+
{
31+
;
32+
return ($this->initializeIfNotInitiated())(...$arguments);
2833
}
2934

3035
private function initializeIfNotInitiated()
3136
{
3237
if (isset($this->javaClass)) {
3338
return $this->javaClass;
3439
}
35-
$this->javaClass = new JavaClass(
40+
return $this->javaClass = new JavaClass(
3641
new $this->deferLoadingReaderClass(...$this->arguments),
3742
$this->options
3843
);

tests/JarTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ class JarTest extends Base
88
{
99
protected $fixtures = [
1010
'OuterClassTest',
11+
'JarCalleeTest',
12+
'JarCallerTest',
1113
];
1214

1315
public function setUp(): void
1416
{
1517
parent::setUp();
1618
exec('cd ' . __DIR__ . '/caches && jar -cvfe JarTest.jar OuterClassTest OuterClassTest.class');
19+
exec('cd ' . __DIR__ . '/caches && jar -cvfe JarCallTest.jar JarCallerTest JarCalleeTest.class JarCallerTest.class');
1720
}
1821

1922
public function testCallWithEntryPoint()
@@ -47,4 +50,20 @@ public function testCallWithTargetedMethod()
4750
$result
4851
);
4952
}
53+
54+
public function testSamePlaceClass()
55+
{
56+
ob_start();
57+
(new JavaArchive(__DIR__ . '/caches/JarCallTest.jar'))
58+
->getClassByName('JarCallerTest')
59+
->getInvoker()
60+
->getStatic()
61+
->getMethods()
62+
->call(
63+
'main',
64+
[]
65+
);
66+
$result = ob_get_clean();
67+
$this->assertEquals("TEST\n", $result);
68+
}
5069
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class JarCalleeTest
2+
{
3+
public JarCalleeTest(String a)
4+
{
5+
System.out.println(a);
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class JarCallerTest
2+
{
3+
public static void main(String[] args)
4+
{
5+
new JarCalleeTest("TEST");
6+
}
7+
}
8+

0 commit comments

Comments
 (0)