Skip to content

Commit 61635e3

Browse files
committed
Fix
1 parent ccbf9fc commit 61635e3

File tree

7 files changed

+58
-14
lines changed

7 files changed

+58
-14
lines changed

src/Core/JVM/Invoker/Invokable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,9 @@ public function getList(): array
266266
{
267267
return $this->methods;
268268
}
269+
270+
public function has(string $name): bool
271+
{
272+
return count($this->methods[$name] ?? []) > 0;
273+
}
269274
}

src/Core/JVM/Invoker/InvokerInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ public function __construct(JavaClassInvoker $javaClassInvoker, array $methods);
99
public function call(string $name, ...$arguments);
1010
public function isDynamic(): bool;
1111
public function getList(): array;
12+
public function has(string $name): bool;
1213
}

src/Core/JavaArchive.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ public function getClasses(): array
209209

210210
public function getClassByName(string $name): JavaClassInterface
211211
{
212+
$name = str_replace('/', '.', $name);
212213
if (!isset($this->classes[$name])) {
213-
throw new ClassNotFoundException(str_replace('/', '.', $name) . ' does not found on ' . $this->jarFile . '.');
214+
throw new ClassNotFoundException($name . ' does not found on ' . $this->jarFile . '.');
214215
}
215216
return $this->classes[$name];
216217
}

src/Core/JavaClass.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,16 @@ public function __construct(JavaClassReaderInterface $reader, array $options = [
202202
$this,
203203
$options
204204
);
205-
}
206205

207-
// public function __debugInfo()
208-
// {
209-
// return [
210-
// 'className' => $this->getClassName(),
211-
// 'superClass' => get_class($this->getSuperClass()),
212-
// 'methods' => [
213-
// 'static' => array_keys($this->invoker->getStatic()->getMethods()->getList()),
214-
// 'dynamic' => array_keys($this->invoker->getDynamic()->getMethods()->getList()),
215-
// ],
216-
// ];
217-
// }
206+
if ($this->invoker->getStatic()->getMethods()->has('<clinit>')) {
207+
$this->invoker
208+
->getStatic()
209+
->getMethods()
210+
->call(
211+
'<clinit>'
212+
);
213+
}
214+
}
218215

219216
public function getClassName(bool $shortName = false): string
220217
{

src/Utilities/DebugTool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ public function getLogger(): Logger
3535
{
3636
return $this->logger;
3737
}
38-
}
38+
}

tests/KotlinTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace PHPJava\Tests;
3+
4+
use PHPJava\Core\JavaArchive;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class KotlinTest extends Base
8+
{
9+
public function setUp(): void
10+
{
11+
if (!$this->hasKotlin()) {
12+
return;
13+
}
14+
exec('kotlinc ' . __DIR__ . '/kotlin/HelloWorldKotlin.kt -include-runtime -d ' . __DIR__ . '/caches/HelloWorldKotlin.jar');
15+
}
16+
17+
public function testHelloWorldOnKotlin()
18+
{
19+
if (!$this->hasKotlin()) {
20+
$this->markTestSkipped('Not installed kotlin in your environment.');
21+
return;
22+
}
23+
24+
ob_start();
25+
$jar = new JavaArchive(__DIR__ . '/caches/HelloWorldKotlin.jar');
26+
$jar->execute([]);
27+
$result = rtrim(ob_get_clean());
28+
29+
$this->assertEquals('Hello World!', $result);
30+
}
31+
32+
private function hasKotlin()
33+
{
34+
exec('which kotlinc', $output);
35+
return count($output) > 0;
36+
}
37+
}

tests/kotlin/HelloWorldKotlin.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fun main(args: Array<String>) {
2+
println("Hello World!")
3+
}

0 commit comments

Comments
 (0)