• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 Google LLC
3  * Copyright 2010-2020 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.symbol.impl.kotlin
19 
20 import com.google.devtools.ksp.KSObjectCache
21 import com.google.devtools.ksp.processing.impl.KSNameImpl
22 import com.google.devtools.ksp.processing.impl.ResolverImpl
23 import com.google.devtools.ksp.symbol.*
24 import com.google.devtools.ksp.symbol.impl.binary.createKSValueArguments
25 import com.google.devtools.ksp.symbol.impl.binary.getDefaultArguments
26 import com.google.devtools.ksp.symbol.impl.toLocation
27 import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
28 import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget.*
29 import org.jetbrains.kotlin.psi.KtAnnotationEntry
30 import org.jetbrains.kotlin.psi.KtClassOrObject
31 import org.jetbrains.kotlin.psi.KtFile
32 import org.jetbrains.kotlin.psi.KtFunction
33 import org.jetbrains.kotlin.psi.KtParameter
34 import org.jetbrains.kotlin.psi.KtProperty
35 import org.jetbrains.kotlin.psi.KtPropertyAccessor
36 import org.jetbrains.kotlin.psi.KtTypeAlias
37 import org.jetbrains.kotlin.psi.KtTypeParameter
38 import org.jetbrains.kotlin.psi.KtTypeProjection
39 import org.jetbrains.kotlin.psi.KtTypeReference
40 
41 class KSAnnotationImpl private constructor(val ktAnnotationEntry: KtAnnotationEntry) : KSAnnotation {
42     companion object : KSObjectCache<KtAnnotationEntry, KSAnnotationImpl>() {
<lambda>null43         fun getCached(ktAnnotationEntry: KtAnnotationEntry) = cache.getOrPut(ktAnnotationEntry) {
44             KSAnnotationImpl(ktAnnotationEntry)
45         }
46     }
47 
48     override val origin = Origin.KOTLIN
49 
<lambda>null50     override val parent: KSNode? by lazy {
51         var parentPsi = ktAnnotationEntry.parent
52         while (true) {
53             when (parentPsi) {
54                 null, is KtFile, is KtClassOrObject, is KtFunction, is KtParameter, is KtTypeParameter,
55                 is KtTypeAlias, is KtProperty, is KtPropertyAccessor, is KtTypeProjection, is KtTypeReference -> break
56                 else -> parentPsi = parentPsi.parent
57             }
58         }
59         when (parentPsi) {
60             is KtFile -> KSFileImpl.getCached(parentPsi)
61             is KtClassOrObject -> KSClassDeclarationImpl.getCached(parentPsi)
62             is KtFunction -> KSFunctionDeclarationImpl.getCached(parentPsi)
63             is KtParameter -> KSValueParameterImpl.getCached(parentPsi)
64             is KtTypeParameter -> KSTypeParameterImpl.getCached(parentPsi)
65             is KtTypeAlias -> KSTypeAliasImpl.getCached(parentPsi)
66             is KtProperty -> KSPropertyDeclarationImpl.getCached(parentPsi)
67             is KtPropertyAccessor -> KSPropertyAccessorImpl.getCached(parentPsi)
68             is KtTypeProjection -> KSTypeArgumentKtImpl.getCached(parentPsi)
69             is KtTypeReference -> KSTypeReferenceImpl.getCached(parentPsi)
70             else -> null
71         }
72     }
73 
<lambda>null74     override val location: Location by lazy {
75         ktAnnotationEntry.toLocation()
76     }
77 
<lambda>null78     override val annotationType: KSTypeReference by lazy {
79         KSTypeReferenceImpl.getCached(ktAnnotationEntry.typeReference!!)
80     }
81 
<lambda>null82     override val arguments: List<KSValueArgument> by lazy {
83         resolved?.createKSValueArguments(this) ?: emptyList()
84     }
85 
<lambda>null86     override val defaultArguments: List<KSValueArgument> by lazy {
87         resolved?.getDefaultArguments(this) ?: emptyList()
88     }
89 
<lambda>null90     override val shortName: KSName by lazy {
91         KSNameImpl.getCached(ktAnnotationEntry.shortName!!.asString())
92     }
93 
<lambda>null94     override val useSiteTarget: AnnotationUseSiteTarget? by lazy {
95         when (ktAnnotationEntry.useSiteTarget?.getAnnotationUseSiteTarget()) {
96             null -> null
97             FILE -> AnnotationUseSiteTarget.FILE
98             PROPERTY -> AnnotationUseSiteTarget.PROPERTY
99             FIELD -> AnnotationUseSiteTarget.FIELD
100             PROPERTY_GETTER -> AnnotationUseSiteTarget.GET
101             PROPERTY_SETTER -> AnnotationUseSiteTarget.SET
102             RECEIVER -> AnnotationUseSiteTarget.RECEIVER
103             CONSTRUCTOR_PARAMETER -> AnnotationUseSiteTarget.PARAM
104             SETTER_PARAMETER -> AnnotationUseSiteTarget.SETPARAM
105             PROPERTY_DELEGATE_FIELD -> AnnotationUseSiteTarget.DELEGATE
106         }
107     }
108 
acceptnull109     override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
110         return visitor.visitAnnotation(this, data)
111     }
112 
<lambda>null113     private val resolved: AnnotationDescriptor? by lazy {
114         ResolverImpl.instance!!.resolveAnnotationEntry(ktAnnotationEntry)
115     }
116 
toStringnull117     override fun toString(): String {
118         return "@${shortName.asString()}"
119     }
120 }
121