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.java
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 com.google.devtools.ksp.symbol.impl.toLocation
24 import com.intellij.psi.PsiAnnotation
25 import com.intellij.psi.PsiMethod
26 import com.intellij.psi.PsiParameter
27 
28 class KSValueParameterJavaImpl private constructor(val psi: PsiParameter) : KSValueParameter {
29     companion object : KSObjectCache<PsiParameter, KSValueParameterJavaImpl>() {
<lambda>null30         fun getCached(psi: PsiParameter) = cache.getOrPut(psi) { KSValueParameterJavaImpl(psi) }
31     }
32 
33     override val origin = Origin.JAVA
34 
<lambda>null35     override val location: Location by lazy {
36         psi.toLocation()
37     }
<lambda>null38     override val parent: KSNode? by lazy {
39         var parentPsi = psi.parent
40         while (true) {
41             when (parentPsi) {
42                 null, is PsiMethod, is PsiAnnotation -> break
43                 else -> parentPsi = parentPsi.parent
44             }
45         }
46         when (parentPsi) {
47             is PsiMethod -> KSFunctionDeclarationJavaImpl.getCached(parentPsi)
48             is PsiAnnotation -> KSAnnotationJavaImpl.getCached(parentPsi)
49             else -> null
50         }
51     }
52 
<lambda>null53     override val annotations: Sequence<KSAnnotation> by lazy {
54         psi.annotations.asSequence().map { KSAnnotationJavaImpl.getCached(it) }
55     }
56 
57     override val isCrossInline: Boolean = false
58 
59     override val isNoInline: Boolean = false
60 
61     override val isVararg: Boolean = psi.isVarArgs
62 
63     override val isVal: Boolean = false
64 
65     override val isVar: Boolean = false
66 
<lambda>null67     override val name: KSName? by lazy {
68         if (psi.name != null) {
69             KSNameImpl.getCached(psi.name!!)
70         } else {
71             null
72         }
73     }
74 
<lambda>null75     override val type: KSTypeReference by lazy {
76         KSTypeReferenceJavaImpl.getCached(psi.type, this)
77     }
78 
79     override val hasDefault: Boolean = false
80 
acceptnull81     override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
82         return visitor.visitValueParameter(this, data)
83     }
84 
toStringnull85     override fun toString(): String {
86         return name?.asString() ?: "_"
87     }
88 }
89