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.memoized
22 import com.google.devtools.ksp.symbol.*
23 import com.google.devtools.ksp.symbol.impl.kotlin.KSTypeArgumentImpl
24 import com.google.devtools.ksp.symbol.impl.toLocation
25 import com.intellij.psi.PsiType
26 import com.intellij.psi.PsiWildcardType
27 import com.intellij.psi.impl.source.PsiClassReferenceType
28 
29 class KSTypeArgumentJavaImpl private constructor(
30     val psi: PsiType,
31     override val parent: KSNode?
32 ) : KSTypeArgumentImpl() {
33     companion object : KSObjectCache<PsiType, KSTypeArgumentJavaImpl>() {
<lambda>null34         fun getCached(psi: PsiType, parent: KSNode?) = cache.getOrPut(psi) { KSTypeArgumentJavaImpl(psi, parent) }
35     }
36 
37     override val origin = Origin.JAVA
38 
<lambda>null39     override val location: Location by lazy {
40         (psi as? PsiClassReferenceType)?.reference?.toLocation() ?: NonExistLocation
41     }
42 
<lambda>null43     override val annotations: Sequence<KSAnnotation> by lazy {
44         psi.annotations.asSequence().map { KSAnnotationJavaImpl.getCached(it) }.memoized()
45     }
46 
47     // Could be unbounded, need to model unbdouned type argument.
<lambda>null48     override val type: KSTypeReference? by lazy {
49         KSTypeReferenceJavaImpl.getCached(psi, this)
50     }
51 
<lambda>null52     override val variance: Variance by lazy {
53         if (psi is PsiWildcardType) {
54             when {
55                 psi.isExtends -> Variance.COVARIANT
56                 psi.isSuper -> Variance.CONTRAVARIANT
57                 psi.bound == null -> Variance.STAR
58                 else -> Variance.INVARIANT
59             }
60         } else {
61             Variance.INVARIANT
62         }
63     }
64 }
65