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
+
19
+ package com.google.devtools.ksp.symbol.impl.java
20
+
21
+ import com.google.devtools.ksp.processing.impl.ResolverImpl
22
+ import com.google.devtools.ksp.symbol.*
23
+ import com.google.devtools.ksp.symbol.impl.*
24
+ import com.google.devtools.ksp.symbol.impl.kotlin.KSExpectActualNoImpl
25
+ import com.google.devtools.ksp.symbol.impl.kotlin.KSNameImpl
26
+ import com.google.devtools.ksp.symbol.impl.kotlin.getKSTypeCached
27
+ import com.intellij.psi.PsiEnumConstant
28
+ import com.intellij.psi.PsiJavaFile
29
+ import org.jetbrains.kotlin.descriptors.ClassDescriptor
30
+ import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
31
+ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
32
+ import org.jetbrains.kotlin.descriptors.PropertyDescriptor
33
+ import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
34
+ import org.jetbrains.kotlin.resolve.scopes.getDescriptorsFiltered
35
+ import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections
36
+
37
+ class KSClassDeclarationJavaEnumEntryImpl private constructor(val psi : PsiEnumConstant ) : KSClassDeclaration, KSDeclarationJavaImpl(),
38
+ KSExpectActual by KSExpectActualNoImpl () {
39
+ companion object : KSObjectCache <PsiEnumConstant , KSClassDeclarationJavaEnumEntryImpl >() {
40
+ fun getCached (psi : PsiEnumConstant ) = cache.getOrPut(psi) { KSClassDeclarationJavaEnumEntryImpl (psi) }
41
+ }
42
+
43
+ override val origin = Origin .JAVA
44
+
45
+ override val location: Location by lazy {
46
+ psi.toLocation()
47
+ }
48
+
49
+ override val annotations: List <KSAnnotation > by lazy {
50
+ psi.annotations.map { KSAnnotationJavaImpl .getCached(it) }
51
+ }
52
+
53
+ override val classKind: ClassKind = ClassKind .ENUM_ENTRY
54
+
55
+ override val containingFile: KSFile ? by lazy {
56
+ KSFileJavaImpl .getCached(psi.containingFile as PsiJavaFile )
57
+ }
58
+
59
+ override val isCompanionObject = false
60
+
61
+ private val descriptor: ClassDescriptor ? by lazy {
62
+ ResolverImpl .instance.resolveJavaDeclaration(psi) as ClassDescriptor
63
+ }
64
+
65
+ override fun getAllFunctions (): List <KSFunctionDeclaration > {
66
+ return descriptor?.let {
67
+ ResolverImpl .instance.incrementalContext.recordLookupForGetAllFunctions(it)
68
+ it.unsubstitutedMemberScope.getDescriptorsFiltered(DescriptorKindFilter .FUNCTIONS )
69
+ .toList()
70
+ .filter { (it as FunctionDescriptor ).visibility != DescriptorVisibilities .INVISIBLE_FAKE }
71
+ .plus(it.constructors)
72
+ .map { (it as FunctionDescriptor ).toKSFunctionDeclaration() }
73
+ } ? : emptyList()
74
+ }
75
+
76
+ override fun getAllProperties (): List <KSPropertyDeclaration > {
77
+ return descriptor?.let {
78
+ ResolverImpl .instance.incrementalContext.recordLookupForGetAllProperties(it)
79
+ it.unsubstitutedMemberScope.getDescriptorsFiltered(DescriptorKindFilter .VARIABLES )
80
+ .toList()
81
+ .filter { (it as PropertyDescriptor ).visibility != DescriptorVisibilities .INVISIBLE_FAKE }
82
+ .map{ (it as PropertyDescriptor ).toKSPropertyDeclaration() }
83
+ } ? : emptyList()
84
+ }
85
+
86
+ override val declarations: List <KSDeclaration > = emptyList()
87
+
88
+ override val modifiers: Set <Modifier > by lazy {
89
+ val modifiers = mutableSetOf<Modifier >()
90
+ modifiers.addAll(psi.toKSModifiers())
91
+ modifiers
92
+ }
93
+
94
+ override val parentDeclaration: KSDeclaration ? by lazy {
95
+ psi.findParentDeclaration()
96
+ }
97
+
98
+ override val primaryConstructor: KSFunctionDeclaration ? = null
99
+
100
+ override val qualifiedName: KSName by lazy {
101
+ KSNameImpl .getCached(" ${parentDeclaration!! .qualifiedName!! .asString()} .${psi.name} " )
102
+ }
103
+
104
+ override val simpleName: KSName by lazy {
105
+ KSNameImpl .getCached(psi.name)
106
+ }
107
+
108
+ override val superTypes: List <KSTypeReference > = emptyList()
109
+
110
+ override val typeParameters: List <KSTypeParameter > = emptyList()
111
+
112
+ override fun asType (typeArguments : List <KSTypeArgument >): KSType {
113
+ return getKSTypeCached(descriptor!! .defaultType.replaceTypeArguments(typeArguments), typeArguments)
114
+ }
115
+
116
+ override fun asStarProjectedType (): KSType {
117
+ return getKSTypeCached(descriptor!! .defaultType.replaceArgumentsWithStarProjections())
118
+ }
119
+
120
+ override fun <D , R > accept (visitor : KSVisitor <D , R >, data : D ): R {
121
+ return visitor.visitClassDeclaration(this , data)
122
+ }
123
+ }
0 commit comments