1 /*
<lambda>null2 * Copyright 2022 Google LLC
3 * Copyright 2010-2022 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 package com.google.devtools.ksp.impl.symbol.kotlin
19
20 import com.google.devtools.ksp.KSObjectCache
21 import com.google.devtools.ksp.processing.impl.KSNameImpl
22 import com.google.devtools.ksp.symbol.*
23 import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationApplication
24 import org.jetbrains.kotlin.analysis.api.annotations.KtConstantAnnotationValue
25 import org.jetbrains.kotlin.analysis.api.annotations.KtNamedAnnotationValue
26 import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
27 import org.jetbrains.kotlin.analysis.api.components.KtConstantEvaluationMode
28 import org.jetbrains.kotlin.analysis.api.components.buildClassType
29 import org.jetbrains.kotlin.analysis.api.symbols.KtValueParameterSymbol
30 import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget.*
31 import org.jetbrains.kotlin.psi.KtParameter
32
33 class KSAnnotationImpl private constructor(private val annotationApplication: KtAnnotationApplication) : KSAnnotation {
34 companion object : KSObjectCache<KtAnnotationApplication, KSAnnotationImpl>() {
35 fun getCached(annotationApplication: KtAnnotationApplication) =
36 cache.getOrPut(annotationApplication) { KSAnnotationImpl(annotationApplication) }
37 }
38
39 override val annotationType: KSTypeReference by lazy {
40 analyze {
41 KSTypeReferenceImpl.getCached(buildClassType(annotationApplication.classId!!))
42 }
43 }
44
45 override val arguments: List<KSValueArgument> by lazy {
46 val presentArgs = annotationApplication.arguments.map { KSValueArgumentImpl.getCached(it) }
47 val presentNames = presentArgs.mapNotNull { it.name?.asString() }
48 val absentArgs = analyze {
49 annotationApplication.classId?.toKtClassSymbol()?.let { symbol ->
50 symbol.getMemberScope().getConstructors().singleOrNull()?.let {
51 it.valueParameters.filter { valueParameter ->
52 valueParameter.name.asString() !in presentNames
53 }.mapNotNull { valueParameterSymbol ->
54 valueParameterSymbol.getDefaultValue()?.let { constantValue ->
55 KSValueArgumentImpl.getCached(
56 KtNamedAnnotationValue(
57 valueParameterSymbol.name, KtConstantAnnotationValue(constantValue)
58 )
59 )
60 }
61 }
62 }
63 } ?: emptyList<KSValueArgument>()
64 }
65 presentArgs + absentArgs
66 }
67
68 override val defaultArguments: List<KSValueArgument>
69 get() = TODO("Not yet implemented")
70
71 override val shortName: KSName by lazy {
72 KSNameImpl.getCached(annotationApplication.classId!!.shortClassName.asString())
73 }
74
75 override val useSiteTarget: AnnotationUseSiteTarget? by lazy {
76 when (annotationApplication.useSiteTarget) {
77 null -> null
78 FILE -> AnnotationUseSiteTarget.FILE
79 PROPERTY -> AnnotationUseSiteTarget.PROPERTY
80 FIELD -> AnnotationUseSiteTarget.FIELD
81 PROPERTY_GETTER -> AnnotationUseSiteTarget.GET
82 PROPERTY_SETTER -> AnnotationUseSiteTarget.SET
83 RECEIVER -> AnnotationUseSiteTarget.RECEIVER
84 CONSTRUCTOR_PARAMETER -> AnnotationUseSiteTarget.PARAM
85 SETTER_PARAMETER -> AnnotationUseSiteTarget.SETPARAM
86 PROPERTY_DELEGATE_FIELD -> AnnotationUseSiteTarget.DELEGATE
87 }
88 }
89
90 override val origin: Origin = Origin.KOTLIN
91
92 override val location: Location by lazy {
93 annotationApplication.psi?.toLocation() ?: NonExistLocation
94 }
95
96 override val parent: KSNode?
97 get() = TODO("Not yet implemented")
98
99 override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
100 return visitor.visitAnnotation(this, data)
101 }
102
103 override fun toString(): String {
104 return "@${shortName.asString()}"
105 }
106 }
107
getDefaultValuenull108 internal fun KtValueParameterSymbol.getDefaultValue(): KtConstantValue? {
109 return this.psi?.let {
110 when (it) {
111 is KtParameter -> analyze {
112 it.defaultValue?.evaluate(KtConstantEvaluationMode.CONSTANT_EXPRESSION_EVALUATION)
113 }
114 else -> throw IllegalStateException("Unhandled default value type ${it.javaClass}")
115 }
116 }
117 }
118