Skip to content

Commit 9be2bf3

Browse files
committed
Added Builder to Java generated code
1 parent 1c3cd5d commit 9be2bf3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1364
-24
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
The format is based on [Keep a Changelog](http://keepachangelog.com/).
44

5+
## [0.81] - 2022-06-30
6+
### Changed
7+
- `pom.xml`: bumped dependency version
8+
9+
## [0.80] - 2022-06-30
10+
### Changed
11+
- `pom.xml`: switched to `log-front` 5.0, dropped `logback`
12+
- `pom.xml`: bumped dependency version
13+
- templates: added `Builder` classes to Java output
14+
515
## [0.79] - 2022-06-16
616
### Changed
717
- `CodeGenerator`: fixed additional bug in validation in derived types

CONFIG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ The default target language is Kotlin; to change this, the `targetLanguage` prop
7171
```
7272
The values allowed are `kotlin`, `java` or `typescript`.
7373

74+
**New in version 0.80:** Java output now includes `Builder` classes, to aid with creation of classes with long
75+
constructor parameter lists.
76+
7477

7578
## `packageName`
7679

pom.xml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>net.pwall.json</groupId>
77
<artifactId>json-kotlin-schema-codegen</artifactId>
8-
<version>0.79</version>
8+
<version>0.81</version>
99
<name>JSON Schema Code Generation</name>
1010
<description>Code generation from JSON Schema to Kotlin or Java</description>
1111
<packaging>jar</packaging>
@@ -61,7 +61,7 @@
6161
<dependency>
6262
<groupId>net.pwall.json</groupId>
6363
<artifactId>json-kotlin-schema</artifactId>
64-
<version>0.35</version>
64+
<version>0.36</version>
6565
</dependency>
6666
<dependency>
6767
<groupId>net.pwall.json</groupId>
@@ -91,12 +91,12 @@
9191
<dependency>
9292
<groupId>net.pwall.yaml</groupId>
9393
<artifactId>yaml-simple</artifactId>
94-
<version>1.12</version>
94+
<version>1.13</version>
9595
</dependency>
9696
<dependency>
9797
<groupId>net.pwall.log</groupId>
9898
<artifactId>log-front</artifactId>
99-
<version>4.0</version>
99+
<version>5.0</version>
100100
</dependency>
101101
<dependency>
102102
<groupId>org.jetbrains.kotlin</groupId>
@@ -113,12 +113,6 @@
113113
<artifactId>kotlin-test-junit</artifactId>
114114
<scope>test</scope>
115115
</dependency>
116-
<dependency>
117-
<groupId>ch.qos.logback</groupId>
118-
<artifactId>logback-classic</artifactId>
119-
<version>1.2.11</version>
120-
<scope>test</scope>
121-
</dependency>
122116
</dependencies>
123117

124118
<build>

src/main/kotlin/net/pwall/json/schema/codegen/CodeGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ import net.pwall.json.schema.validation.PatternValidator
6666
import net.pwall.json.schema.validation.StringValidator
6767
import net.pwall.json.schema.validation.TypeValidator
6868
import net.pwall.json.schema.validation.UniqueItemsValidator
69+
import net.pwall.log.Log.getLogger
6970
import net.pwall.log.Logger
70-
import net.pwall.log.LoggerFactory
7171
import net.pwall.mustache.Template
7272
import net.pwall.mustache.parser.Parser as MustacheParser
7373
import net.pwall.util.Strings
@@ -97,7 +97,7 @@ class CodeGenerator(
9797
/** An optional marker interface to add to each generated class */
9898
var markerInterface: ClassId? = null,
9999
/** A [Logger] object for the output of logging messages */
100-
val log: Logger = LoggerFactory.getDefaultLogger(CodeGenerator::class.qualifiedName)
100+
val log: Logger = getLogger(CodeGenerator::class.qualifiedName)
101101
) {
102102

103103
enum class NestedClassNameOption {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public static class Builder {
2+
3+
{{#properties}} private {{>type}} {{&javaName}};
4+
{{/properties}}
5+
6+
{{#properties}} public Builder with{{&capitalisedName}}({{>type}} {{&javaName}}) {
7+
this.{{&javaName}} = {{&javaName}};
8+
return this;
9+
}
10+
11+
{{/properties}}
12+
public {{&className}} build() {
13+
return new {{&className}}(
14+
{{#properties}} {{&javaName}}{{^last}},{{/last}}
15+
{{/properties}} );
16+
}
17+
18+
}

src/main/resources/java/class.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
{{/baseProperty}}{{/properties}} }
2020

2121
{{/numberOfProperties}}{{>getters}}{{>equals}}
22-
{{>hashcode}}{{#nestedClasses}}
22+
{{>hashcode}}
23+
{{>builder}}{{#nestedClasses}}
2324
{{#constraints}}{{#indent.increment}}{{>nested_class}}{{/indent.increment}}{{/constraints}}{{/nestedClasses}}
2425
}{{/constraints}}

src/test/kotlin/net/pwall/json/schema/codegen/CodeGeneratorAnyTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ public class TestGenerateAny {
103103
return aaa.hashCode();
104104
}
105105
106+
public static class Builder {
107+
108+
private Object aaa;
109+
110+
public Builder withAaa(Object aaa) {
111+
this.aaa = aaa;
112+
return this;
113+
}
114+
115+
public TestGenerateAny build() {
116+
return new TestGenerateAny(
117+
aaa
118+
);
119+
}
120+
121+
}
122+
106123
}
107124
"""
108125

src/test/kotlin/net/pwall/json/schema/codegen/CodeGeneratorArrayItemTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,37 @@ public class TestArrayItems {
183183
return hash ^ (ccc != null ? ccc.hashCode() : 0);
184184
}
185185
186+
public static class Builder {
187+
188+
private List<Integer> aaa;
189+
private List<String> bbb;
190+
private List<List<String>> ccc;
191+
192+
public Builder withAaa(List<Integer> aaa) {
193+
this.aaa = aaa;
194+
return this;
195+
}
196+
197+
public Builder withBbb(List<String> bbb) {
198+
this.bbb = bbb;
199+
return this;
200+
}
201+
202+
public Builder withCcc(List<List<String>> ccc) {
203+
this.ccc = ccc;
204+
return this;
205+
}
206+
207+
public TestArrayItems build() {
208+
return new TestArrayItems(
209+
aaa,
210+
bbb,
211+
ccc
212+
);
213+
}
214+
215+
}
216+
186217
}
187218
"""
188219

src/test/kotlin/net/pwall/json/schema/codegen/CodeGeneratorArrayTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,30 @@ public class TestArray {
152152
return hash ^ bbb.hashCode();
153153
}
154154
155+
public static class Builder {
156+
157+
private List<Person> aaa;
158+
private Set<String> bbb;
159+
160+
public Builder withAaa(List<Person> aaa) {
161+
this.aaa = aaa;
162+
return this;
163+
}
164+
165+
public Builder withBbb(Set<String> bbb) {
166+
this.bbb = bbb;
167+
return this;
168+
}
169+
170+
public TestArray build() {
171+
return new TestArray(
172+
aaa,
173+
bbb
174+
);
175+
}
176+
177+
}
178+
155179
public static class Person {
156180
157181
private final UUID id;

src/test/kotlin/net/pwall/json/schema/codegen/CodeGeneratorBaseAndDerivedClassTest.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ public class TestBase {
192192
return aaa.hashCode();
193193
}
194194
195+
public static class Builder {
196+
197+
private String aaa;
198+
199+
public Builder withAaa(String aaa) {
200+
this.aaa = aaa;
201+
return this;
202+
}
203+
204+
public TestBase build() {
205+
return new TestBase(
206+
aaa
207+
);
208+
}
209+
210+
}
211+
195212
}
196213
"""
197214

@@ -237,6 +254,30 @@ public class TestBaseDerived extends TestBase {
237254
return hash ^ bbb.hashCode();
238255
}
239256
257+
public static class Builder {
258+
259+
private String aaa;
260+
private String bbb;
261+
262+
public Builder withAaa(String aaa) {
263+
this.aaa = aaa;
264+
return this;
265+
}
266+
267+
public Builder withBbb(String bbb) {
268+
this.bbb = bbb;
269+
return this;
270+
}
271+
272+
public TestBaseDerived build() {
273+
return new TestBaseDerived(
274+
aaa,
275+
bbb
276+
);
277+
}
278+
279+
}
280+
240281
}
241282
"""
242283

src/test/kotlin/net/pwall/json/schema/codegen/CodeGeneratorBaseAndEmptyDerivedClassTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,23 @@ public class TestBase {
157157
return aaa.hashCode();
158158
}
159159
160+
public static class Builder {
161+
162+
private String aaa;
163+
164+
public Builder withAaa(String aaa) {
165+
this.aaa = aaa;
166+
return this;
167+
}
168+
169+
public TestBase build() {
170+
return new TestBase(
171+
aaa
172+
);
173+
}
174+
175+
}
176+
160177
}
161178
"""
162179

@@ -190,6 +207,23 @@ public class TestBaseEmptyDerived extends TestBase {
190207
return super.hashCode();
191208
}
192209
210+
public static class Builder {
211+
212+
private String aaa;
213+
214+
public Builder withAaa(String aaa) {
215+
this.aaa = aaa;
216+
return this;
217+
}
218+
219+
public TestBaseEmptyDerived build() {
220+
return new TestBaseEmptyDerived(
221+
aaa
222+
);
223+
}
224+
225+
}
226+
193227
}
194228
"""
195229

src/test/kotlin/net/pwall/json/schema/codegen/CodeGeneratorClassNameTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ public class Supplied {
8787
return id.hashCode();
8888
}
8989
90+
public static class Builder {
91+
92+
private String id;
93+
94+
public Builder withId(String id) {
95+
this.id = id;
96+
return this;
97+
}
98+
99+
public Supplied build() {
100+
return new Supplied(
101+
id
102+
);
103+
}
104+
105+
}
106+
90107
}
91108
"""
92109

src/test/kotlin/net/pwall/json/schema/codegen/CodeGeneratorCommentTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ public class TestEmpty {
9191
return 0;
9292
}
9393
94+
public static class Builder {
95+
96+
public TestEmpty build() {
97+
return new TestEmpty(
98+
);
99+
}
100+
101+
}
102+
94103
}
95104
"""
96105

0 commit comments

Comments
 (0)