Skip to content

Commit a1749ce

Browse files
evanchoolyswankjesse
authored andcommitted
Don't validate modifiers when emitting (#458)
* don't validate modifiers when copying parameter values to the transient property instances fixes #457 * try another approach to bypassing validation * fix/simplify validation check * rename property to make it more meaningful and update logic where it's used * fix formatting * make method internal
1 parent 3cf64d4 commit a1749ce

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/main/java/com/squareup/kotlinpoet/PropertySpec.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package com.squareup.kotlinpoet
1717

1818
import com.squareup.kotlinpoet.FunSpec.Companion.GETTER
1919
import com.squareup.kotlinpoet.FunSpec.Companion.SETTER
20+
import com.squareup.kotlinpoet.KModifier.Target.PROPERTY
2021
import java.lang.reflect.Type
2122
import kotlin.reflect.KClass
2223

@@ -98,6 +99,15 @@ class PropertySpec private constructor(builder: Builder) {
9899
}
99100
}
100101

102+
internal fun fromPrimaryConstructorParameter(parameter: ParameterSpec): PropertySpec {
103+
val builder = toBuilder()
104+
.addAnnotations(parameter.annotations)
105+
builder.isPrimaryConstructorParameter = true
106+
builder.modifiers += parameter.modifiers
107+
108+
return builder.build()
109+
}
110+
101111
override fun equals(other: Any?): Boolean {
102112
if (this === other) return true
103113
if (other == null) return false
@@ -125,6 +135,7 @@ class PropertySpec private constructor(builder: Builder) {
125135
}
126136

127137
class Builder internal constructor(internal val name: String, internal val type: TypeName) {
138+
internal var isPrimaryConstructorParameter = false
128139
internal var mutable = false
129140
internal val kdoc = CodeBlock.builder()
130141
internal var initializer: CodeBlock? = null
@@ -218,7 +229,7 @@ class PropertySpec private constructor(builder: Builder) {
218229
"properties. You should mark either the getter, the setter, or both inline.")
219230
}
220231
for (it in modifiers) {
221-
it.checkTarget(KModifier.Target.PROPERTY)
232+
if (!isPrimaryConstructorParameter) it.checkTarget(PROPERTY)
222233
}
223234
return PropertySpec(this)
224235
}

src/main/java/com/squareup/kotlinpoet/TypeSpec.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,8 @@ class TypeSpec private constructor(builder: TypeSpec.Builder) {
285285
val parameter = primaryConstructor.parameter(property.name) ?: continue
286286
if (parameter.type != property.type) continue
287287
if (CodeBlock.of("%N", parameter) != property.initializer) continue
288-
result[property.name] = property.toBuilder()
289-
.addAnnotations(parameter.annotations)
290-
.addModifiers(*parameter.modifiers.toTypedArray())
291-
.build()
288+
289+
result[property.name] = property.fromPrimaryConstructorParameter(parameter)
292290
}
293291
return result
294292
}

src/test/java/com/squareup/kotlinpoet/TypeSpecTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,22 @@ class TypeSpecTest {
528528
|""".trimMargin())
529529
}
530530

531+
@Test fun classesMayHaveVarargConstructorProperties() {
532+
val variable = TypeSpec.classBuilder("Variable")
533+
.primaryConstructor(FunSpec.constructorBuilder()
534+
.addParameter(ParameterSpec.builder("name", String::class, VARARG).build())
535+
.build())
536+
.addProperty(PropertySpec.builder("name", String::class).initializer("name").build())
537+
.build()
538+
assertThat(toString(variable)).isEqualTo("""
539+
|package com.squareup.tacos
540+
|
541+
|import kotlin.String
542+
|
543+
|class Variable(vararg val name: String)
544+
|""".trimMargin())
545+
}
546+
531547
@Test fun enumConstantsRequired() {
532548
assertThrows<IllegalArgumentException> {
533549
TypeSpec.enumBuilder("Roshambo")

0 commit comments

Comments
 (0)