Skip to content

Commit

Permalink
Switch AnnotationSpec.get() to use safer arrayOf() syntax
Browse files Browse the repository at this point in the history
This allows these annotation specs' members to be more portable regardless of whether it's used as an annotation or constructor called. See square/moshi#1390 (comment) for more details
  • Loading branch information
ZacSweers committed Oct 19, 2021
1 parent 43de708 commit cffb88b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,7 @@ class KotlinPoetMetadataSpecsTest : MultiClassInspectorTest() {
"""
@kotlin.SinceKotlin(version = "1.3")
@kotlin.`annotation`.Retention(value = kotlin.`annotation`.AnnotationRetention.RUNTIME)
@kotlin.`annotation`.Target(allowedTargets = [kotlin.`annotation`.AnnotationTarget.CLASS])
@kotlin.`annotation`.Target(allowedTargets = arrayOf(kotlin.`annotation`.AnnotationTarget.CLASS))
public annotation class Metadata(
@get:kotlin.jvm.JvmName(name = "k")
public val kind: kotlin.Int = throw NotImplementedError("Stub!"),
Expand Down Expand Up @@ -1779,7 +1779,7 @@ class KotlinPoetMetadataSpecsTest : MultiClassInspectorTest() {
assertThat(typeSpec.trimmedToString()).isEqualTo(
"""
@kotlin.`annotation`.Retention(value = kotlin.`annotation`.AnnotationRetention.RUNTIME)
@kotlin.`annotation`.Target(allowedTargets = [kotlin.`annotation`.AnnotationTarget.CLASS])
@kotlin.`annotation`.Target(allowedTargets = arrayOf(kotlin.`annotation`.AnnotationTarget.CLASS))
public annotation class Metadata(
@get:kotlin.jvm.JvmName(name = "k")
public val kind: kotlin.Int = throw NotImplementedError("Stub!"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ public class AnnotationSpec private constructor(
builder.add("%T::class", t.asTypeName())

override fun visitArray(values: List<AnnotationValue>, name: String): CodeBlock.Builder {
builder.add("[⇥⇥")
builder.add("arrayOf(⇥⇥")
values.forEachIndexed { index, value ->
if (index > 0) builder.add(", ")
value.accept(this, name)
}
builder.add("⇤⇤]")
builder.add("⇤⇤)")
return builder
}
}
Expand Down Expand Up @@ -207,12 +207,12 @@ public class AnnotationSpec private constructor(
val member = CodeBlock.builder()
member.add("%L = ", method.name)
if (value.javaClass.isArray) {
member.add("[⇥⇥")
member.add("arrayOf(⇥⇥")
for (i in 0 until Array.getLength(value)) {
if (i > 0) member.add(", ")
member.add(Builder.memberForValue(Array.get(value, i)))
}
member.add("⇤⇤]")
member.add("⇤⇤)")
builder.addMember(member.build())
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ class AnnotationSpecTest {
| f = 11.1,
| j = AnnotationSpecTest.AnnotationA(),
| l = Override::class,
| m = [9, 8, 1],
| m = arrayOf(9, 8, 1),
| o = AnnotationSpecTest.Breakfast.PANCAKES,
| p = 1701,
| q = AnnotationSpecTest.AnnotationC(value = "bar"),
| r = [Float::class, Double::class]
| r = arrayOf(Float::class, Double::class)
|)
|public class Taco
|""".trimMargin()
Expand All @@ -140,11 +140,11 @@ class AnnotationSpecTest {
| f = 11.1,
| j = AnnotationSpecTest.AnnotationA(),
| l = Override::class,
| m = [9, 8, 1],
| m = arrayOf(9, 8, 1),
| o = AnnotationSpecTest.Breakfast.PANCAKES,
| p = 1701,
| q = AnnotationSpecTest.AnnotationC(value = "bar"),
| r = [Float::class, Double::class]
| r = arrayOf(Float::class, Double::class)
|)
|public class IsAnnotated
|""".trimMargin()
Expand Down Expand Up @@ -186,11 +186,11 @@ class AnnotationSpecTest {
|@AnnotationSpecTest.HasDefaultsAnnotation(
| f = 11.1,
| l = Override::class,
| m = [9, 8, 1],
| m = arrayOf(9, 8, 1),
| o = AnnotationSpecTest.Breakfast.PANCAKES,
| p = 1701,
| q = AnnotationSpecTest.AnnotationC(value = "bar"),
| r = [Float::class, Double::class]
| r = arrayOf(Float::class, Double::class)
|)
|public class Taco
|""".trimMargin()
Expand All @@ -217,18 +217,18 @@ class AnnotationSpecTest {
| d = 8,
| e = 9.0f,
| f = 11.1,
| g = ['\u0000', '쫾', 'z', '€', 'ℕ', '"', '\'', '\t', '\n'],
| g = arrayOf('\u0000', '쫾', 'z', '€', 'ℕ', '"', '\'', '\t', '\n'),
| h = true,
| i = AnnotationSpecTest.Breakfast.WAFFLES,
| j = AnnotationSpecTest.AnnotationA(),
| k = "maple",
| l = Override::class,
| m = [9, 8, 1],
| n = [AnnotationSpecTest.Breakfast.WAFFLES, AnnotationSpecTest.Breakfast.PANCAKES],
| m = arrayOf(9, 8, 1),
| n = arrayOf(AnnotationSpecTest.Breakfast.WAFFLES, AnnotationSpecTest.Breakfast.PANCAKES),
| o = AnnotationSpecTest.Breakfast.PANCAKES,
| p = 1701,
| q = AnnotationSpecTest.AnnotationC(value = "bar"),
| r = [Float::class, Double::class]
| r = arrayOf(Float::class, Double::class)
|)
|public class Taco
|""".trimMargin()
Expand Down Expand Up @@ -359,7 +359,8 @@ class AnnotationSpecTest {
|import java.lang.Boolean
|import java.lang.Object
|
|@JavaClassWithArrayValueAnnotation.AnnotationWithArrayValue(value = [Object::class, Boolean::class])
|@JavaClassWithArrayValueAnnotation.AnnotationWithArrayValue(value = arrayOf(Object::class,
| Boolean::class))
|public class Result
|""".trimMargin()
)
Expand Down Expand Up @@ -387,7 +388,7 @@ class AnnotationSpecTest {
|import java.lang.Object
|import kotlin.Boolean
|
|@AnnotationSpecTest.AnnotationWithArrayValue(value = [Object::class, Boolean::class])
|@AnnotationSpecTest.AnnotationWithArrayValue(value = arrayOf(Object::class, Boolean::class))
|public class Result
""".trimMargin()
)
Expand All @@ -408,7 +409,8 @@ class AnnotationSpecTest {
|import java.lang.Boolean
|import java.lang.Object
|
|@JavaClassWithArrayValueAnnotation.AnnotationWithArrayValue(value = [Object::class, Boolean::class])
|@JavaClassWithArrayValueAnnotation.AnnotationWithArrayValue(value = arrayOf(Object::class,
| Boolean::class))
|public class Result
""".trimMargin()
)
Expand All @@ -428,7 +430,7 @@ class AnnotationSpecTest {
|import java.lang.Object
|import kotlin.Boolean
|
|@AnnotationSpecTest.AnnotationWithArrayValue(value = [Object::class, Boolean::class])
|@AnnotationSpecTest.AnnotationWithArrayValue(value = arrayOf(Object::class, Boolean::class))
|public class Result
""".trimMargin()
)
Expand Down

0 comments on commit cffb88b

Please sign in to comment.