Skip to content

Commit

Permalink
Replace casts with abstract functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukellmann committed Nov 9, 2023
1 parent c37900c commit cda8056
Show file tree
Hide file tree
Showing 33 changed files with 85 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class KSClassDeclarationDescriptorImpl private constructor(val descriptor: Class
}
}

override fun asKSDeclaration(): KSDeclaration = this

override val classKind: ClassKind by lazy {
when (descriptor.kind) {
KtClassKind.INTERFACE -> ClassKind.INTERFACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.parentsWithSelf
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull

abstract class KSDeclarationDescriptorImpl(private val descriptor: DeclarationDescriptor) /*: KSDeclaration*/ {
abstract fun asKSDeclaration(): KSDeclaration

open /*override*/ val origin by lazy {
descriptor.origin
Expand All @@ -41,7 +42,7 @@ abstract class KSDeclarationDescriptorImpl(private val descriptor: DeclarationDe
open /*override*/ val location: Location = NonExistLocation

open /*override*/ val annotations: Sequence<KSAnnotation> by lazy {
descriptor.annotations.asSequence().map { KSAnnotationDescriptorImpl.getCached(it, this as KSDeclaration) }
descriptor.annotations.asSequence().map { KSAnnotationDescriptorImpl.getCached(it, this.asKSDeclaration()) }
.memoized()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class KSFunctionDeclarationDescriptorImpl private constructor(val descriptor: Fu
cache.getOrPut(descriptor) { KSFunctionDeclarationDescriptorImpl(descriptor) }
}

override fun asKSDeclaration(): KSDeclaration = this

override fun findOverridee(): KSDeclaration? {
return descriptor?.findClosestOverridee()?.toKSDeclaration()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.symbol.impl.toFunctionKSModifiers
import com.google.devtools.ksp.symbol.impl.toKSModifiers
import com.google.devtools.ksp.symbol.impl.toKSPropertyDeclaration
import com.google.devtools.ksp.toKSModifiers
import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor

abstract class KSPropertyAccessorDescriptorImpl(val descriptor: PropertyAccessorDescriptor) /*: KSPropertyAccessor*/ {
protected abstract fun asKSPropertyAccessor(): KSPropertyAccessor

open /*override*/ val origin: Origin by lazy {
when (receiver.origin) {
// if receiver is kotlin source, that means we are a synthetic where developer
Expand All @@ -50,7 +53,8 @@ abstract class KSPropertyAccessorDescriptorImpl(val descriptor: PropertyAccessor
}

open /*override*/ val annotations: Sequence<KSAnnotation> by lazy {
descriptor.annotations.asSequence().map { KSAnnotationDescriptorImpl.getCached(it, this as KSPropertyAccessor) }
descriptor.annotations.asSequence()
.map { KSAnnotationDescriptorImpl.getCached(it, this.asKSPropertyAccessor()) }
.memoized()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class KSPropertyDeclarationDescriptorImpl private constructor(val descriptor: Pr
}
}

override fun asKSDeclaration(): KSDeclaration = this

override val extensionReceiver: KSTypeReference? by lazy {
if (descriptor.extensionReceiverParameter != null) {
KSTypeReferenceDescriptorImpl.getCached(descriptor.extensionReceiverParameter!!.type, origin, this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class KSPropertyGetterDescriptorImpl private constructor(descriptor: PropertyGet
}
}

override fun asKSPropertyAccessor(): KSPropertyAccessor = this

override val returnType: KSTypeReference? by lazy {
if (descriptor.returnType != null) {
KSTypeReferenceDescriptorImpl.getCached(descriptor.returnType!!, origin, this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class KSPropertySetterDescriptorImpl private constructor(descriptor: PropertySet
}
}

override fun asKSPropertyAccessor(): KSPropertyAccessor = this

override val parameter: KSValueParameter by lazy {
descriptor.valueParameters.singleOrNull()?.let { KSValueParameterDescriptorImpl.getCached(it, this) }
?: throw IllegalStateException("Failed to resolve property type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class KSTypeAliasDescriptorImpl(descriptor: TypeAliasDescriptor) :
}
}

override fun asKSDeclaration(): KSDeclaration = this

override val name: KSName by lazy {
KSNameImpl.getCached(descriptor.name.asString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class KSTypeParameterDescriptorImpl private constructor(val descriptor: TypePara
}
}

override fun asKSDeclaration(): KSDeclaration = this

override val bounds: Sequence<KSTypeReference> by lazy {
descriptor.upperBounds.asSequence().map { KSTypeReferenceDescriptorImpl.getCached(it, origin, this) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class KSClassDeclarationJavaEnumEntryImpl private constructor(val psi: PsiEnumCo
fun getCached(psi: PsiEnumConstant) = cache.getOrPut(psi) { KSClassDeclarationJavaEnumEntryImpl(psi) }
}

override fun asKSDeclaration(): KSDeclaration = this

override val origin = Origin.JAVA

override val location: Location by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class KSClassDeclarationJavaImpl private constructor(val psi: PsiClass) :
fun getCached(psi: PsiClass) = cache.getOrPut(psi) { KSClassDeclarationJavaImpl(psi) }
}

override fun asKSDeclaration(): KSDeclaration = this

override val origin = Origin.JAVA

override val location: Location by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ import com.google.devtools.ksp.symbol.impl.getDocString
import com.intellij.psi.PsiElement

abstract class KSDeclarationJavaImpl(private val psi: PsiElement) /*: KSDeclaration*/ {
protected abstract fun asKSDeclaration(): KSDeclaration

open /*override*/ val packageName: KSName by lazy {
(this as KSDeclaration).containingFile!!.packageName
this.asKSDeclaration().containingFile!!.packageName
}

override fun toString(): String {
return (this as KSDeclaration).simpleName.asString()
return this.asKSDeclaration().simpleName.asString()
}

open /*override*/ val docString by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class KSFunctionDeclarationJavaImpl private constructor(val psi: PsiMethod) :
fun getCached(psi: PsiMethod) = cache.getOrPut(psi) { KSFunctionDeclarationJavaImpl(psi) }
}

override fun asKSDeclaration(): KSDeclaration = this

override val origin = Origin.JAVA

override val location: Location by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class KSPropertyDeclarationJavaImpl private constructor(val psi: PsiField) :
fun getCached(psi: PsiField) = cache.getOrPut(psi) { KSPropertyDeclarationJavaImpl(psi) }
}

override fun asKSDeclaration(): KSDeclaration = this

override val origin = Origin.JAVA

override val location: Location by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class KSTypeParameterJavaImpl private constructor(val psi: PsiTypeParameter) :
fun getCached(psi: PsiTypeParameter) = cache.getOrPut(psi) { KSTypeParameterJavaImpl(psi) }
}

override fun asKSDeclaration(): KSDeclaration = this

override val origin = Origin.JAVA

override val location: Location by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ abstract class KSPropertyAccessorImpl(val ktPropertyAccessor: KtPropertyAccessor
}
}
}

protected abstract fun asKSPropertyAccessor(): KSPropertyAccessor

open /*override*/ val receiver: KSPropertyDeclaration by lazy {
KSPropertyDeclarationImpl.getCached(ktPropertyAccessor.property as KtProperty)
}
open /*override*/ val annotations: Sequence<KSAnnotation> by lazy {
ktPropertyAccessor.filterUseSiteTargetAnnotations().map { KSAnnotationImpl.getCached(it) }
.plus((this as KSPropertyAccessor).findAnnotationFromUseSiteTarget())
.plus(this.asKSPropertyAccessor().findAnnotationFromUseSiteTarget())
}

open /*override*/ val parent: KSNode? by lazy {
Expand All @@ -68,7 +71,7 @@ abstract class KSPropertyAccessorImpl(val ktPropertyAccessor: KtPropertyAccessor
open /*override*/ val origin: Origin = Origin.KOTLIN

open /*override*/ fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
return visitor.visitPropertyAccessor(this as KSPropertyAccessor, data)
return visitor.visitPropertyAccessor(this.asKSPropertyAccessor(), data)
}

internal val originalAnnotations: List<KSAnnotation> by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class KSPropertyGetterImpl private constructor(ktPropertyGetter: KtPropertyAcces
}
}

override fun asKSPropertyAccessor(): KSPropertyAccessor = this

override val returnType: KSTypeReference? by lazy {
val property = ktPropertyGetter.property
if (property.typeReference != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class KSPropertySetterImpl private constructor(ktPropertySetter: KtPropertyAcces
}
}

override fun asKSPropertyAccessor(): KSPropertyAccessor = this

override val parameter: KSValueParameter by lazy {
ktPropertySetter.parameterList?.parameters?.singleOrNull()?.let { KSValueParameterImpl.getCached(it) }
?: KSValueParameterSyntheticImpl.getCached(this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import com.google.devtools.ksp.processing.impl.findAnnotationFromUseSiteTarget
import com.google.devtools.ksp.symbol.*

abstract class KSPropertyAccessorSyntheticImpl(ksPropertyDeclaration: KSPropertyDeclaration) /*: KSPropertyAccessor*/ {
protected abstract fun asKSPropertyAccessor(): KSPropertyAccessor

open /*override*/ val annotations: Sequence<KSAnnotation> by lazy {
(this as KSPropertyAccessor).findAnnotationFromUseSiteTarget()
this.asKSPropertyAccessor().findAnnotationFromUseSiteTarget()
}

open /*override*/ val location: Location by lazy {
Expand All @@ -36,6 +38,6 @@ abstract class KSPropertyAccessorSyntheticImpl(ksPropertyDeclaration: KSProperty
open /*override*/ val receiver: KSPropertyDeclaration = ksPropertyDeclaration

open /*override*/ fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
return visitor.visitPropertyAccessor(this as KSPropertyAccessor, data)
return visitor.visitPropertyAccessor(this.asKSPropertyAccessor(), data)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class KSPropertyGetterSyntheticImpl(val ksPropertyDeclaration: KSPropertyDeclara
}
}

override fun asKSPropertyAccessor(): KSPropertyAccessor = this

private val descriptor: PropertyAccessorDescriptor by lazy {
ResolverImpl.instance!!.resolvePropertyDeclaration(ksPropertyDeclaration)!!.getter!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class KSPropertySetterSyntheticImpl(val ksPropertyDeclaration: KSPropertyDeclara
}
}

override fun asKSPropertyAccessor(): KSPropertyAccessor = this

private val descriptor: PropertyAccessorDescriptor by lazy {
ResolverImpl.instance!!.resolvePropertyDeclaration(ksPropertyDeclaration)!!.setter!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ internal class DeclarationOrdering(
}

private fun getOrder(decl: KSDeclarationDescriptorImpl): Int {
return declOrdering.getOrPut(decl as KSDeclaration) {
return declOrdering.getOrPut(decl.asKSDeclaration()) {
when (decl) {
is KSPropertyDeclarationDescriptorImpl -> {
fieldOrdering[decl.simpleName.asString()]?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtModifierListOwner

abstract class AbstractKSDeclarationImpl(val ktDeclarationSymbol: KtDeclarationSymbol) : /*KSDeclaration,*/ Deferrable {
abstract fun asKSDeclaration(): KSDeclaration

open /*override*/ val origin: Origin by lazy {
mapAAOrigin(ktDeclarationSymbol)
}
Expand Down Expand Up @@ -108,9 +110,9 @@ abstract class AbstractKSDeclarationImpl(val ktDeclarationSymbol: KtDeclarationS
get() = ktDeclarationSymbol.toDocString()

internal val originalAnnotations = if (ktDeclarationSymbol.psi is KtElement || ktDeclarationSymbol.psi == null) {
ktDeclarationSymbol.annotations(this as KSDeclaration)
ktDeclarationSymbol.annotations(this.asKSDeclaration())
} else {
(ktDeclarationSymbol.psi as PsiJvmModifiersOwner)
.annotations.map { KSAnnotationJavaImpl.getCached(it, this as KSDeclaration) }.asSequence()
.annotations.map { KSAnnotationJavaImpl.getCached(it, this.asKSDeclaration()) }.asSequence()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class KSClassDeclarationEnumEntryImpl private constructor(private val ktEnumEntr
cache.getOrPut(ktEnumEntrySymbol) { KSClassDeclarationEnumEntryImpl(ktEnumEntrySymbol) }
}

override fun asKSDeclaration(): KSDeclaration = this

override val qualifiedName: KSName? by lazy {
KSNameImpl.getCached("${this.parentDeclaration!!.qualifiedName!!.asString()}.${simpleName.asString()}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class KSClassDeclarationImpl private constructor(internal val ktClassOrObjectSym
cache.getOrPut(ktClassOrObjectSymbol) { KSClassDeclarationImpl(ktClassOrObjectSymbol) }
}

override fun asKSDeclaration(): KSDeclaration = this

override val qualifiedName: KSName? by lazy {
ktClassOrObjectSymbol.classIdIfNonLocal?.asFqNameString()?.let { KSNameImpl.getCached(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class KSFunctionDeclarationImpl private constructor(internal val ktFunctionSymbo
cache.getOrPut(ktFunctionSymbol) { KSFunctionDeclarationImpl(ktFunctionSymbol) }
}

override fun asKSDeclaration(): KSDeclaration = this

override val functionKind: FunctionKind by lazy {
when (ktFunctionSymbol.symbolKind) {
KtSymbolKind.CLASS_MEMBER -> FunctionKind.MEMBER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ abstract class KSPropertyAccessorImpl(
internal val ktPropertyAccessorSymbol: KtPropertyAccessorSymbol,
open /*override*/ val receiver: KSPropertyDeclaration
) : /*KSPropertyAccessor,*/ Deferrable {
protected abstract fun asKSPropertyAccessor(): KSPropertyAccessor

open /*override*/ val annotations: Sequence<KSAnnotation> by lazy {
ktPropertyAccessorSymbol.annotations.asSequence()
.filter { it.useSiteTarget != AnnotationUseSiteTarget.SETTER_PARAMETER }
.map { KSAnnotationImpl.getCached(it, this as KSPropertyAccessor) }
.plus((this as KSPropertyAccessor).findAnnotationFromUseSiteTarget())
.map { KSAnnotationImpl.getCached(it, this.asKSPropertyAccessor()) }
.plus(this.asKSPropertyAccessor().findAnnotationFromUseSiteTarget())
}

internal val originalAnnotations = ktPropertyAccessorSymbol.annotations(this as KSPropertyAccessor)
internal val originalAnnotations = ktPropertyAccessorSymbol.annotations(this.asKSPropertyAccessor())

open /*override*/ val location: Location by lazy {
ktPropertyAccessorSymbol.psi?.toLocation() ?: NonExistLocation
Expand Down Expand Up @@ -96,6 +97,8 @@ class KSPropertySetterImpl private constructor(
cache.getOrPut(Pair(owner, setter)) { KSPropertySetterImpl(owner, setter) }
}

override fun asKSPropertyAccessor(): KSPropertyAccessor = this

override val parameter: KSValueParameter by lazy {
KSValueParameterImpl.getCached(setter.parameter, this)
}
Expand Down Expand Up @@ -126,6 +129,8 @@ class KSPropertyGetterImpl private constructor(
cache.getOrPut(Pair(owner, getter)) { KSPropertyGetterImpl(owner, getter) }
}

override fun asKSPropertyAccessor(): KSPropertyAccessor = this

override val returnType: KSTypeReference? by lazy {
((owner as? KSPropertyDeclarationImpl)?.ktPropertySymbol?.psiIfSource() as? KtProperty)?.typeReference
?.let { KSTypeReferenceImpl.getCached(it, this) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class KSPropertyDeclarationImpl private constructor(internal val ktPropertySymbo
cache.getOrPut(ktPropertySymbol) { KSPropertyDeclarationImpl(ktPropertySymbol) }
}

override fun asKSDeclaration(): KSDeclaration = this

override val annotations: Sequence<KSAnnotation> by lazy {
ktPropertySymbol.annotations.asSequence()
.filter { !it.isUseSiteTargetAnnotation() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class KSPropertyDeclarationJavaImpl private constructor(private val ktJavaFieldS
fun getCached(ktJavaFieldSymbol: KtJavaFieldSymbol): KSPropertyDeclaration =
cache.getOrPut(ktJavaFieldSymbol) { KSPropertyDeclarationJavaImpl(ktJavaFieldSymbol) }
}

override fun asKSDeclaration(): KSDeclaration = this

override val getter: KSPropertyGetter?
get() = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.google.devtools.ksp.impl.symbol.kotlin

import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.symbol.KSDeclaration
import com.google.devtools.ksp.symbol.KSExpectActual
import com.google.devtools.ksp.symbol.KSName
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
Expand All @@ -23,6 +24,8 @@ class KSPropertyDeclarationLocalVariableImpl private constructor(
cache.getOrPut(ktLocalVariableSymbol) { KSPropertyDeclarationLocalVariableImpl(ktLocalVariableSymbol) }
}

override fun asKSDeclaration(): KSDeclaration = this

override val getter: KSPropertyGetter? = null

override val setter: KSPropertySetter? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.google.devtools.ksp.impl.symbol.kotlin
import com.google.devtools.ksp.KSObjectCache
import com.google.devtools.ksp.impl.symbol.kotlin.resolved.KSTypeReferenceResolvedImpl
import com.google.devtools.ksp.processing.impl.KSNameImpl
import com.google.devtools.ksp.symbol.KSDeclaration
import com.google.devtools.ksp.symbol.KSExpectActual
import com.google.devtools.ksp.symbol.KSName
import com.google.devtools.ksp.symbol.KSTypeAlias
Expand All @@ -36,6 +37,8 @@ class KSTypeAliasImpl private constructor(private val ktTypeAliasSymbol: KtTypeA
cache.getOrPut(ktTypeAliasSymbol) { KSTypeAliasImpl(ktTypeAliasSymbol) }
}

override fun asKSDeclaration(): KSDeclaration = this

override val name: KSName by lazy {
KSNameImpl.getCached(ktTypeAliasSymbol.nameOrAnonymous.asString())
}
Expand Down
Loading

0 comments on commit cda8056

Please sign in to comment.