Skip to content

Commit

Permalink
improve enum entry toString()
Browse files Browse the repository at this point in the history
(cherry picked from commit a046228)
  • Loading branch information
neetopia committed May 17, 2024
1 parent 0c5a4f0 commit 1fc29cd
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 16 deletions.
2 changes: 2 additions & 0 deletions api/src/main/kotlin/com/google/devtools/ksp/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ private fun <T> Any.asEnum(returnType: Class<T>): T =
null,
if (this is KSType) {
this.declaration.simpleName.getShortName()
} else if (this is KSClassDeclaration) {
this.simpleName.getShortName()
} else {
this.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ class KSClassDeclarationEnumEntryImpl private constructor(private val ktEnumEntr
emptySequence()
}

override fun toString(): String {
return "$parent.${simpleName.asString()}"
}

override fun defer(): Restorable? {
return ktEnumEntrySymbol.defer(Companion::getCached)
}
Expand Down
4 changes: 2 additions & 2 deletions kotlin-analysis-api/testData/annotationValue/java.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
// Array
// @Foo
// @Suppress
// G
// ONE
// RGB.G
// JavaEnum.ONE
// 31
// [warning1, warning 2]
// END
Expand Down
96 changes: 96 additions & 0 deletions kotlin-analysis-api/testData/annotationValue/kotlin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2020 Google LLC
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// WITH_RUNTIME
// TEST PROCESSOR: AnnotationArgumentProcessor
// EXPECTED:
// defaultInNested
// SomeClass$WithDollarSign
// Str
// 42
// Foo
// File
// Local
// Array
// Error type synthetic declaration
// [<ERROR TYPE>, Foo]
// @Foo
// @Suppress
// RGB.G
// JavaEnum.ONE
// 31
// Throws
// END
// FILE: a.kt

enum class RGB {
R, G, B
}

class ThrowsClass {
@Throws(Exception::class)
protected open fun throwsException() {
}
}

annotation class Foo(val s: Int) {
annotation class Nested(val nestedDefault:String = "defaultInNested")
}
class `SomeClass$WithDollarSign`

annotation class MyAnnotation(val clazz: KClass<*>)


annotation class Bar(
val argStr: String,
val argInt: Int,
val argClsUser: kotlin.reflect.KClass<*>,
val argClsLib: kotlin.reflect.KClass<*>,
val argClsLocal: kotlin.reflect.KClass<*>,
val argClsArray: kotlin.reflect.KClass<*>,
val argClsMissing: kotlin.reflect.KClass<*>,
val argClsMissingInArray: Array<kotlin.reflect.KClass<*>>,
val argAnnoUser: Foo,
val argAnnoLib: Suppress,
val argEnum: RGB,
val argJavaNum: JavaEnum,
val argDef: Int = 31
)

fun Fun() {
@Foo.Nested
@MyAnnotation(`SomeClass$WithDollarSign`::class)
@Bar(
"Str",
40 + 2,
Foo::class,
java.io.File::class,
Local::class,
Array<String>::class,
Missing::class,
[Missing::class, Foo::class],
Foo(17),
Suppress("name1", "name2"),
RGB.G,
JavaEnum.ONE
)
class Local
}

// FILE: JavaEnum.java

enum JavaEnum { ONE, TWO, THREE }
26 changes: 13 additions & 13 deletions kotlin-analysis-api/testData/parent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
// parent of Enum<INVARIANT (RGB..RGB?)>: Enum<(RGB..RGB?)>
// parent of Enum<(RGB..RGB?)>: RGB
// parent of RGB: File: B.java
// parent of R: RGB
// parent of G: RGB
// parent of B: RGB
// parent of RGB.R: RGB
// parent of RGB.G: RGB
// parent of RGB.B: RGB
// parent of Array<RGB>: values
// parent of values: RGB
// parent of String: value
Expand Down Expand Up @@ -156,10 +156,10 @@
// parent of CMYK: File: a.kt
// parent of CMYK: synthetic constructor for CMYK
// parent of synthetic constructor for CMYK: CMYK
// parent of C: CMYK
// parent of M: CMYK
// parent of Y: CMYK
// parent of K: CMYK
// parent of CMYK.C: CMYK
// parent of CMYK.M: CMYK
// parent of CMYK.Y: CMYK
// parent of CMYK.K: CMYK
// parent of Array<CMYK>: values
// parent of values: CMYK
// parent of String: value
Expand All @@ -179,9 +179,9 @@
// parent of YUV: YUV
// parent of YUV: <init>
// parent of <init>: YUV
// parent of Y: YUV
// parent of U: YUV
// parent of V: YUV
// parent of YUV.Y: YUV
// parent of YUV.U: YUV
// parent of YUV.V: YUV
// parent of YUV: YUV
// parent of YUV: INVARIANT YUV
// parent of INVARIANT YUV: Array<INVARIANT YUV>
Expand Down Expand Up @@ -215,9 +215,9 @@
// parent of HSV: HSV
// parent of HSV: <init>
// parent of <init>: HSV
// parent of H: HSV
// parent of S: HSV
// parent of V: HSV
// parent of HSV.H: HSV
// parent of HSV.S: HSV
// parent of HSV.V: HSV
// parent of Array<HSV>: values
// parent of values: HSV
// parent of String: value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class KSPAATest : AbstractKSPAATest() {
@TestMetadata("annotationValue_kt.kt")
@Test
fun testAnnotationValue_kt() {
runTest("../test-utils/testData/api/annotationValue_kt.kt")
runTest("../kotlin-analysis-api/testData/annotationValue/kotlin.kt")
}

@TestMetadata("annotationWithArrayValue.kt")
Expand Down

0 comments on commit 1fc29cd

Please sign in to comment.