Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KSP: Cache overridee #9753

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
package io.micronaut.kotlin.processing.visitor

import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import com.google.devtools.ksp.symbol.KSDeclaration
import com.google.devtools.ksp.symbol.KSModifierListOwner
import com.google.devtools.ksp.symbol.KSPropertySetter
import com.google.devtools.ksp.symbol.Modifier
import io.micronaut.core.annotation.AnnotationMetadata
import io.micronaut.core.util.ArrayUtils
Expand All @@ -39,6 +38,8 @@ internal abstract class AbstractKotlinMethodElement<T : KotlinNativeElement>(
visitorContext: KotlinVisitorContext
) : AbstractKotlinElement<T>(nativeType, annotationMetadataFactory, visitorContext), MethodElement {

abstract val declaration: KSDeclaration?
abstract val overridee: KSDeclaration?
abstract val internalDeclaringType: ClassElement
abstract val internalDeclaredTypeArguments: Map<String, ClassElement>
abstract val internalReturnType: ClassElement
Expand Down Expand Up @@ -104,19 +105,16 @@ internal abstract class AbstractKotlinMethodElement<T : KotlinNativeElement>(
}

override fun overrides(overridden: MethodElement): Boolean {
if (overridden !is AbstractKotlinElement<*>) {
if (this == overridden || overridden !is AbstractKotlinMethodElement<*>) {
return false
}
val nativeType = getNativeType().element
val overriddenNativeType = (overridden as AbstractKotlinElement<*>).nativeType.element
if (nativeType == overriddenNativeType) {
return false
} else if (nativeType is KSFunctionDeclaration) {
return overriddenNativeType == nativeType.findOverridee()
} else if (nativeType is KSPropertySetter && overriddenNativeType is KSPropertySetter) {
return overriddenNativeType.receiver == nativeType.receiver.findOverridee()
if (name != overridden.getName() || parameters.size != overridden.parameters.size) {
return false // Fast escape
}
if (nativeType == overridden.nativeType) {
return false // The same method
}
return false
return overridee == overridden.declaration
}

override fun hides(memberElement: MemberElement?) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.google.devtools.ksp.*
import com.google.devtools.ksp.symbol.*
import io.micronaut.inject.ast.*
import io.micronaut.inject.ast.annotation.ElementAnnotationMetadataFactory
import java.util.*

@OptIn(KspExperimental::class)
internal abstract class AbstractKotlinPropertyAccessorMethodElement<T : KotlinNativeElement>(
Expand All @@ -37,6 +36,14 @@ internal abstract class AbstractKotlinPropertyAccessorMethodElement<T : KotlinNa
visitorContext
), MethodElement {

override val declaration: KSDeclaration? by lazy {
accessor.receiver
}

override val overridee: KSDeclaration? by lazy {
accessor.receiver.findOverridee()
}

override val internalDeclaringType: ClassElement by lazy {
resolveDeclaringType(accessor.receiver, owningType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import io.micronaut.inject.ast.annotation.ElementAnnotationMetadataFactory
@OptIn(KspExperimental::class)
internal open class KotlinMethodElement(
owningType: ClassElement,
val declaration: KSFunctionDeclaration,
override val declaration: KSFunctionDeclaration,
private val presetParameters: List<ParameterElement>?,
elementAnnotationMetadataFactory: ElementAnnotationMetadataFactory,
visitorContext: KotlinVisitorContext
Expand All @@ -48,6 +48,10 @@ internal open class KotlinMethodElement(
visitorContext
)

override val overridee: KSDeclaration? by lazy {
declaration.findOverridee()
}

override val internalDeclaringType: ClassElement by lazy {
resolveDeclaringType(declaration, owningType)
}
Expand Down