Skip to content

Commit e5586d9

Browse files
authored
Merge pull request #142 from php-java/implement-call-method-initializer
Fix #139
2 parents 3e5102b + ac8c3cc commit e5586d9

8 files changed

Lines changed: 81 additions & 33 deletions

File tree

src/Core/JVM/Invoker/Invokable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public function call(string $name, ...$arguments)
7272
$constantPool = $currentConstantPool->getEntries();
7373
$convertedPassedArguments = $this->stringifyArguments(...$arguments);
7474

75+
/**
76+
* @var _MethodInfo $method
77+
*/
7578
$method = $operationCache->fetchOrPush(
7679
"{$name}.{$convertedPassedArguments}",
7780
function () use ($name, $arguments) {
@@ -82,6 +85,8 @@ function () use ($name, $arguments) {
8285
}
8386
);
8487

88+
$currentConstantPool = $method->getConstantPool() ?? $currentConstantPool;
89+
8590
if ($method instanceof FlexibleMethod) {
8691
/**
8792
* @var FlexibleMethod $method

src/Core/JavaClass.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,15 @@ public function __construct(ReaderInterface $reader, array $options = [])
135135
$reader->getBinaryReader()->readUnsignedShort()
136136
);
137137

138-
$constantPoolEntries = $this->constantPool->getEntries();
139-
140-
$this->debugTool->getLogger()->info('Constant Pools: ' . count($constantPoolEntries));
138+
$this->debugTool->getLogger()->info('Constant Pools: ' . count($this->constantPool));
141139

142140
// read access flag
143141
$this->accessFlag = $reader->getBinaryReader()->readUnsignedShort();
144142

145143
// read this class
146144
$this->thisClass = $reader->getBinaryReader()->readUnsignedShort();
147145

148-
$this->className = $constantPoolEntries[$constantPoolEntries[$this->thisClass]->getClassIndex()];
146+
$this->className = $this->constantPool[$this->constantPool[$this->thisClass]->getClassIndex()];
149147

150148
// read super class
151149
$this->superClassIndex = $reader->getBinaryReader()->readUnsignedShort();
@@ -205,14 +203,26 @@ public function __construct(ReaderInterface $reader, array $options = [])
205203

206204
$this->debugTool->getLogger()->info('Extracted attributes: ' . count($this->attributePool));
207205

206+
$innerClasses = [];
208207
foreach ($this->attributePool as $entry) {
209208
if ($entry->getAttributeData() instanceof InnerClassesAttribute) {
210-
$this->innerClasses = array_merge(
211-
$this->innerClasses,
209+
$innerClasses = array_merge(
210+
$innerClasses,
212211
$entry->getAttributeData()->getClasses()
213212
);
214213
}
215214
}
215+
216+
// Add to class resolver
217+
foreach ($innerClasses as $innerClass) {
218+
$this->options['class_resolver']->add([
219+
[
220+
ClassResolver::RESOURCE_TYPE_INNER_CLASS,
221+
$innerClass
222+
],
223+
]);
224+
}
225+
216226
$this->debugTool->getLogger()->info('End of Class');
217227

218228
$this->invoker = new JavaClassInvoker(

src/Kernel/Attributes/InnerClassesAttribute.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ public function execute(): void
1717
{
1818
$this->numberOfClasses = $this->readUnsignedShort();
1919
for ($i = 0; $i < $this->numberOfClasses; $i++) {
20-
$this->classes[$i] = new _Classes($this->reader);
21-
$this->classes[$i]->setInnerClassInfoIndex($this->readUnsignedShort());
22-
$this->classes[$i]->setOuterClassInfoIndex($this->readUnsignedShort());
23-
$this->classes[$i]->setInnerNameIndex($this->readUnsignedShort());
24-
$this->classes[$i]->setInnerClassAccessFlag($this->readUnsignedShort());
20+
$class = new _Classes($this->reader);
21+
$class->setConstantPool($this->getConstantPool())
22+
->setDebugTool($this->getDebugTool());
23+
$class->execute();
24+
$this->classes[] = $class;
2525
}
2626
}
2727

src/Kernel/Structures/_Classes.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,10 @@ class _Classes implements StructureInterface
1414

1515
public function execute(): void
1616
{
17-
}
18-
19-
public function setInnerClassInfoIndex($innerClassInfoIndex)
20-
{
21-
$this->innerClassInfoIndex = $innerClassInfoIndex;
22-
}
23-
24-
public function setOuterClassInfoIndex($outerClassInfoIndex)
25-
{
26-
$this->outerClassInfoIndex = $outerClassInfoIndex;
27-
}
28-
29-
public function setInnerNameIndex($innerNameIndex)
30-
{
31-
$this->innerNameIndex = $innerNameIndex;
32-
}
33-
34-
public function setInnerClassAccessFlag($innerClassAccessFlag)
35-
{
36-
$this->innerClassAccessFlag = $innerClassAccessFlag;
17+
$this->innerClassInfoIndex = $this->readUnsignedShort();
18+
$this->outerClassInfoIndex = $this->readUnsignedShort();
19+
$this->innerNameIndex = $this->readUnsignedShort();
20+
$this->innerClassAccessFlag = $this->readUnsignedShort();
3721
}
3822

3923
public function getInnerClassInfoIndex()

src/Utilities/ClassResolver.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPJava\Core\JVM\Parameters\Runtime;
77
use PHPJava\Core\Stream\Reader\FileReader;
88
use PHPJava\Core\Stream\Reader\ReaderInterface;
9+
use PHPJava\Kernel\Structures\_Classes;
910
use PHPJava\Packages\java\lang\ClassNotFoundException;
1011

1112
class ClassResolver
@@ -16,6 +17,7 @@ class ClassResolver
1617
const RESOURCE_TYPE_FILE = 'RESOURCE_TYPE_FILE';
1718
const RESOURCE_TYPE_JAR = 'RESOURCE_TYPE_JAR';
1819
const RESOURCE_TYPE_CLASS = 'RESOURCE_TYPE_CLASS';
20+
const RESOURCE_TYPE_INNER_CLASS = 'RESOURCE_TYPE_INNER_CLASS';
1921

2022
// resolved types
2123
const RESOLVED_TYPE_CLASS = 'RESOLVED_TYPE_CLASS';
@@ -42,11 +44,17 @@ public function resolve(string $javaPath, JavaClass $class = null): array
4244
$relativePath = implode('/', $namespaces);
4345
foreach ($this->resolves as [$resourceType, $value]) {
4446
switch ($resourceType) {
47+
case static::RESOURCE_TYPE_INNER_CLASS:
48+
// TODO: Implement here
49+
break;
4550
case static::RESOURCE_TYPE_FILE:
4651
$path = realpath($value . '/' . $relativePath . '.class');
4752
if (($key = array_search($path, $this->resolvedPaths, true)) !== false) {
4853
return $this->resolvedPaths[$key];
4954
}
55+
/**
56+
* @var JavaClass $initiatedClass
57+
*/
5058
if (is_file($path)) {
5159
$initiatedClass = new JavaClass(
5260
new FileReader($path),
@@ -73,7 +81,7 @@ public function resolve(string $javaPath, JavaClass $class = null): array
7381
} catch (ClassNotFoundException $e) {
7482
}
7583
break;
76-
case static::RESOLVED_TYPE_CLASS:
84+
case static::RESOURCE_TYPE_CLASS:
7785
/**
7886
* @var ReaderInterface $value
7987
*/

tests/EnclosingMethodTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace PHPJava\Tests;
3+
4+
class EnclosingMethodTest extends Base
5+
{
6+
protected $fixtures = [
7+
'EnclosingMethodTest',
8+
];
9+
10+
public function testEnclosingMethod()
11+
{
12+
ob_start();
13+
$this->initiatedJavaClasses['EnclosingMethodTest']
14+
->getInvoker()
15+
->getStatic()
16+
->getMethods()
17+
->call(
18+
'main',
19+
[]
20+
);
21+
$result = ob_get_clean();
22+
$this->assertEquals("Hello World!\n", $result);
23+
}
24+
}

tests/ObjectCompareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ObjectCompareTest extends Base
77
'ObjectCompareTest',
88
];
99

10-
public function testCallTableswitchPattern1()
10+
public function testObjectCompareTest()
1111
{
1212
ob_start();
1313
$calculatedValue = $this->initiatedJavaClasses['ObjectCompareTest']
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class EnclosingMethodTest
2+
{
3+
public String text;
4+
5+
public static void main(String[] args)
6+
{
7+
new EnclosingMethodTest() {{
8+
this.text = "Hello World!";
9+
callHelloWorld();
10+
}};
11+
}
12+
13+
public void callHelloWorld()
14+
{
15+
System.out.println(this.text);
16+
}
17+
}

0 commit comments

Comments
 (0)