From 1fc29cd6b4ed8ac1874ac984d14791eca1537804 Mon Sep 17 00:00:00 2001 From: Jiaxiang Chen Date: Tue, 14 May 2024 10:23:38 -0700 Subject: [PATCH] improve enum entry toString() (cherry picked from commit a046228d4d6109ea66a961db215564fc738fb765) --- .../kotlin/com/google/devtools/ksp/utils.kt | 2 + .../kotlin/KSClassDeclarationEnumEntryImpl.kt | 4 + .../testData/annotationValue/java.kt | 4 +- .../testData/annotationValue/kotlin.kt | 96 +++++++++++++++++++ kotlin-analysis-api/testData/parent.kt | 26 ++--- .../com/google/devtools/ksp/test/KSPAATest.kt | 2 +- 6 files changed, 118 insertions(+), 16 deletions(-) create mode 100644 kotlin-analysis-api/testData/annotationValue/kotlin.kt diff --git a/api/src/main/kotlin/com/google/devtools/ksp/utils.kt b/api/src/main/kotlin/com/google/devtools/ksp/utils.kt index d0e4207a6e..15a4cac39a 100644 --- a/api/src/main/kotlin/com/google/devtools/ksp/utils.kt +++ b/api/src/main/kotlin/com/google/devtools/ksp/utils.kt @@ -494,6 +494,8 @@ private fun Any.asEnum(returnType: Class): T = null, if (this is KSType) { this.declaration.simpleName.getShortName() + } else if (this is KSClassDeclaration) { + this.simpleName.getShortName() } else { this.toString() } diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationEnumEntryImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationEnumEntryImpl.kt index 26237027c5..6946d5ed97 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationEnumEntryImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/KSClassDeclarationEnumEntryImpl.kt @@ -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) } diff --git a/kotlin-analysis-api/testData/annotationValue/java.kt b/kotlin-analysis-api/testData/annotationValue/java.kt index 4a1735e5b7..c5bd2a3e37 100644 --- a/kotlin-analysis-api/testData/annotationValue/java.kt +++ b/kotlin-analysis-api/testData/annotationValue/java.kt @@ -32,8 +32,8 @@ // Array // @Foo // @Suppress -// G -// ONE +// RGB.G +// JavaEnum.ONE // 31 // [warning1, warning 2] // END diff --git a/kotlin-analysis-api/testData/annotationValue/kotlin.kt b/kotlin-analysis-api/testData/annotationValue/kotlin.kt new file mode 100644 index 0000000000..cb62ad2b65 --- /dev/null +++ b/kotlin-analysis-api/testData/annotationValue/kotlin.kt @@ -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 +// [, 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>, + 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::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 } diff --git a/kotlin-analysis-api/testData/parent.kt b/kotlin-analysis-api/testData/parent.kt index 06bf77a3bf..b84b04480e 100644 --- a/kotlin-analysis-api/testData/parent.kt +++ b/kotlin-analysis-api/testData/parent.kt @@ -50,9 +50,9 @@ // parent of Enum: 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: values // parent of values: RGB // parent of String: value @@ -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: values // parent of values: CMYK // parent of String: value @@ -179,9 +179,9 @@ // parent of YUV: YUV // parent of YUV: // parent of : 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 @@ -215,9 +215,9 @@ // parent of HSV: HSV // parent of HSV: // parent of : 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: values // parent of values: HSV // parent of String: value diff --git a/test-utils/src/test/kotlin/com/google/devtools/ksp/test/KSPAATest.kt b/test-utils/src/test/kotlin/com/google/devtools/ksp/test/KSPAATest.kt index ddbf8f31f5..55cee84a48 100644 --- a/test-utils/src/test/kotlin/com/google/devtools/ksp/test/KSPAATest.kt +++ b/test-utils/src/test/kotlin/com/google/devtools/ksp/test/KSPAATest.kt @@ -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")