Provide equals/hashCode based on original PSI to avoid potential PCEs

#KT-59445
This commit is contained in:
Vladimir Dolzhenko
2023-06-20 13:31:15 +02:00
committed by Space Team
parent 05652e7d8d
commit d0477a6a30
2 changed files with 15 additions and 0 deletions
@@ -15,10 +15,19 @@ internal class JavaElementPsiSourceWithSmartPointer<PSI : PsiElement>(
override val factory: JavaElementSourceFactory,
) : JavaElementPsiSource<PSI>() {
// is used only for the purposes of equals/hashCode to avoid underlying PCE
private val originalPsi: PSI = psi
override val psi: PSI
get() {
return pointer.element
?: error("Cannot restore a PsiElement from $pointer")
}
override fun equals(other: Any?): Boolean {
return if (other === this) true else other is JavaElementPsiSourceWithSmartPointer<*> && originalPsi == other.originalPsi
}
override fun hashCode(): Int = originalPsi.hashCode()
}
@@ -18,6 +18,12 @@ class JavaElementPsiSourceWithFixedPsi<PSI : PsiElement>(
override val factory: JavaElementSourceFactory
get() = JavaElementSourceFactory.getInstance(psi.project)
override fun equals(other: Any?): Boolean {
return if (other === this) true else other is JavaElementPsiSourceWithFixedPsi<*> && psi == other.psi
}
override fun hashCode(): Int = psi.hashCode()
override fun toString(): String {
return psi.toString()
}