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.symbol.*
22 import com.google.devtools.ksp.symbol.impl.getInstanceForCurrentRound
23 import com.google.devtools.ksp.symbol.impl.toLocation
24 import com.intellij.psi.PsiClassType
25 import com.intellij.psi.PsiJavaCodeReferenceElement
26 import com.intellij.psi.impl.source.PsiClassReferenceType
27 
28 class KSClassifierReferenceJavaImpl private constructor(
29     val psi: PsiClassType,
30     override val parent: KSNode
31 ) : KSClassifierReference {
32     companion object : KSObjectCache<Pair<PsiClassType, KSNode>, KSClassifierReferenceJavaImpl>() {
getCachednull33         fun getCached(psi: PsiClassType, parent: KSNode): KSClassifierReferenceJavaImpl {
34             val curParent = getInstanceForCurrentRound(parent) as KSTypeReference
35             return cache.getOrPut(Pair(psi, curParent)) { KSClassifierReferenceJavaImpl(psi, curParent) }
36         }
37     }
38 
39     override val origin = Origin.JAVA
40 
<lambda>null41     override val location: Location by lazy {
42         (psi as? PsiJavaCodeReferenceElement)?.toLocation() ?: NonExistLocation
43     }
44 
<lambda>null45     override val qualifier: KSClassifierReference? by lazy {
46         val qualifierReference = (psi as? PsiClassReferenceType)?.reference?.qualifier as? PsiJavaCodeReferenceElement
47             ?: return@lazy null
48         val qualifierType = PsiClassReferenceType(qualifierReference, psi.languageLevel)
49         getCached(qualifierType, parent)
50     }
51 
52     // PsiClassType.parameters is semantically argument
<lambda>null53     override val typeArguments: List<KSTypeArgument> by lazy {
54         psi.parameters.map { KSTypeArgumentJavaImpl.getCached(it, this) }
55     }
56 
referencedNamenull57     override fun referencedName(): String {
58         return psi.className + if (psi.parameterCount > 0) "<${
59         psi.parameters.joinToString(", ") {
60             it.presentableText
61         }
62         }>" else ""
63     }
64 
toStringnull65     override fun toString() = referencedName()
66 }
67