Skip to content

Commit a046228

Browse files
committed
improve enum entry toString()
1 parent c59f7bb commit a046228

File tree

6 files changed

+118
-16
lines changed

6 files changed

+118
-16
lines changed

api/src/main/kotlin/com/google/devtools/ksp/utils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ private fun <T> Any.asEnum(returnType: Class<T>): T =
494494
null,
495495
if (this is KSType) {
496496
this.declaration.simpleName.getShortName()
497+
} else if (this is KSClassDeclaration) {
498+
this.simpleName.getShortName()
497499
} else {
498500
this.toString()
499501
}

kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationEnumEntryImpl.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ class KSClassDeclarationEnumEntryImpl private constructor(private val ktEnumEntr
120120
emptySequence()
121121
}
122122

123+
override fun toString(): String {
124+
return "$parent.${simpleName.asString()}"
125+
}
126+
123127
override fun defer(): Restorable? {
124128
return ktEnumEntrySymbol.defer(Companion::getCached)
125129
}

kotlin-analysis-api/testData/annotationValue/java.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
// Array
3333
// @Foo
3434
// @Suppress
35-
// G
36-
// ONE
35+
// RGB.G
36+
// JavaEnum.ONE
3737
// 31
3838
// [warning1, warning 2]
3939
// END
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// WITH_RUNTIME
19+
// TEST PROCESSOR: AnnotationArgumentProcessor
20+
// EXPECTED:
21+
// defaultInNested
22+
// SomeClass$WithDollarSign
23+
// Str
24+
// 42
25+
// Foo
26+
// File
27+
// Local
28+
// Array
29+
// Error type synthetic declaration
30+
// [<ERROR TYPE>, Foo]
31+
// @Foo
32+
// @Suppress
33+
// RGB.G
34+
// JavaEnum.ONE
35+
// 31
36+
// Throws
37+
// END
38+
// FILE: a.kt
39+
40+
enum class RGB {
41+
R, G, B
42+
}
43+
44+
class ThrowsClass {
45+
@Throws(Exception::class)
46+
protected open fun throwsException() {
47+
}
48+
}
49+
50+
annotation class Foo(val s: Int) {
51+
annotation class Nested(val nestedDefault:String = "defaultInNested")
52+
}
53+
class `SomeClass$WithDollarSign`
54+
55+
annotation class MyAnnotation(val clazz: KClass<*>)
56+
57+
58+
annotation class Bar(
59+
val argStr: String,
60+
val argInt: Int,
61+
val argClsUser: kotlin.reflect.KClass<*>,
62+
val argClsLib: kotlin.reflect.KClass<*>,
63+
val argClsLocal: kotlin.reflect.KClass<*>,
64+
val argClsArray: kotlin.reflect.KClass<*>,
65+
val argClsMissing: kotlin.reflect.KClass<*>,
66+
val argClsMissingInArray: Array<kotlin.reflect.KClass<*>>,
67+
val argAnnoUser: Foo,
68+
val argAnnoLib: Suppress,
69+
val argEnum: RGB,
70+
val argJavaNum: JavaEnum,
71+
val argDef: Int = 31
72+
)
73+
74+
fun Fun() {
75+
@Foo.Nested
76+
@MyAnnotation(`SomeClass$WithDollarSign`::class)
77+
@Bar(
78+
"Str",
79+
40 + 2,
80+
Foo::class,
81+
java.io.File::class,
82+
Local::class,
83+
Array<String>::class,
84+
Missing::class,
85+
[Missing::class, Foo::class],
86+
Foo(17),
87+
Suppress("name1", "name2"),
88+
RGB.G,
89+
JavaEnum.ONE
90+
)
91+
class Local
92+
}
93+
94+
// FILE: JavaEnum.java
95+
96+
enum JavaEnum { ONE, TWO, THREE }

kotlin-analysis-api/testData/parent.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
// parent of Enum<INVARIANT (RGB..RGB?)>: Enum<(RGB..RGB?)>
5151
// parent of Enum<(RGB..RGB?)>: RGB
5252
// parent of RGB: File: B.java
53-
// parent of R: RGB
54-
// parent of G: RGB
55-
// parent of B: RGB
53+
// parent of RGB.R: RGB
54+
// parent of RGB.G: RGB
55+
// parent of RGB.B: RGB
5656
// parent of Array<RGB>: values
5757
// parent of values: RGB
5858
// parent of String: value
@@ -156,10 +156,10 @@
156156
// parent of CMYK: File: a.kt
157157
// parent of CMYK: synthetic constructor for CMYK
158158
// parent of synthetic constructor for CMYK: CMYK
159-
// parent of C: CMYK
160-
// parent of M: CMYK
161-
// parent of Y: CMYK
162-
// parent of K: CMYK
159+
// parent of CMYK.C: CMYK
160+
// parent of CMYK.M: CMYK
161+
// parent of CMYK.Y: CMYK
162+
// parent of CMYK.K: CMYK
163163
// parent of Array<CMYK>: values
164164
// parent of values: CMYK
165165
// parent of String: value
@@ -179,9 +179,9 @@
179179
// parent of YUV: YUV
180180
// parent of YUV: <init>
181181
// parent of <init>: YUV
182-
// parent of Y: YUV
183-
// parent of U: YUV
184-
// parent of V: YUV
182+
// parent of YUV.Y: YUV
183+
// parent of YUV.U: YUV
184+
// parent of YUV.V: YUV
185185
// parent of YUV: YUV
186186
// parent of YUV: INVARIANT YUV
187187
// parent of INVARIANT YUV: Array<INVARIANT YUV>
@@ -215,9 +215,9 @@
215215
// parent of HSV: HSV
216216
// parent of HSV: <init>
217217
// parent of <init>: HSV
218-
// parent of H: HSV
219-
// parent of S: HSV
220-
// parent of V: HSV
218+
// parent of HSV.H: HSV
219+
// parent of HSV.S: HSV
220+
// parent of HSV.V: HSV
221221
// parent of Array<HSV>: values
222222
// parent of values: HSV
223223
// parent of String: value

test-utils/src/test/kotlin/com/google/devtools/ksp/test/KSPAATest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class KSPAATest : AbstractKSPAATest() {
9292
@TestMetadata("annotationValue_kt.kt")
9393
@Test
9494
fun testAnnotationValue_kt() {
95-
runTest("../test-utils/testData/api/annotationValue_kt.kt")
95+
runTest("../kotlin-analysis-api/testData/annotationValue/kotlin.kt")
9696
}
9797

9898
@TestMetadata("annotationWithArrayValue.kt")

0 commit comments

Comments
 (0)