Skip to content

Commit 26102be

Browse files
committed
feat: constructors & fields
1 parent 88bd461 commit 26102be

File tree

3 files changed

+83
-10
lines changed

3 files changed

+83
-10
lines changed

docs/reference/class-building.md

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,44 @@ The following "usage" section of each method will build out a `Dog` class in Lua
2222

2323
---
2424

25+
### `classBuilder:field(name, type, access, definition)`
26+
27+
Creates or overrides a constructor.
28+
29+
#### Parameters
30+
31+
1. `name` - `string`: The name of the field to create.
32+
2. `type` - `userdata [class]`: The class type of the field.
33+
3. `access` - `table?`: Access flags for the field. See [Access Modifier Table](#access-modifier-table)
34+
4. `definition` - `any?`: Optional value or function for defining the field.
35+
36+
#### Usage
37+
38+
If the field is not final, the definition is optional.
39+
40+
<<< @/reference/snippets/code/Dog.lua#field{1 Lua:line-numbers=4}
41+
42+
---
43+
44+
### `classBuilder:constructor(parameters, access, definesFields)`
45+
46+
Creates a field.
47+
48+
#### Parameters
49+
50+
1. `parameters` - `table<userdata [class]>`: The parameters of the constructor.
51+
2. `access` - `table?`: Access flags for the constructor. See [Access Modifier Table](#access-modifier-table)
52+
3. `definedFields` - `boolean?`: Whether or not to define instance fields on this constructor.
53+
54+
55+
#### Usage
56+
57+
If the constructor has a parameter match with a constructor in the parent class, the function to define it is optional. If `definedFields` is set, instance fields created by `classBuilder:field()` are initialized in it. If the access table is nil, the constructor defaults to public access.
58+
59+
<<< @/reference/snippets/code/Dog.lua#constructor{1 Lua:line-numbers=10}
60+
61+
---
62+
2563
### `classBuilder:override(methodName, parameters, access)`
2664

2765
Override an existing method of the parent class.
@@ -30,15 +68,14 @@ Override an existing method of the parent class.
3068

3169
1. `methodName` - `string`: The name of the method to override.
3270
2. `parameters` - `table<userdata [class]>`: The parameters of the method to override.
33-
3. `access` - `{ static = boolean? }`: Access flags for the overriding method. Set `static` if the method to override is static.
34-
4. `func` - `function`: The function to call for handling when the method is invoked.
71+
3. `access` - `table`: Access flags for the overriding method. See [Access Modifier Table](#access-modifier-table)
3572

3673
#### Usage
3774

3875
In addition to overriding the method we must supply a function to the builder on the index matching `methodName`.
3976
If the overriding method is not static then the first parameter to the submitted function is `this`, followed by the rest of the parameters specified in `parameters`.
4077

41-
<<< @/reference/snippets/code/Dog.lua#override{1 Lua:line-numbers=4}
78+
<<< @/reference/snippets/code/Dog.lua#override{1 Lua:line-numbers=15}
4279

4380
---
4481

@@ -51,15 +88,14 @@ Create an entirely new method on the class.
5188
1. `methodName` - `string`: The method name. Must not exist in any parent classes.
5289
2. `parameters` - `table<userdata [class]>`: A table representing the parameter types in order.
5390
3. `returnClass` - `userdata [class]`: The class to be returned by this method.
54-
4. `access` - `{ abstract = boolean?, static = boolean? }`: Access flags for overriding the method.
55-
5. `func` - `function`: The function to call for handling when the method is invoked. If the parameter `access.abstract` is `true`, this parameter is ignored.
91+
4. `access` - `table`: Access flags for the overriding method. See [Access Modifier Table](#access-modifier-table)
5692

5793
#### Usage
5894

5995
If the method to be created is abstract, a function does not have to be submitted.
6096
If the method is not static then the first parameter to the submitted function is `this`, followed by the rest of the parameters specified in `parameters`.
6197

62-
<<< @/reference/snippets/code/Dog.lua#create{1 Lua:line-numbers=21}
98+
<<< @/reference/snippets/code/Dog.lua#create{1 Lua:line-numbers=34}
6399

64100
---
65101

@@ -73,7 +109,27 @@ Builds the class.
73109

74110
#### Usage
75111

76-
<<< @/reference/snippets/code/Dog.lua#build{1 Lua:line-numbers=26}
112+
<<< @/reference/snippets/code/Dog.lua#build{1 Lua:line-numbers=39}
113+
114+
---
115+
116+
### Access Modifier Table
117+
118+
Access modifier tables are used in almost every class builder method. They are defined as:
119+
120+
```Lua
121+
{
122+
public = boolean?,
123+
protected = boolean?,
124+
private = boolean?,
125+
static = boolean?,
126+
final = boolean?,
127+
abstract = boolean?,
128+
interface = boolean?
129+
}
130+
```
131+
132+
Depending on the method being called, these modifiers may or may not be used.
77133

78134
## Mixin Class Builder
79135

docs/reference/java-lib.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ Provides a class builder extending from the given `superclass`. Optionally appli
178178

179179
1. `superclass` - `userdata [class]`: The parent class of the class being built.
180180
2. `interfaces` - `table<userdata [class]>?`: An optional table of interfaces to be applied to the class.
181-
3. `access` - `{ static = boolean?, interface = boolean?, abstract = boolean? }?`: An optional table of flags to define properties of the class.
181+
3. `access` - `table`: Optional access flags for the class being built. See [Access Modifier Table](#access-modifier-table).
182182

183183
### Returns
184184

docs/reference/snippets/code/Dog.lua

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,30 @@ local Animal = require("com.example.Animal")
33

44
local dogClassBuilder = java.extendClass(Animal)
55
-- #endregion init
6+
-- #region field
7+
local setField = {}
8+
dogClassBuilder:field("weight", java.int, { final = true }, function(this)
9+
local weight = setField[this]
10+
setField[this] = nil
11+
return weight
12+
end)
13+
-- #endregion field
14+
-- #region constructor
15+
dogClassBuilder:constructor({java.int, java.int}, nil, true)
16+
function dogClassBuilder:constructor(self, speed, weight)
17+
self:super(speed)
18+
setField[self] = weight
19+
end
20+
-- #endregion constructor
621
-- #region override
722
dogClassBuilder:override("noise", {java.boolean, java.int}, {})
823

924
function dogClassBuilder:noise(this, chasing, packSize)
1025
if this:makesNoise() then
1126
-- We create isRunning() later, but can use it here!
12-
if this:isRunning() or chasing then
27+
if this.weight >= 20 then
28+
return "*huff*, *huff*"
29+
elseif this:isRunning() or chasing then
1330
return "*pant*, *pant*"
1431
else
1532
if packSize > 2 then
@@ -31,6 +48,6 @@ end
3148
-- #endregion create
3249
-- #region build
3350
local Dog = dogClassBuilder:build()
34-
local inu = Dog(0)
51+
local inu = Dog(0, 10)
3552
print(inu:noise()) -- prints "*woof!* *woof!*"
3653
--#endregion build

0 commit comments

Comments
 (0)