Skip to content

Commit 631a9be

Browse files
committed
Implement field/property compiler
1 parent 4eea015 commit 631a9be

28 files changed

+668
-342
lines changed

docs/compiler/README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ $enhancedConstantPool = ConstantPoolEnhancer::factory(
4747
$finder
4848
);
4949

50+
$className = 'HelloWorld';
51+
5052
$enhancedConstantPool
5153
->addString('Hello PHPJava Compiler!')
5254
->addClass(_Object::class)
53-
->addClass('HelloWorld')
55+
->addClass($className)
5456
->addClass(System::class)
5557
->addClass(PrintStream::class)
5658
->addFieldref(
@@ -85,7 +87,7 @@ $compiler = new Compiler(
8587
->enableSuper()
8688
->make()
8789
)
88-
->setThisClass($enhancedConstantPool->findClass('HelloWorld'))
90+
->setThisClass($enhancedConstantPool->findClass($className))
8991
->setSuperClass($enhancedConstantPool->findClass(_Object::class))
9092
->setMethods(
9193
(new Methods())
@@ -95,14 +97,15 @@ $compiler = new Compiler(
9597
->enablePublic()
9698
->enableStatic()
9799
->make(),
98-
$enhancedConstantPool->findUtf8('main'),
99-
$enhancedConstantPool->findUtf8(
100-
(new Descriptor())
101-
->addArgument(_String::class, 1)
102-
->setReturn(_Void::class)
103-
->make()
104-
)
100+
$className,
101+
'main',
102+
(new Descriptor())
103+
->addArgument(_String::class, 1)
104+
->setReturn(_Void::class)
105+
->make()
105106
))
107+
->setConstantPool($constantPool)
108+
->setConstantPoolFinder($finder)
106109
->setAttributes(
107110
(new Attributes())
108111
->add(
@@ -160,7 +163,7 @@ $compiler = new Compiler(
160163
->setConstantPool($constantPool->toArray())
161164
);
162165

163-
$compiler->compile(fopen('HelloWorld.class', 'w+'));
166+
$compiler->compile(fopen($className . '.class', 'w+'));
164167
```
165168

166169
Run `java` command after the `HelloWorld.class` file is generated.

src/Compiler/Builder/Collection/AbstractEntryCollection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ public function enableIntern(bool $enable): EntryCollectionInterface
7676
$this->enableIntern = $enable;
7777
return $this;
7878
}
79+
80+
public function length(): int
81+
{
82+
return count($this->entries);
83+
}
7984
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace PHPJava\Compiler\Builder\Collection;
3+
4+
use PHPJava\Compiler\Builder\Structures\EntryCollectionInterface;
5+
6+
class Fields extends AbstractEntryCollection implements EntryCollectionInterface, \ArrayAccess
7+
{
8+
}

src/Compiler/Builder/Field.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
namespace PHPJava\Compiler\Builder;
3+
4+
use PHPJava\Compiler\Builder\Maps\EntryMap;
5+
use PHPJava\Compiler\Lang\Assembler\Traits\ConstantPoolManageable;
6+
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\ConstantPoolEnhanceable;
7+
8+
class Field implements BuilderInterface, EntryInterface
9+
{
10+
use ConstantPoolManageable;
11+
use ConstantPoolEnhanceable;
12+
13+
private $attributes = [];
14+
private $accessFlags = 0;
15+
protected $value;
16+
17+
/**
18+
* @var string
19+
*/
20+
private $className;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $name;
26+
27+
/**
28+
* @var string
29+
*/
30+
private $descriptor;
31+
32+
public function __construct(
33+
int $accessFlags,
34+
string $className,
35+
string $name,
36+
string $descriptor
37+
) {
38+
$this->accessFlags = $accessFlags;
39+
$this->className = $className;
40+
$this->name = $name;
41+
$this->descriptor = $descriptor;
42+
}
43+
44+
public function getAccessFlags(): int
45+
{
46+
return $this->accessFlags;
47+
}
48+
49+
public function getClassName(): string
50+
{
51+
return $this->className;
52+
}
53+
54+
public function getName(): string
55+
{
56+
return $this->name;
57+
}
58+
59+
public function getDescriptor(): string
60+
{
61+
return $this->descriptor;
62+
}
63+
64+
public function getNameIndex(): int
65+
{
66+
/**
67+
* @var EntryMap $result
68+
*/
69+
$result = $this->getEnhancedConstantPool()
70+
->findUtf8($this->name)
71+
->getResult();
72+
return $result
73+
->getEntryIndex();
74+
}
75+
76+
public function getDescriptorIndex(): int
77+
{
78+
/**
79+
* @var EntryMap $result
80+
*/
81+
$result = $this->getEnhancedConstantPool()
82+
->findUtf8($this->descriptor)
83+
->getResult();
84+
return $result
85+
->getEntryIndex();
86+
}
87+
88+
public function getAttributes(): array
89+
{
90+
return $this->attributes;
91+
}
92+
93+
public function setAttributes(array $attributes): self
94+
{
95+
$this->attributes = $attributes;
96+
return $this;
97+
}
98+
99+
public function setValue(string $value): self
100+
{
101+
$this->value = $value;
102+
return $this;
103+
}
104+
105+
public function getValue(): string
106+
{
107+
return $this->value;
108+
}
109+
}

src/Compiler/Builder/Method.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,73 @@
11
<?php
22
namespace PHPJava\Compiler\Builder;
33

4-
use PHPJava\Compiler\Builder\Finder\Result\FinderResultInterface;
54
use PHPJava\Compiler\Builder\Maps\EntryMap;
5+
use PHPJava\Compiler\Lang\Assembler\Traits\ConstantPoolManageable;
6+
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\ConstantPoolEnhanceable;
67

78
class Method implements BuilderInterface, EntryInterface
89
{
10+
use ConstantPoolManageable;
11+
use ConstantPoolEnhanceable;
12+
913
private $attributes = [];
1014
private $accessFlags = 0;
1115

1216
/**
13-
* @var FinderResultInterface
17+
* @var string
18+
*/
19+
private $className;
20+
21+
/**
22+
* @var string
1423
*/
15-
private $nameIndex;
24+
private $name;
1625

1726
/**
18-
* @var FinderResultInterface
27+
* @var string
1928
*/
20-
private $descriptorIndex;
29+
private $descriptor;
2130

2231
public function __construct(
2332
int $accessFlags,
24-
FinderResultInterface $nameIndex,
25-
FinderResultInterface $descriptorIndex
33+
string $className,
34+
string $name,
35+
string $descriptor
2636
) {
2737
$this->accessFlags = $accessFlags;
28-
$this->nameIndex = $nameIndex;
29-
$this->descriptorIndex = $descriptorIndex;
38+
$this->className = $className;
39+
$this->name = $name;
40+
$this->descriptor = $descriptor;
3041
}
3142

3243
public function getAccessFlags(): int
3344
{
3445
return $this->accessFlags;
3546
}
3647

48+
public function getClassName(): string
49+
{
50+
return $this->className;
51+
}
52+
53+
public function getName(): string
54+
{
55+
return $this->name;
56+
}
57+
58+
public function getDescriptor(): string
59+
{
60+
return $this->descriptor;
61+
}
62+
3763
public function getNameIndex(): int
3864
{
3965
/**
4066
* @var EntryMap $result
4167
*/
42-
$result = $this->nameIndex->getResult();
68+
$result = $this->getEnhancedConstantPool()
69+
->findUtf8($this->name)
70+
->getResult();
4371
return $result
4472
->getEntryIndex();
4573
}
@@ -49,7 +77,9 @@ public function getDescriptorIndex(): int
4977
/**
5078
* @var EntryMap $result
5179
*/
52-
$result = $this->descriptorIndex->getResult();
80+
$result = $this->getEnhancedConstantPool()
81+
->findUtf8($this->descriptor)
82+
->getResult();
5383
return $result
5484
->getEntryIndex();
5585
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
<?php
22
namespace PHPJava\Compiler\Builder\Segments;
33

4+
use PHPJava\Compiler\Builder\Field;
5+
46
class Fields extends AbstractSegment implements SegmentInterface
57
{
68
public function build(): void
79
{
10+
foreach ($this->classFileStructureBuilder->getFields() as $field) {
11+
/**
12+
* @var Field $field
13+
*/
14+
$this->binaryWriter->writeUnsignedShort($field->getAccessFlags());
15+
$this->binaryWriter->writeUnsignedShort($field->getNameIndex());
16+
$this->binaryWriter->writeUnsignedShort($field->getDescriptorIndex());
17+
18+
// attributes_count
19+
$this->binaryWriter->writeUnsignedShort(0);
20+
}
821
}
922
}

src/Compiler/Builder/Signatures/Descriptor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function make(): string
4141
static function (array $argument) {
4242
[$type, $deepArray] = $argument;
4343
$string = str_repeat('[', $deepArray);
44+
$type = ltrim($type, '\\');
4445
switch ($type) {
4546
case _Char::class:
4647
case _Byte::class:
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace PHPJava\Compiler\Builder\Signatures;
4+
5+
use PHPJava\Kernel\Maps\FieldAccessFlag as Flag;
6+
7+
class FieldAccessFlag extends AbstractAccessFlag implements AccessFlagInterface
8+
{
9+
public function enablePublic(): self
10+
{
11+
$this->flagValue |= Flag::ACC_PUBLIC;
12+
return $this;
13+
}
14+
15+
public function enableStatic(): self
16+
{
17+
$this->flagValue |= Flag::ACC_STATIC;
18+
return $this;
19+
}
20+
21+
public function enableFinal(): self
22+
{
23+
$this->flagValue |= Flag::ACC_FINAL;
24+
return $this;
25+
}
26+
27+
public function enableProtected(): self
28+
{
29+
$this->flagValue |= Flag::ACC_PROTECTED;
30+
return $this;
31+
}
32+
33+
public function enablePrivate(): self
34+
{
35+
$this->flagValue |= Flag::ACC_PRIVATE;
36+
return $this;
37+
}
38+
39+
public function enableSynthetic(): self
40+
{
41+
$this->flagValue |= Flag::ACC_SYNTHETIC;
42+
return $this;
43+
}
44+
45+
public function enableTransient(): self
46+
{
47+
$this->flagValue |= Flag::ACC_TRANSIENT;
48+
return $this;
49+
}
50+
51+
public function enableEnum(): self
52+
{
53+
$this->flagValue |= Flag::ACC_ENUM;
54+
return $this;
55+
}
56+
}

src/Compiler/Lang/Assembler/Bundler/AbstractBundler.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPJava\Compiler\Lang\Assembler\Bundler;
44

55
use PHPJava\Compiler\Builder\Collection\ConstantPool;
6+
use PHPJava\Compiler\Builder\Collection\Fields;
67
use PHPJava\Compiler\Builder\Collection\Methods;
78
use PHPJava\Compiler\Builder\Finder\ConstantPoolFinder;
89
use PHPJava\Compiler\Lang\Assembler\Traits\ConstantPoolManageable;
@@ -27,6 +28,11 @@ abstract class AbstractBundler
2728
*/
2829
protected $methods;
2930

31+
/**
32+
* @var Fields
33+
*/
34+
protected $fields;
35+
3036
/**
3137
* @var StreamReaderInterface
3238
*/

0 commit comments

Comments
 (0)