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

Commit 50a5107

Browse files
Upgraded KotlinPoet to 1.3.0
1 parent f5c47fa commit 50a5107

File tree

5 files changed

+40
-17
lines changed

5 files changed

+40
-17
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ apply plugin: 'kotlin-kapt'
1515
...
1616
dependencies {
1717
...
18-
implementation 'com.thinkinglogic.builder:kotlin-builder-annotation:1.1.0'
19-
kapt 'com.thinkinglogic.builder:kotlin-builder-processor:1.1.0'
18+
implementation 'com.thinkinglogic.builder:kotlin-builder-annotation:1.2.0'
19+
kapt 'com.thinkinglogic.builder:kotlin-builder-processor:1.2.0'
2020
}
2121
```
2222
##### Maven
@@ -26,7 +26,7 @@ dependencies {
2626
<dependency>
2727
<groupId>com.thinkinglogic.builder</groupId>
2828
<artifactId>kotlin-builder-annotation</artifactId>
29-
<version>1.1.0</version>
29+
<version>1.2.0</version>
3030
</dependency>
3131
...
3232
</dependencies>
@@ -46,7 +46,7 @@ dependencies {
4646
<annotationProcessorPath>
4747
<groupId>com.thinkinglogic.builder</groupId>
4848
<artifactId>kotlin-builder-processor</artifactId>
49-
<version>1.1.0</version>
49+
<version>1.2.0</version>
5050
</annotationProcessorPath>
5151
</annotationProcessorPaths>
5252
</configuration>

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ buildscript {
2121

2222
allprojects {
2323
group = 'com.thinkinglogic.builder'
24-
version = '1.1.0'
24+
version = '1.2.0'
2525
apply plugin: "kotlin"
2626

2727
repositories {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.thinkinglogic.example
2+
3+
import com.thinkinglogic.builder.annotation.Builder
4+
import com.thinkinglogic.builder.annotation.DefaultValue
5+
import java.time.LocalDate
6+
7+
@Builder
8+
data class DataClassWithLongPropertyNames(
9+
@DefaultValue("myDefault") val stringWithAVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongNameThatWouldCauseLineWrappingInTheGeneratedFile: String = "myDefault",
10+
val nullableString: String?
11+
) {
12+
13+
/**
14+
* @return a Builder initialised with fields from this object.
15+
*/
16+
fun toBuilder() = DataClassWithLongPropertyNamesBuilder(this)
17+
18+
companion object {
19+
@JvmStatic
20+
fun builder() = DataClassWithLongPropertyNamesBuilder()
21+
}
22+
}

kotlin-builder-processor/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apply plugin: 'maven-publish'
55
dependencies {
66
implementation project(':kotlin-builder-annotation')
77

8-
implementation 'com.squareup:kotlinpoet:1.0.0-RC1'
8+
implementation 'com.squareup:kotlinpoet:1.3.0'
99
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
1010
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
1111
}

kotlin-builder-processor/src/main/kotlin/com/thinkinglogic/builder/processor/BuilderProcessor.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class BuilderProcessor : AbstractProcessor() {
126126
val code = StringBuilder()
127127
fields.forEach { field ->
128128
if (getterFieldNames.contains(field.simpleName.toString())) {
129-
code.append(" this.${field.simpleName} = $source.${field.simpleName}")
129+
code.append(" this.${field.simpleName}·=·$source.${field.simpleName}")
130130
.appendln()
131131
}
132132
}
@@ -149,11 +149,11 @@ class BuilderProcessor : AbstractProcessor() {
149149
/** Creates a 'build()' function that will invoke a constructor for [returnType], passing [fields] as arguments and returning the new instance. */
150150
private fun createBuildFunction(fields: List<Element>, returnType: TypeElement): FunSpec {
151151
val code = StringBuilder("$CHECK_REQUIRED_FIELDS_FUNCTION_NAME()")
152-
code.appendln().append("return ${returnType.simpleName}(")
152+
code.appendln().append("return·${returnType.simpleName}(")
153153
val iterator = fields.listIterator()
154154
while (iterator.hasNext()) {
155155
val field = iterator.next()
156-
code.appendln().append(" ${field.simpleName} = ${field.simpleName}")
156+
code.appendln().append(" ${field.simpleName}·=·${field.simpleName}")
157157
if (!field.isNullable()) {
158158
code.append("!!")
159159
}
@@ -174,7 +174,7 @@ class BuilderProcessor : AbstractProcessor() {
174174
val code = StringBuilder()
175175
fields.filterNot { it.isNullable() }
176176
.forEach { field ->
177-
code.append(" check(${field.simpleName} != null, {\"${field.simpleName} must not be null\"})").appendln()
177+
code.append(" check(${field.simpleName}·!=·null, {\"${field.simpleName}·must·not·be·null\"})").appendln()
178178
}
179179

180180
return FunSpec.builder(CHECK_REQUIRED_FIELDS_FUNCTION_NAME)
@@ -185,7 +185,8 @@ class BuilderProcessor : AbstractProcessor() {
185185

186186
/** Creates a property for the field identified by this element. */
187187
private fun Element.asProperty(): PropertySpec =
188-
PropertySpec.varBuilder(simpleName.toString(), asKotlinTypeName().asNullable(), KModifier.PRIVATE)
188+
PropertySpec.builder(simpleName.toString(), asKotlinTypeName().copy(nullable = true), KModifier.PRIVATE)
189+
.mutable()
189190
.initializer(defaultValue())
190191
.build()
191192

@@ -208,14 +209,14 @@ class BuilderProcessor : AbstractProcessor() {
208209
private fun Element.asSetterFunctionReturning(builder: ClassName): FunSpec {
209210
val fieldType = asKotlinTypeName()
210211
val parameterClass = if (isNullable()) {
211-
fieldType.asNullable()
212+
fieldType.copy(nullable = true)
212213
} else {
213214
fieldType
214215
}
215216
return FunSpec.builder(simpleName.toString())
216217
.addParameter(ParameterSpec.builder("value", parameterClass).build())
217218
.returns(builder)
218-
.addCode("return apply { $simpleName = value }\n")
219+
.addCode("return apply·{ $simpleName·=·value }\n")
219220
.build()
220221
}
221222

@@ -245,7 +246,7 @@ class BuilderProcessor : AbstractProcessor() {
245246
* @throws NoSuchElementException if [this.typeArguments] is empty.
246247
*/
247248
private fun ParameterizedTypeName.withNullableType(): ParameterizedTypeName {
248-
val lastType = this.typeArguments.last().asNullable()
249+
val lastType = this.typeArguments.last().copy(nullable = true)
249250
val typeArguments = ArrayList<TypeName>()
250251
typeArguments.addAll(this.typeArguments.dropLast(1))
251252
typeArguments.add(lastType)
@@ -261,9 +262,9 @@ class BuilderProcessor : AbstractProcessor() {
261262
private fun ParameterizedTypeName.asMutableCollection(): ParameterizedTypeName {
262263
val mutable = MUTABLE_COLLECTIONS[rawType]!!
263264
.parameterizedBy(*this.typeArguments.toTypedArray())
264-
.annotated(this.annotations)
265-
return if (nullable) {
266-
mutable.asNullable()
265+
.copy(annotations = this.annotations)
266+
return if (isNullable) {
267+
mutable.copy(nullable = true)
267268
} else {
268269
mutable
269270
}

0 commit comments

Comments
 (0)