Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit eedff5b

Browse files
Added examples of creates .builder() and .toBuilder() methods for Lombok interoperability
1 parent 57a1697 commit eedff5b

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,31 @@ constructor(
147147
) {
148148
val fullName = "$forename $surname"
149149
}
150-
```
150+
```
151+
152+
#### builder() and toBuilder() methods
153+
The `@Builder` annotation processor cannot modify bytecode, so it cannot generate builder() and toBuilder() methods for you,
154+
but you can add them yourself:
155+
```kotlin
156+
import com.thinkinglogic.builder.annotation.Builder
157+
158+
@Builder
159+
data class MyDataClass(
160+
val notNullString: String,
161+
val nullableString: String?
162+
) {
163+
164+
fun toBuilder(): MyDataClassBuilder = MyDataClassBuilder(this)
165+
166+
companion object {
167+
@JvmStatic fun builder() = MyDataClassBuilder()
168+
}
169+
}
170+
```
171+
`MyDataClass.builder()` and `myDataClassObject.toBuilder()` can now be invoked from java,
172+
enabling a complete drop-in replacement for the Lombok @Builder annotation.
173+
174+
---
151175
Examples of all of the above may be found in the kotlin-builder-example-usage sub-project.
152176
## License
153177
This software is Licenced under the [MIT License](LICENSE.md).

kotlin-builder-example-usage/src/main/kotlin/com/thinkinglogic/example/SimpleDataClass.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@ data class SimpleDataClass(
1313
val date: LocalDate,
1414
@DefaultValue("withDefaultValue") val stringWithDefault: String = "withDefaultValue",
1515
@DefaultValue("LocalDate.MIN") val defaultDate: LocalDate = LocalDate.MIN
16-
)
16+
) {
17+
18+
/**
19+
* @return a Builder initialised with fields from this object.
20+
*/
21+
fun toBuilder() = SimpleDataClassBuilder(this)
22+
23+
companion object {
24+
@JvmStatic
25+
fun builder() = SimpleDataClassBuilder()
26+
}
27+
}

kotlin-builder-example-usage/src/test/java/com/thinkinglogic/example/SimpleDataClassJavaTest.java

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

33
import org.junit.jupiter.api.Test;
44

5+
import java.time.LocalDate;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
58
import static org.assertj.core.api.Assertions.catchThrowable;
69
import static org.assertj.core.api.BDDAssertions.then;
710

@@ -20,4 +23,32 @@ void builderShouldRejectNullValueForRequiredFields() {
2023
.isInstanceOf(IllegalArgumentException.class)
2124
.hasMessageContaining("notNullString");
2225
}
26+
27+
@Test
28+
void toBuilderShouldReturnInitialisedBuilder() {
29+
// given
30+
SimpleDataClass original = new SimpleDataClassBuilder()
31+
.date(LocalDate.now())
32+
.notNullString("Foo")
33+
.nullableString("Bar")
34+
.notNullLong(123L)
35+
.build();
36+
37+
// when
38+
SimpleDataClass result = original.toBuilder().build();
39+
40+
// then
41+
assertThat(result).isEqualTo(original);
42+
}
43+
44+
@Test
45+
void staticBuilderMethodReturnsBuilder() {
46+
// given
47+
48+
// when
49+
SimpleDataClassBuilder builder = SimpleDataClass.builder();
50+
51+
// then
52+
assertThat(builder).isNotNull();
53+
}
2354
}

0 commit comments

Comments
 (0)