Skip to content

Commit ccb1326

Browse files
committed
WIP commit
1 parent 23500f8 commit ccb1326

6 files changed

Lines changed: 50 additions & 24 deletions

File tree

src/core/JavaClassInvoker.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,26 @@ public function __construct(JavaClass $javaClass)
105105
}
106106
}
107107

108+
public function construct(): self
109+
{
110+
if (isset($this->staticMethods['<init>'])) {
111+
$this->getStaticMethods()->call('<init>');
112+
}
113+
114+
// reset dynamic fields
115+
$this->dynamicFieldAccessor = new JVM\Field\DynamicField(
116+
$this,
117+
[]
118+
);
119+
120+
$this->dynamicMethodAccessor = new JVM\Invoker\DynamicMethodInvoker(
121+
$this,
122+
$this->dynamicMethods
123+
);
124+
125+
return $this;
126+
}
127+
108128
public function getJavaClass(): JavaClass
109129
{
110130
return $this->javaClass;

src/core/jvm/field/FieldGettable.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22
namespace PHPJava\Core\JVM\Field;
33

44
use PHPJava\Core\JavaClassInvoker;
5+
use PHPJava\Imitation\java\lang\_String;
6+
use PHPJava\Imitation\java\lang\NoSuchFieldException;
57

68
trait FieldGettable
79
{
10+
/**
11+
* @param $name
12+
* @return mixed
13+
* @throws NoSuchFieldException
14+
*/
815
public function get(string $name)
916
{
17+
if (!isset($this->fields[$name])) {
18+
throw new NoSuchFieldException('Get to undefined field ' . $name);
19+
}
20+
if ($this->fields[$name] instanceof _String) {
21+
return (string) $this->fields[$name];
22+
}
1023
return $this->fields[$name];
1124
}
1225
}

src/core/jvm/field/FieldSettable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ trait FieldSettable
88
public function set(string $name, $value)
99
{
1010
$this->fields[$name] = $value;
11+
return $this;
1112
}
1213
}

src/core/jvm/field/StaticField.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,17 @@
33

44
use PHPJava\Core\JavaClassInvoker;
55
use PHPJava\Imitation\java\lang\_String;
6-
use PHPJava\Imitation\java\lang\NoSuchFieldException;
76

87
class StaticField
98
{
10-
public $fields = [];
9+
use FieldGettable;
10+
use FieldSettable;
1111

1212
private $javaClassInvoker;
13+
public $fields = [];
1314

1415
public function __construct(JavaClassInvoker $javaClassInvoker)
1516
{
1617
$this->javaClassInvoker = $javaClassInvoker;
1718
}
18-
19-
/**
20-
* @param $name
21-
* @return mixed
22-
* @throws NoSuchFieldException
23-
*/
24-
public function get(string $name)
25-
{
26-
if (!isset($this->fields[$name])) {
27-
throw new NoSuchFieldException('Get to undefined static field ' . $name);
28-
}
29-
if ($this->fields[$name] instanceof _String) {
30-
return (string) $this->fields[$name];
31-
}
32-
return $this->fields[$name];
33-
}
34-
35-
public function set(string $name, $value)
36-
{
37-
$this->fields[$name] = $value;
38-
return $this;
39-
}
4019
}

src/imitation/java/lang/Throwable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ class Throwable extends \Exception
55
{
66
use \PHPJava\Imitation\PHPJava\Extended\_Object;
77

8+
public function __construct($message = "", $code = 0, Throwable $previous = null)
9+
{
10+
parent::__construct($message, $code, $previous);
11+
}
12+
813
public function __toString(): string
914
{
1015
return $this->getMessage();

tools/test.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
new \PHPJava\Core\JavaClassReader(__DIR__ . '/Test.class')
1010
);
1111

12+
13+
var_dump(
14+
$javaClass->getInvoker()
15+
->construct()
16+
->getDynamicFields()
17+
->get('z')
18+
);
19+
1220
$javaClass->getInvoker()->getStaticFields()
1321
->set('c', 100)
1422
->set('b', 'New String');

0 commit comments

Comments
 (0)