Skip to content

Commit 2803b1c

Browse files
committed
WIP commit
1 parent e460118 commit 2803b1c

6 files changed

Lines changed: 65 additions & 15 deletions

File tree

src/Core/JVM/Invoker/Invokable.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ function ($argument) {
8080
);
8181

8282
$method = null;
83+
84+
// the special method
8385
foreach ($methodReferences as $methodReference) {
8486
$methodSignature = Formatter::buildArgumentsSignature(
8587
Formatter::parseSignature($constantPool[$methodReference->getDescriptorIndex()]->getString())['arguments']
@@ -92,6 +94,8 @@ function ($argument) {
9294
}
9395

9496
if ($method === null) {
97+
// var_dump($convertedPassedArguments);
98+
// debug_print_backtrace();
9599
throw new NoSuchMethodException('Call to undefined method ' . $name . '.');
96100
}
97101

src/Core/JavaClass.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PHPJava\Core\JVM\ConstantPool;
99
use PHPJava\Core\JVM\Validations\MagicByte;
1010
use PHPJava\Exceptions\ValidatorException;
11+
use PHPJava\Kernel\Attributes\AttributeInterface;
12+
use PHPJava\Kernel\Attributes\InnerClassesAttribute;
1113
use PHPJava\Kernel\Maps\AccessFlag;
1214
use PHPJava\Kernel\Structures\_Utf8;
1315
use PHPJava\Utilities\Formatter;
@@ -62,6 +64,8 @@ class JavaClass
6264
*/
6365
private $invoker;
6466

67+
private $innerClasses = [];
68+
6569
/**
6670
* JavaClass constructor.
6771
* @param JavaClassReader $reader
@@ -94,6 +98,7 @@ public function __construct(JavaClassReader $reader)
9498
$this->thisClass = $reader->getBinaryReader()->readUnsignedShort();
9599

96100
$constantPoolEntries = $this->constantPool->getEntries();
101+
97102
$this->className = $constantPoolEntries[$constantPoolEntries[$this->thisClass]->getClassIndex()];
98103

99104
// read super class
@@ -127,14 +132,32 @@ public function __construct(JavaClassReader $reader)
127132
$this->constantPool
128133
);
129134

135+
foreach ($this->activeAttributes->getEntries() as $entry) {
136+
if ($entry->getAttributeData() instanceof InnerClassesAttribute) {
137+
$this->innerClasses = array_merge(
138+
$this->innerClasses,
139+
$entry->getAttributeData()->getClasses()
140+
);
141+
}
142+
}
143+
130144
$this->invoker = new JavaClassInvoker($this);
131145
}
132146

133-
public function getClassName(): string
147+
public function getClassName(bool $shortName = false): string
134148
{
149+
if ($shortName === true) {
150+
$split = explode('$', $this->className->getString());
151+
return $split[count($split) - 1];
152+
}
135153
return $this->className->getString();
136154
}
137155

156+
public function getInnerClasses(): array
157+
{
158+
return $this->innerClasses;
159+
}
160+
138161
public function getFields(): array
139162
{
140163
return $this->activeFields->getEntries();

src/Kernel/Attributes/InnerClassesAttribute.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace PHPJava\Kernel\Attributes;
33

44
use PHPJava\Exceptions\NotImplementedException;
5+
use PHPJava\Kernel\Structures\_Classes;
56
use PHPJava\Utilities\BinaryTool;
67

78
final class InnerClassesAttribute implements AttributeInterface
@@ -10,20 +11,21 @@ final class InnerClassesAttribute implements AttributeInterface
1011
use \PHPJava\Kernel\Core\ConstantPool;
1112

1213
private $numberOfClasses = 0;
13-
private $classes = array();
14+
private $classes = [];
15+
1416
public function execute(): void
1517
{
1618
$this->numberOfClasses = $this->readUnsignedShort();
1719
for ($i = 0; $i < $this->numberOfClasses; $i++) {
18-
$thises[$i] = new JavaStructureClasses($this);
19-
$thises[$i]->setInnerClassInfoIndex($this->readUnsignedShort());
20-
$thises[$i]->setOuterClassInfoIndex($this->readUnsignedShort());
21-
$thises[$i]->setInnerNameIndex($this->readUnsignedShort());
22-
$thises[$i]->setInnerClassAccessFlag($this->readUnsignedShort());
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());
2325
}
2426
}
25-
public function getClasses()
27+
public function getClasses(): array
2628
{
27-
return $thises;
29+
return $this->classes;
2830
}
2931
}

src/Kernel/Mnemonics/_new.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,21 @@ public function execute(): void
2626
/**
2727
* @var \PHPJava\Core\JavaClass $classObject
2828
*/
29-
$this->pushStack(
30-
$classObject
31-
->getInvoker()
32-
->construct()
33-
->getJavaClass()
29+
$classObject = $classObject
30+
->getInvoker()
31+
->construct()
32+
->getJavaClass();
33+
34+
$className = explode(
35+
'$',
36+
$classObject->getClassName()
3437
);
38+
39+
if (count($className) > 1) {
40+
$classObject->setParentClass($this->javaClass);
41+
}
42+
43+
$this->pushStack($classObject);
3544
return;
3645
}
3746
$this->pushStack(new $classObject());

src/Utilities/ClassResolver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public static function resolve($javaPath): array
3434
foreach (static::$resolves as [$resourceType, $value]) {
3535
switch ($resourceType) {
3636
case static::RESOURCE_TYPE_FILE:
37-
$path = realpath($value . '/' . $relativePath . '.class');
37+
$relativePathByMainClass = explode('$', $relativePath, 2)[0];
38+
$path = realpath($value . '/' . $relativePathByMainClass . '.class');
3839
if (($key = array_search($path, static::$resolvedPaths, true)) !== false) {
3940
return static::$resolvedPaths[$key];
4041
}

src/Utilities/TypeResolver.php

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

4+
use PHPJava\Core\JavaClass;
45
use PHPJava\Exceptions\TypeException;
56

67
class TypeResolver
@@ -76,6 +77,16 @@ public static function convertPHPtoJava($arguments, $defaultJavaArgumentType = '
7677
$firstParameter['deep_array'] += $deepArray;
7778
return $firstParameter;
7879
}
80+
if ($phpType === 'object') {
81+
if ($arguments instanceof JavaClass) {
82+
return [
83+
'type' => 'class',
84+
'class_name' => $arguments->getClassName(false),
85+
'deep_array' => $deepArray,
86+
];
87+
}
88+
throw new TypeException(get_class($arguments) . ' does not supported to convert to Java\'s argument.');
89+
}
7990
$resolveType = static::SIGNATURE_MAP[static::PHP_TYPE_MAP[$phpType][0]] ?? null;
8091
if ($resolveType === 'class') {
8192
return [

0 commit comments

Comments
 (0)