Skip to content

Commit

Permalink
retrieve annotations for KSType from KotlinType instead of KSTypeRefe…
Browse files Browse the repository at this point in the history
…rence
  • Loading branch information
neetopia committed Sep 9, 2022
1 parent 5a19640 commit ad95fbb
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
Expand Up @@ -25,6 +25,7 @@ import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.KSTypeArgument
import com.google.devtools.ksp.symbol.Nullability
import com.google.devtools.ksp.symbol.Origin
import com.google.devtools.ksp.symbol.impl.binary.KSAnnotationDescriptorImpl
import com.google.devtools.ksp.symbol.impl.binary.KSTypeArgumentDescriptorImpl
import com.google.devtools.ksp.symbol.impl.convertKotlinType
import com.google.devtools.ksp.symbol.impl.replaceTypeArguments
Expand Down Expand Up @@ -156,6 +157,10 @@ fun getKSTypeCached(
) {
KSErrorType
} else {
KSTypeImpl.getCached(kotlinType, ksTypeArguments, annotations)
KSTypeImpl.getCached(
kotlinType,
ksTypeArguments,
kotlinType.annotations.map { KSAnnotationDescriptorImpl.getCached(it, null) }.asSequence()
)
}
}
Expand Up @@ -224,6 +224,12 @@ class KSPCompilerPluginTest : AbstractKSPCompilerPluginTest() {
runTest("testData/api/functionTypeAlias.kt")
}

@TestMetadata("functionTypeAnnotation.kt")
@Test
fun testFunctionTypeAnnotation() {
runTest("testData/api/functionTypeAnnotation.kt")
}

@TestMetadata("functionTypes.kt")
@Test
fun testFunctionTypes() {
Expand Down
33 changes: 33 additions & 0 deletions compiler-plugin/testData/api/functionTypeAnnotation.kt
@@ -0,0 +1,33 @@
/*
* Copyright 2022 Google LLC
* Copyright 2010-2022 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: FunctionTypeAnnotationProcessor
// EXPECTED:
// strExt0: Function1 @ExtensionFunctionType
// strExt1: Function2 @ExtensionFunctionType
// strWithAnnoExt0: Function1 @A, @ExtensionFunctionType
// strWithAnnoExt1: Function2 @A, @ExtensionFunctionType
// END
// FILE: a.kt
annotation class A

val strExt0: String.() -> Unit = TODO()
val strExt1: String.(Int) -> Unit = TODO()
val strWithAnnoExt0: @A String.() -> Unit = TODO()
val strWithAnnoExt1: @A String.(@A Int) -> Unit = TODO()
Expand Up @@ -240,6 +240,13 @@ class KSPAATest : AbstractKSPAATest() {
runTest("../compiler-plugin/testData/api/functionTypeAlias.kt")
}

@Disabled
@TestMetadata("functionTypeAnnotation.kt")
@Test
fun testFunctionTypeAnnotation() {
runTest("testData/api/functionTypeAnnotation.kt")
}

@Disabled
@TestMetadata("functionTypes.kt")
@Test
Expand Down
@@ -0,0 +1,36 @@
package com.google.devtools.ksp.processor

import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSNode
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
import com.google.devtools.ksp.visitor.KSTopDownVisitor

class FunctionTypeAnnotationProcessor : AbstractTestProcessor() {
val results = mutableListOf<String>()

override fun toResult(): List<String> {
return results
}

override fun process(resolver: Resolver): List<KSAnnotated> {
val files = resolver.getNewFiles()

files.forEach {
it.accept(
object : KSTopDownVisitor<Unit, Unit>() {
override fun defaultHandler(node: KSNode, data: Unit) = Unit

override fun visitPropertyDeclaration(property: KSPropertyDeclaration, data: Unit) {
val type = property.type.resolve()
val propertyName = property.simpleName.asString()
val typeName = type.declaration.simpleName.asString()
results.add("$propertyName: $typeName ${type.annotations.joinToString { it.toString() }}")
}
},
Unit
)
}
return emptyList()
}
}
Expand Up @@ -23,12 +23,10 @@ import com.google.devtools.ksp.visitor.KSTopDownVisitor

open class FunctionTypeProcessor : AbstractTestProcessor() {
val results = mutableListOf<String>()
val typeCollector = TypeCollector()
val types = mutableSetOf<KSType>()

override fun process(resolver: Resolver): List<KSAnnotated> {
val files = resolver.getNewFiles()
val ignoredNames = mutableSetOf<String>()

files.forEach {
it.accept(
Expand Down

0 comments on commit ad95fbb

Please sign in to comment.