@@ -6,9 +6,55 @@ This project aims to be a minimal viable replacement for the Lombok @Builder plu
6
6
[ ![ License] ( https://img.shields.io/badge/license-MIT-blue.svg )] ( https://opensource.org/licenses/MIT )
7
7
8
8
## Usage
9
- TODO: gradle and maven
9
+ #### Import ` kotlin-builder-annotation ` and ` kotlin-builder-processor `
10
+ And configure the [ Kotlin annotation processor (kapt)] ( https://kotlinlang.org/docs/reference/kapt.html ) .
11
+ ##### Gradle
12
+ ``` gradle
13
+ ...
14
+ apply plugin: 'kotlin-kapt'
15
+ ...
16
+ dependencies {
17
+ ...
18
+ implementation 'com.thinkinglogic.builder:kotlin-builder-annotation:1.2.0'
19
+ kapt 'com.thinkinglogic.builder:kotlin-builder-processor:1.2.0'
20
+ }
21
+ ```
22
+ ##### Maven
23
+ ``` maven
24
+ ...
25
+ <dependencies>
26
+ <dependency>
27
+ <groupId>com.thinkinglogic.builder</groupId>
28
+ <artifactId>kotlin-builder-annotation</artifactId>
29
+ <version>1.2.0</version>
30
+ </dependency>
31
+ ...
32
+ </dependencies>
33
+ ...
34
+ <execution>
35
+ <id>kapt</id>
36
+ <goals>
37
+ <goal>kapt</goal>
38
+ </goals>
39
+ <configuration>
40
+ <sourceDirs>
41
+ <sourceDir>src/main/kotlin</sourceDir>
42
+ <sourceDir>src/main/java</sourceDir>
43
+ </sourceDirs>
44
+ <annotationProcessorPaths>
45
+ <!-- Specify your annotation processors here. -->
46
+ <annotationProcessorPath>
47
+ <groupId>com.thinkinglogic.builder</groupId>
48
+ <artifactId>kotlin-builder-processor</artifactId>
49
+ <version>1.2.0</version>
50
+ </annotationProcessorPath>
51
+ </annotationProcessorPaths>
52
+ </configuration>
53
+ </execution>
10
54
11
- #### Annotate your class with the @Builder annotation
55
+ ```
56
+
57
+ #### Annotate your class(es) with the @Builder annotation
12
58
``` kotlin
13
59
import com.thinkinglogic.builder.annotation.Builder
14
60
@@ -20,8 +66,8 @@ data class MyDataClass(
20
66
```
21
67
That's it! Client code can now use a builder to construct instances of your class.
22
68
23
- Unlike Lombok there's no bytecode manipulation, so we don't have a ` MyDataClass.builder() ` static method.
24
- Instead we create a ` new MyDataClassBuilder() ` :
69
+ Unlike Lombok there's no bytecode manipulation, so we don't expose a ` MyDataClass.builder() ` static method.
70
+ Instead clients create a ` new MyDataClassBuilder() ` , for instance :
25
71
26
72
``` java
27
73
public class MyDataFactory {
@@ -41,6 +87,9 @@ The builder will check for required fields, so
41
87
` new MyDataClassBuilder().notNullString("Foo").build(); `
42
88
would return a new instance with a null value for 'nullableString'.
43
89
90
+ To replace Kotlin's ` copy() ` (and Lombok's ` toBuilder() ` ) method, clients can pass an instance of the annotated class when constructing a builder:
91
+ ` new MyDataClassBuilder(myDataClassInstance) ` - the builder will be initialised with values from the instance.
92
+
44
93
#### Default values
45
94
Kotlin doesn't retain information about default values after compilation, so it cannot be accessed during annotation processing.
46
95
Instead we must use the ` @DefaultValue ` annotation to tell the builder about it:
@@ -72,7 +121,7 @@ data class MyDataClass(
72
121
```
73
122
74
123
#### Mutable collections
75
- Information about the mutability of collections and maps is lost during compilation, so there is a ` @Mutable ` annotation:
124
+ Information about the mutability of collections and maps is lost during compilation, so there is an ` @Mutable ` annotation:
76
125
``` kotlin
77
126
import com.thinkinglogic.builder.annotation.Builder
78
127
import com.thinkinglogic.builder.annotation.Mutable
@@ -85,7 +134,7 @@ data class MyDataClass(
85
134
```
86
135
87
136
#### Constructor parameters
88
- The ` @Builder ` annotation should be placed on a constructor instead of the class if you have constructor-only parameters:
137
+ The ` @Builder ` annotation may be placed on a constructor instead of the class - useful if you have constructor-only parameters:
89
138
``` kotlin
90
139
import com.thinkinglogic.builder.annotation.Builder
91
140
@@ -98,8 +147,32 @@ constructor(
98
147
) {
99
148
val fullName = " $forename $surname "
100
149
}
101
- ```
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.
102
173
174
+ ---
175
+ Examples of all of the above may be found in the kotlin-builder-example-usage sub-project.
103
176
## License
104
177
This software is Licenced under the [ MIT License] ( LICENSE.md ) .
105
178
0 commit comments