[DLC] add missing visitor methods

Some of our light classes have no correct parent (KT-56882),
so I temporarily disabled
AbstractSymbolLightClassesParentingTestBase for such cases
(previously decompiled light classes were not tested at all)

^KT-56613
^KT-56882
This commit is contained in:
Dmitrii Gridin
2023-02-22 17:14:03 +01:00
committed by Space Team
parent 0bd193ccba
commit 95455c9870
6 changed files with 69 additions and 18 deletions
@@ -5,8 +5,10 @@
package org.jetbrains.kotlin.analysis.decompiled.light.classes package org.jetbrains.kotlin.analysis.decompiled.light.classes
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiClass import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.asJava.classes.KtExtensibleLightClass import org.jetbrains.kotlin.asJava.classes.KtExtensibleLightClass
import org.jetbrains.kotlin.asJava.elements.KtLightElementBase import org.jetbrains.kotlin.asJava.elements.KtLightElementBase
import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.KtClassOrObject
@@ -15,4 +17,12 @@ abstract class KtLightClassForDecompiledDeclarationBase(
val clsDelegate: PsiClass, val clsDelegate: PsiClass,
clsParent: PsiElement, clsParent: PsiElement,
final override val kotlinOrigin: KtClassOrObject? final override val kotlinOrigin: KtClassOrObject?
) : KtLightElementBase(clsParent), PsiClass, KtExtensibleLightClass ) : KtLightElementBase(clsParent), PsiClass, KtExtensibleLightClass {
override fun accept(visitor: PsiElementVisitor) {
if (visitor is JavaElementVisitor) {
visitor.visitClass(this)
} else {
visitor.visitElement(this)
}
}
}
@@ -34,4 +34,12 @@ internal class KtLightEnumClassForDecompiledDeclaration(
override fun equals(other: Any?): Boolean = this === other || other is KtLightEnumClassForDecompiledDeclaration && super.equals(other) override fun equals(other: Any?): Boolean = this === other || other is KtLightEnumClassForDecompiledDeclaration && super.equals(other)
override fun hashCode(): Int = super.hashCode() override fun hashCode(): Int = super.hashCode()
}
override fun accept(visitor: PsiElementVisitor) {
if (visitor is JavaElementVisitor) {
visitor.visitEnumConstantInitializer(this)
} else {
visitor.visitElement(this)
}
}
}
@@ -44,4 +44,12 @@ internal class KtLightEnumEntryForDecompiledDeclaration(
fldDelegate == other.fldDelegate fldDelegate == other.fldDelegate
override fun hashCode(): Int = super.hashCode() override fun hashCode(): Int = super.hashCode()
}
override fun accept(visitor: PsiElementVisitor) {
if (visitor is JavaElementVisitor) {
visitor.visitEnumConstant(this)
} else {
visitor.visitElement(this)
}
}
}
@@ -77,4 +77,12 @@ open class KtLightFieldForDecompiledDeclaration(
another is KtLightFieldForDecompiledDeclaration && fldDelegate.isEquivalentTo(another.fldDelegate) || another is KtLightFieldForDecompiledDeclaration && fldDelegate.isEquivalentTo(another.fldDelegate) ||
fldDelegate.isEquivalentTo(another) fldDelegate.isEquivalentTo(another)
} }
}
override fun accept(visitor: PsiElementVisitor) {
if (visitor is JavaElementVisitor) {
visitor.visitField(this)
} else {
visitor.visitElement(this)
}
}
}
@@ -109,10 +109,18 @@ class KtLightMethodForDecompiledDeclaration(
another is KtLightMethodForDecompiledDeclaration && funDelegate.isEquivalentTo(another.funDelegate) || another is KtLightMethodForDecompiledDeclaration && funDelegate.isEquivalentTo(another.funDelegate) ||
funDelegate.isEquivalentTo(another) funDelegate.isEquivalentTo(another)
} }
override fun accept(visitor: PsiElementVisitor) {
if (visitor is JavaElementVisitor) {
visitor.visitMethod(this)
} else {
visitor.visitElement(this)
}
}
} }
private fun KtLightMethod.checkIsMangled(): Boolean { private fun KtLightMethod.checkIsMangled(): Boolean {
val demangledName = demangleInternalName(name) ?: return false val demangledName = demangleInternalName(name) ?: return false
val originalName = propertyNameByAccessor(demangledName, this) ?: demangledName val originalName = propertyNameByAccessor(demangledName, this) ?: demangledName
return originalName == kotlinOrigin?.name return originalName == kotlinOrigin?.name
} }
@@ -40,15 +40,20 @@ open class AbstractSymbolLightClassesParentingTestBase(
protected fun createLightElementsVisitor(directives: RegisteredDirectives, assertions: AssertionsService): JavaElementVisitor { protected fun createLightElementsVisitor(directives: RegisteredDirectives, assertions: AssertionsService): JavaElementVisitor {
Assume.assumeFalse("The test is not supported", Directives.IGNORE_PARENTING_CHECK in directives) Assume.assumeFalse("The test is not supported", Directives.IGNORE_PARENTING_CHECK in directives)
// drop after KT-56882
val ignoreDecompiledClasses = stopIfCompilationErrorDirectivePresent
return object : JavaElementVisitor() { return object : JavaElementVisitor() {
private val declarationStack = ArrayDeque<PsiElement>() private val declarationStack = ArrayDeque<PsiElement>()
private fun <T : PsiElement> checkParentAndVisitChildren( private fun <T : PsiElement> checkParentAndVisitChildren(
declaration: T?, declaration: T?,
notCheckItself: Boolean = false,
action: T.(visitor: JavaElementVisitor) -> Unit = {}, action: T.(visitor: JavaElementVisitor) -> Unit = {},
) { ) {
if (declaration == null) return if (declaration == null) return
checkDeclarationParent(declaration) if (!notCheckItself) {
checkDeclarationParent(declaration)
}
declarationStack.addLast(declaration) declarationStack.addLast(declaration)
try { try {
@@ -72,19 +77,19 @@ open class AbstractSymbolLightClassesParentingTestBase(
} }
override fun visitModifierList(list: PsiModifierList?) { override fun visitModifierList(list: PsiModifierList?) {
checkParentAndVisitChildren(list) { visitor -> checkParentAndVisitChildren(list, notCheckItself = ignoreDecompiledClasses) { visitor ->
annotations.forEach { it.accept(visitor) } annotations.forEach { it.accept(visitor) }
} }
} }
override fun visitParameterList(list: PsiParameterList?) { override fun visitParameterList(list: PsiParameterList?) {
checkParentAndVisitChildren(list) { visitor -> checkParentAndVisitChildren(list, notCheckItself = ignoreDecompiledClasses) { visitor ->
parameters.forEach { it.accept(visitor) } parameters.forEach { it.accept(visitor) }
} }
} }
override fun visitTypeParameterList(list: PsiTypeParameterList?) { override fun visitTypeParameterList(list: PsiTypeParameterList?) {
checkParentAndVisitChildren(list) { visitor -> checkParentAndVisitChildren(list, notCheckItself = ignoreDecompiledClasses) { visitor ->
typeParameters.forEach { it.accept(visitor) } typeParameters.forEach { it.accept(visitor) }
} }
} }
@@ -108,6 +113,8 @@ open class AbstractSymbolLightClassesParentingTestBase(
} }
override fun visitMethod(method: PsiMethod?) { override fun visitMethod(method: PsiMethod?) {
if (method is SyntheticElement) return
checkParentAndVisitChildren(method) { visitor -> checkParentAndVisitChildren(method) { visitor ->
annotations.forEach { it.accept(visitor) } annotations.forEach { it.accept(visitor) }
@@ -150,17 +157,19 @@ open class AbstractSymbolLightClassesParentingTestBase(
(lastDeclaration as PsiModifierList).parent (lastDeclaration as PsiModifierList).parent
} as PsiModifierListOwner } as PsiModifierListOwner
when (psiModifierListOwner) { if (!ignoreDecompiledClasses) {
is PsiClass, when (psiModifierListOwner) {
is PsiParameter -> is PsiClass,
assertions.assertTrue(owner is SymbolLightClassModifierList<*>) is PsiParameter ->
assertions.assertTrue(owner is SymbolLightClassModifierList<*>)
is PsiField, is PsiField,
is PsiMethod -> is PsiMethod ->
assertions.assertTrue(owner is SymbolLightMemberModifierList<*>) assertions.assertTrue(owner is SymbolLightMemberModifierList<*>)
else -> else ->
throw IllegalStateException("Unexpected annotation owner kind: ${lastDeclaration::class.java}") throw IllegalStateException("Unexpected annotation owner kind: ${lastDeclaration::class.java}")
}
} }
val modifierList = psiModifierListOwner.modifierList!! val modifierList = psiModifierListOwner.modifierList!!