-
Notifications
You must be signed in to change notification settings - Fork 252
/
KSAnnotationImpl.kt
147 lines (135 loc) · 6.58 KB
/
KSAnnotationImpl.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* 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.
*/
package com.google.devtools.ksp.impl.symbol.kotlin
import com.google.devtools.ksp.common.IdKeyPair
import com.google.devtools.ksp.common.KSObjectCache
import com.google.devtools.ksp.common.impl.KSNameImpl
import com.google.devtools.ksp.impl.ResolverAAImpl
import com.google.devtools.ksp.impl.symbol.java.KSValueArgumentLiteImpl
import com.google.devtools.ksp.impl.symbol.java.calcValue
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.symbol.*
import com.intellij.psi.PsiAnnotationMethod
import com.intellij.psi.PsiArrayInitializerMemberValue
import com.intellij.psi.PsiClass
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationApplicationWithArgumentsInfo
import org.jetbrains.kotlin.analysis.api.annotations.KtNamedAnnotationValue
import org.jetbrains.kotlin.analysis.api.components.buildClassType
import org.jetbrains.kotlin.analysis.api.lifetime.KtAlwaysAccessibleLifetimeToken
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbolOrigin
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget.*
import org.jetbrains.kotlin.psi.KtAnnotationEntry
// TODO: implement a psi based version of annotation application.
class KSAnnotationImpl private constructor(
private val annotationApplication: KtAnnotationApplicationWithArgumentsInfo,
override val parent: KSNode?
) : KSAnnotation {
companion object : KSObjectCache<IdKeyPair<KtAnnotationApplicationWithArgumentsInfo, KSNode?>, KSAnnotationImpl>() {
fun getCached(annotationApplication: KtAnnotationApplicationWithArgumentsInfo, parent: KSNode? = null) =
cache.getOrPut(IdKeyPair(annotationApplication, parent)) { KSAnnotationImpl(annotationApplication, parent) }
}
override val annotationType: KSTypeReference by lazy {
analyze {
if (annotationApplication.psi is KtAnnotationEntry) {
KSTypeReferenceImpl.getCached(
(annotationApplication.psi as KtAnnotationEntry).typeReference!!,
parent = this@KSAnnotationImpl
)
} else {
KSTypeReferenceResolvedImpl.getCached(
buildClassType(annotationApplication.classId!!),
parent = this@KSAnnotationImpl
)
}
}
}
override val arguments: List<KSValueArgument> by lazy {
val presentArgs = annotationApplication.arguments.map { KSValueArgumentImpl.getCached(it, Origin.KOTLIN) }
val presentNames = presentArgs.mapNotNull { it.name?.asString() }
val absentArgs = defaultArguments.filter {
it.name?.asString() !in presentNames
}
presentArgs + absentArgs
}
override val defaultArguments: List<KSValueArgument> by lazy {
analyze {
annotationApplication.classId?.toKtClassSymbol()?.let { symbol ->
if (symbol.origin == KtSymbolOrigin.JAVA && symbol.psi != null) {
(symbol.psi as PsiClass).allMethods.filterIsInstance<PsiAnnotationMethod>()
.mapNotNull { annoMethod ->
annoMethod.defaultValue?.let { value ->
val calculatedValue: Any? = if (value is PsiArrayInitializerMemberValue) {
value.initializers.map {
calcValue(it)
}
} else {
calcValue(value)
}
KSValueArgumentLiteImpl.getCached(
KSNameImpl.getCached(annoMethod.name),
calculatedValue,
Origin.SYNTHETIC
)
}
}
} else {
symbol.getMemberScope().getConstructors().singleOrNull()?.let {
it.valueParameters.mapNotNull { valueParameterSymbol ->
valueParameterSymbol.getDefaultValue()?.let { constantValue ->
KSValueArgumentImpl.getCached(
KtNamedAnnotationValue(
valueParameterSymbol.name,
constantValue,
KtAlwaysAccessibleLifetimeToken(ResolverAAImpl.ktModule.project)
),
Origin.SYNTHETIC
)
}
}
}
}
} ?: emptyList()
}
}
override val shortName: KSName by lazy {
KSNameImpl.getCached(annotationApplication.classId!!.shortClassName.asString())
}
override val useSiteTarget: AnnotationUseSiteTarget? by lazy {
when (annotationApplication.useSiteTarget) {
null -> null
FILE -> AnnotationUseSiteTarget.FILE
PROPERTY -> AnnotationUseSiteTarget.PROPERTY
FIELD -> AnnotationUseSiteTarget.FIELD
PROPERTY_GETTER -> AnnotationUseSiteTarget.GET
PROPERTY_SETTER -> AnnotationUseSiteTarget.SET
RECEIVER -> AnnotationUseSiteTarget.RECEIVER
CONSTRUCTOR_PARAMETER -> AnnotationUseSiteTarget.PARAM
SETTER_PARAMETER -> AnnotationUseSiteTarget.SETPARAM
PROPERTY_DELEGATE_FIELD -> AnnotationUseSiteTarget.DELEGATE
}
}
override val origin: Origin = Origin.KOTLIN
override val location: Location by lazy {
annotationApplication.psi?.toLocation() ?: NonExistLocation
}
override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
return visitor.visitAnnotation(this, data)
}
override fun toString(): String {
return "@${shortName.asString()}"
}
}