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

KSP2: Implement KSValueArgument.equals #2154

Merged
merged 1 commit into from
Oct 18, 2024
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 @@ -2,6 +2,7 @@ package com.google.devtools.ksp.impl.symbol.java

import com.google.devtools.ksp.common.IdKeyPair
import com.google.devtools.ksp.common.KSObjectCache
import com.google.devtools.ksp.impl.symbol.kotlin.AbstractKSValueArgumentImpl
import com.google.devtools.ksp.symbol.*

class KSValueArgumentLiteImpl private constructor(
Expand All @@ -10,7 +11,7 @@ class KSValueArgumentLiteImpl private constructor(
override val parent: KSNode,
override val origin: Origin,
override val location: Location
) : KSValueArgument {
) : AbstractKSValueArgumentImpl() {
companion object : KSObjectCache<IdKeyPair<KSName?, Any?>, KSValueArgumentLiteImpl>() {
fun getCached(
name: KSName?,
Expand All @@ -25,12 +26,4 @@ class KSValueArgumentLiteImpl private constructor(
override val isSpread: Boolean = false

override val annotations: Sequence<KSAnnotation> = emptySequence()

override fun toString(): String {
return "${name?.asString() ?: ""}:$value"
}

override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
return visitor.visitValueArgument(this, data)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class KSValueArgumentImpl private constructor(
private val namedAnnotationValue: KaNamedAnnotationValue,
override val parent: KSNode?,
override val origin: Origin
) : KSValueArgument, Deferrable {
) : AbstractKSValueArgumentImpl(), Deferrable {
companion object : KSObjectCache<KaNamedAnnotationValue, KSValueArgumentImpl>() {
fun getCached(namedAnnotationValue: KaNamedAnnotationValue, parent: KSNode?, origin: Origin) =
cache.getOrPut(namedAnnotationValue) {
Expand All @@ -48,16 +48,29 @@ class KSValueArgumentImpl private constructor(
namedAnnotationValue.expression.sourcePsi?.toLocation() ?: NonExistLocation
}

override fun defer(): Restorable {
val parent = if (parent is Deferrable) parent.defer() else null
return Restorable { getCached(namedAnnotationValue, parent?.restore(), origin) }
}
}

abstract class AbstractKSValueArgumentImpl : KSValueArgument {
override fun hashCode(): Int {
return name.hashCode() * 31 + value.hashCode()
}

override fun equals(other: Any?): Boolean {
if (other !is KSValueArgument)
return false

return other.name == this.name && other.value == this.value
}

override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
return visitor.visitValueArgument(this, data)
}

override fun toString(): String {
return "${name?.asString() ?: ""}:$value"
}

override fun defer(): Restorable {
val parent = if (parent is Deferrable) parent.defer() else null
return Restorable { getCached(namedAnnotationValue, parent?.restore(), origin) }
}
}
23 changes: 23 additions & 0 deletions kotlin-analysis-api/testData/annotationValue/annotationValue_kt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@
// TestNestedAnnotationDefaults: hij
// TestNestedAnnotationDefaults: def
// TestNestedAnnotationDefaults: hij
// @JavaAnnotationWithDefaultsInSource(value:def) == @JavaAnnotationWithDefaultsInSource(value:def): true
// @JavaAnnotationWithDefaultsInSource(value:def) == @KotlinAnnotationWithDefaultsInSource(value:hij): false
// @JavaAnnotationWithDefaultsInSource(value:def) == @JavaAnnotationWithDefaults(value:def): true
// @JavaAnnotationWithDefaultsInSource(value:def) == @KotlinAnnotationWithDefaults(value:hij): false
// @KotlinAnnotationWithDefaultsInSource(value:hij) == @JavaAnnotationWithDefaultsInSource(value:def): false
// @KotlinAnnotationWithDefaultsInSource(value:hij) == @KotlinAnnotationWithDefaultsInSource(value:hij): true
// @KotlinAnnotationWithDefaultsInSource(value:hij) == @JavaAnnotationWithDefaults(value:def): false
// @KotlinAnnotationWithDefaultsInSource(value:hij) == @KotlinAnnotationWithDefaults(value:hij): true
// @JavaAnnotationWithDefaults(value:def) == @JavaAnnotationWithDefaultsInSource(value:def): true
// @JavaAnnotationWithDefaults(value:def) == @KotlinAnnotationWithDefaultsInSource(value:hij): false
// @JavaAnnotationWithDefaults(value:def) == @JavaAnnotationWithDefaults(value:def): true
// @JavaAnnotationWithDefaults(value:def) == @KotlinAnnotationWithDefaults(value:hij): false
// @KotlinAnnotationWithDefaults(value:hij) == @JavaAnnotationWithDefaultsInSource(value:def): false
// @KotlinAnnotationWithDefaults(value:hij) == @KotlinAnnotationWithDefaultsInSource(value:hij): true
// @KotlinAnnotationWithDefaults(value:hij) == @JavaAnnotationWithDefaults(value:def): false
// @KotlinAnnotationWithDefaults(value:hij) == @KotlinAnnotationWithDefaults(value:hij): true
// END
// MODULE: module1
// FILE: placeholder.kt
Expand Down Expand Up @@ -169,3 +185,10 @@ annotation class KotlinAnnotationWithDefaultsInSource(val otherAnnotation: Other
@JavaAnnotationWithDefaults
@KotlinAnnotationWithDefaults
class TestNestedAnnotationDefaults {}

// FILE: TestValueArgEquals.java
@JavaAnnotationWithDefaultsInSource
@KotlinAnnotationWithDefaultsInSource
@JavaAnnotationWithDefaults
@KotlinAnnotationWithDefaults
class TestValueArgEquals {}
3 changes: 0 additions & 3 deletions kotlin-analysis-api/testData/parent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
// parent of topProp.getter(): topProp
// parent of Anno: Anno
// parent of Anno: @Anno
// parent of param:: @Anno
// parent of @Anno: topProp
// parent of topProp: File: a.kt
// parent of T: T
Expand All @@ -91,7 +90,6 @@
// parent of T: topFun
// parent of Anno: Anno
// parent of Anno: @Anno
// parent of param:: @Anno
// parent of @Anno: topFun
// parent of topFun: File: a.kt
// parent of Annotation: Annotation
Expand Down Expand Up @@ -125,7 +123,6 @@
// parent of ITF: topClass
// parent of Anno: Anno
// parent of Anno: @Anno
// parent of param:: @Anno
// parent of @Anno: topClass
// parent of topClass: File: a.kt
// parent of Int: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ class AnnotationArgumentProcessor : AbstractTestProcessor() {
}
}

resolver.getClassDeclarationByName("TestValueArgEquals")?.let { cls ->
cls.annotations.forEach { anno1 ->
val arg1 = (anno1.arguments.single().value as KSAnnotation).arguments.single()
cls.annotations.forEach { anno2 ->
val arg2 = (anno2.arguments.single().value as KSAnnotation).arguments.single()
val eq = arg1 == arg2
results.add("$anno1($arg1) == $anno2($arg2): $eq")
}
}
}

return emptyList()
}

Expand Down
Loading