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.processing.impl.ResolverImpl
23 import com.google.devtools.ksp.symbol.*
24 import com.google.devtools.ksp.symbol.impl.*
25 import com.google.devtools.ksp.symbol.impl.binary.getAllFunctions
26 import com.google.devtools.ksp.symbol.impl.binary.getAllProperties
27 import com.google.devtools.ksp.symbol.impl.kotlin.KSErrorType
28 import com.google.devtools.ksp.symbol.impl.kotlin.KSExpectActualNoImpl
29 import com.google.devtools.ksp.symbol.impl.kotlin.getKSTypeCached
30 import com.google.devtools.ksp.toKSModifiers
31 import com.intellij.psi.PsiEnumConstant
32 import com.intellij.psi.PsiJavaFile
33 import org.jetbrains.kotlin.descriptors.ClassDescriptor
34 
35 class KSClassDeclarationJavaEnumEntryImpl private constructor(val psi: PsiEnumConstant) :
36     KSClassDeclaration,
37     KSDeclarationJavaImpl(psi),
38     KSExpectActual by KSExpectActualNoImpl() {
39     companion object : KSObjectCache<PsiEnumConstant, KSClassDeclarationJavaEnumEntryImpl>() {
<lambda>null40         fun getCached(psi: PsiEnumConstant) = cache.getOrPut(psi) { KSClassDeclarationJavaEnumEntryImpl(psi) }
41     }
42 
43     override val origin = Origin.JAVA
44 
<lambda>null45     override val location: Location by lazy {
46         psi.toLocation()
47     }
48 
<lambda>null49     override val annotations: Sequence<KSAnnotation> by lazy {
50         psi.annotations.asSequence().map { KSAnnotationJavaImpl.getCached(it) }
51     }
52 
53     override val classKind: ClassKind = ClassKind.ENUM_ENTRY
54 
<lambda>null55     override val containingFile: KSFile? by lazy {
56         KSFileJavaImpl.getCached(psi.containingFile as PsiJavaFile)
57     }
58 
59     override val isCompanionObject = false
60 
getSealedSubclassesnull61     override fun getSealedSubclasses(): Sequence<KSClassDeclaration> = emptySequence()
62 
63     private val descriptor: ClassDescriptor? by lazy {
64         ResolverImpl.instance!!.resolveJavaDeclaration(psi) as ClassDescriptor
65     }
66 
getAllFunctionsnull67     override fun getAllFunctions(): Sequence<KSFunctionDeclaration> =
68         descriptor?.getAllFunctions() ?: emptySequence()
69 
70     override fun getAllProperties(): Sequence<KSPropertyDeclaration> =
71         descriptor?.getAllProperties() ?: emptySequence()
72 
73     override val declarations: Sequence<KSDeclaration> = emptySequence()
74 
75     override val modifiers: Set<Modifier> by lazy {
76         psi.toKSModifiers()
77     }
78 
<lambda>null79     override val parentDeclaration: KSDeclaration? by lazy {
80         psi.findParentDeclaration()
81     }
82 
83     override val primaryConstructor: KSFunctionDeclaration? = null
84 
<lambda>null85     override val qualifiedName: KSName by lazy {
86         KSNameImpl.getCached("${parentDeclaration!!.qualifiedName!!.asString()}.${psi.name}")
87     }
88 
<lambda>null89     override val simpleName: KSName by lazy {
90         KSNameImpl.getCached(psi.name)
91     }
92 
93     override val superTypes: Sequence<KSTypeReference> = emptySequence()
94 
95     override val typeParameters: List<KSTypeParameter> = emptyList()
96 
97     // Enum can't have type parameters.
asTypenull98     override fun asType(typeArguments: List<KSTypeArgument>): KSType {
99         if (typeArguments.isNotEmpty())
100             return KSErrorType
101         return asStarProjectedType()
102     }
103 
asStarProjectedTypenull104     override fun asStarProjectedType(): KSType {
105         return getKSTypeCached(descriptor!!.defaultType)
106     }
107 
acceptnull108     override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
109         return visitor.visitClassDeclaration(this, data)
110     }
111 }
112