Skip to content

Commit 0b8912b

Browse files
committed
Model Java enum entries as KSClassDeclaration
So that it is more consistent from Kotlin's point of view.
1 parent bea0d68 commit 0b8912b

File tree

3 files changed

+133
-2
lines changed

3 files changed

+133
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/symbol/impl/java/KSClassDeclarationJavaImpl.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import com.google.devtools.ksp.symbol.impl.kotlin.KSNameImpl
3131
import com.google.devtools.ksp.symbol.impl.kotlin.getKSTypeCached
3232
import com.google.devtools.ksp.symbol.impl.replaceTypeArguments
3333
import com.google.devtools.ksp.symbol.impl.toKSFunctionDeclaration
34+
import com.intellij.psi.PsiEnumConstant
3435
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
3536
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
3637
import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl
@@ -96,7 +97,11 @@ class KSClassDeclarationJavaImpl private constructor(val psi: PsiClass) : KSClas
9697
}
9798

9899
override val declarations: List<KSDeclaration> by lazy {
99-
(psi.fields.map { KSPropertyDeclarationJavaImpl.getCached(it) } +
100+
(psi.fields.map {
101+
when (it) {
102+
is PsiEnumConstant -> KSClassDeclarationJavaEnumEntryImpl.getCached(it)
103+
else -> KSPropertyDeclarationJavaImpl.getCached(it)
104+
} } +
100105
psi.innerClasses.map { KSClassDeclarationJavaImpl.getCached(it) } +
101106
psi.constructors.map { KSFunctionDeclarationJavaImpl.getCached(it) } +
102107
psi.methods.map { KSFunctionDeclarationJavaImpl.getCached(it) })

compiler-plugin/testData/api/classKinds.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// EXPECTED:
2121
// JA: ANNOTATION_CLASS
2222
// JC: CLASS
23+
// JE.ENTRY: ENUM_ENTRY
2324
// JE: ENUM_CLASS
2425
// JI: INTERFACE
2526
// KA: ANNOTATION_CLASS
@@ -49,4 +50,6 @@ enum class KE {
4950
class JC {}
5051
interface JI {}
5152
@interface JA {}
52-
enum JE {}
53+
enum JE {
54+
ENTRY
55+
}

0 commit comments

Comments
 (0)