Skip to content

Commit c445884

Browse files
committed
Add deferred loader
1 parent bfe9807 commit c445884

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ $javaClass = new JavaClass(
276276
| entrypoint | ?string | null | Specify to run entrypoint in JAR. | JavaArchive |
277277
| max_stack_exceeded | integer | 9999 | Execute more than the specified number of times be stopped the operation. | JavaClass |
278278
| strict | boolean | true | If strict mode is `true` then execute method, variables and so on with strict. But if strict mode is `false` then execute ambiguously method, variable and etc in PHPJava. | Both |
279-
| preload | boolean | false | preload is pre-read JavaClass in emulating JAR. This may be a lot of consuming memories by large JAR file. but if this is false then to use deffer loading. | JavaArchive |
279+
| preload | boolean | false | preload is pre-read JavaClass in emulating JAR. This may be a lot of consuming memories by large JAR file. but if this is false then to use defer loading. | JavaArchive |
280280
| validation.method.arguments_count_only | boolean | false | If this mode `true` then ClassResolver validate arguments size only. | JavaClass |
281281
| operations.enable_trace | boolean | true | Store operations history into memory if this is enabled. | JavaClass |
282282
| operations.temporary_code_stream | string | php://memory | TBD | JavaClass |

src/Core/JavaArchive.php

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

4+
use PHPJava\Core\JVM\Parameters\GlobalOptions;
45
use PHPJava\Core\JVM\Parameters\Runtime;
56
use PHPJava\Exceptions\UndefinedEntrypointException;
67
use PHPJava\Imitation\java\io\FileNotFoundException;
@@ -93,7 +94,17 @@ function ($fileName) {
9394
if (in_array($className, static::IGNORE_FILES)) {
9495
continue;
9596
}
96-
$this->classes[str_replace('/', '.', $className)] = new JavaClass(
97+
$classPath = str_replace('/', '.', $className);
98+
if (!($this->options['preload'] ?? GlobalOptions::get('preload') ?? Runtime::PRELOAD)) {
99+
$this->classes[$classPath] = new JavaClassDeferredLoader(
100+
JavaClassInlineReader::class,
101+
[$className, $code],
102+
$this->options
103+
);
104+
continue;
105+
}
106+
107+
$this->classes[$classPath] = new JavaClass(
97108
new JavaClassInlineReader(
98109
$className,
99110
$code
@@ -194,7 +205,7 @@ public function getClasses(): array
194205
return $this->classes;
195206
}
196207

197-
public function getClassByName(string $name): JavaClass
208+
public function getClassByName(string $name): JavaClassInterface
198209
{
199210
if (!isset($this->classes[$name])) {
200211
throw new ClassNotFoundException(str_replace('/', '.', $name) . ' does not found on ' . $this->jarFile . '.');

src/Core/JavaClass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use PHPJava\Utilities\DebugTool;
1717
use PHPJava\Utilities\Formatter;
1818

19-
class JavaClass
19+
class JavaClass implements JavaClassInterface
2020
{
2121
use \PHPJava\Kernel\Core\ConstantPool;
2222

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace PHPJava\Core;
3+
4+
class JavaClassDeferredLoader implements JavaClassInterface
5+
{
6+
7+
private $deferLoadingReaderClass;
8+
private $arguments = [];
9+
private $options = [];
10+
private $javaClass;
11+
12+
public function __construct(
13+
string $deferLoadingReaderClass,
14+
array $arguments = [],
15+
array $options = []
16+
) {
17+
$this->deferLoadingReaderClass = $deferLoadingReaderClass;
18+
$this->arguments = $arguments;
19+
$this->options = $options;
20+
}
21+
22+
public function __call($name, $arguments)
23+
{
24+
$this->initializeIfNotInitiated();
25+
return $this->javaClass->{$name}(...$arguments);
26+
}
27+
28+
private function initializeIfNotInitiated()
29+
{
30+
$this->javaClass = new JavaClass(
31+
new $this->deferLoadingReaderClass(...$this->arguments),
32+
$this->options
33+
);
34+
}
35+
}

src/Core/JavaClassInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace PHPJava\Core;
3+
4+
interface JavaClassInterface
5+
{
6+
}

0 commit comments

Comments
 (0)