This repository was archived by the owner on Feb 26, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +68
-2
lines changed
kotlin-builder-example-usage/src
main/kotlin/com/thinkinglogic/example
test/java/com/thinkinglogic/example Expand file tree Collapse file tree 3 files changed +68
-2
lines changed Original file line number Diff line number Diff 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+ ---
151175Examples of all of the above may be found in the kotlin-builder-example-usage sub-project.
152176## License
153177This software is Licenced under the [ MIT License] ( LICENSE.md ) .
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff line change 22
33import org .junit .jupiter .api .Test ;
44
5+ import java .time .LocalDate ;
6+
7+ import static org .assertj .core .api .Assertions .assertThat ;
58import static org .assertj .core .api .Assertions .catchThrowable ;
69import 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}
You can’t perform that action at this time.
0 commit comments