[lc] decompiled: extract common part to KotlinDeclarationInCompiledFileSearcher
^KT-53934
This commit is contained in:
+1
-32
@@ -12,12 +12,9 @@ import org.jetbrains.kotlin.analysis.decompiler.psi.file.KtClsFile
|
||||
import org.jetbrains.kotlin.analysis.decompiler.psi.text.BySignatureIndexer
|
||||
import org.jetbrains.kotlin.analysis.decompiler.psi.text.ClassNameAndSignature
|
||||
import org.jetbrains.kotlin.analysis.decompiler.psi.text.relativeClassName
|
||||
import org.jetbrains.kotlin.asJava.syntheticAccessors
|
||||
import org.jetbrains.kotlin.load.kotlin.MemberSignature
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.allConstructors
|
||||
|
||||
class KotlinDeclarationInCompiledFileSearcherFE10Impl : KotlinDeclarationInCompiledFileSearcher() {
|
||||
override fun findDeclarationInCompiledFile(file: KtClsFile, member: PsiMember, signature: MemberSignature): KtDeclaration? {
|
||||
@@ -26,7 +23,7 @@ class KotlinDeclarationInCompiledFileSearcherFE10Impl : KotlinDeclarationInCompi
|
||||
|
||||
val memberName = member.name
|
||||
if (memberName != null && !file.isContentsLoaded && file.hasDeclarationWithKey(BySignatureIndexer, key)) {
|
||||
findByStub(file, relativeClassName, member, memberName)?.let { return it }
|
||||
findByStubs(file, relativeClassName, member, memberName)?.let { return it }
|
||||
}
|
||||
|
||||
val declaration = file.getDeclaration(BySignatureIndexer, key) ?: return null
|
||||
@@ -37,31 +34,3 @@ class KotlinDeclarationInCompiledFileSearcherFE10Impl : KotlinDeclarationInCompi
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun findByStub(
|
||||
file: KtClsFile,
|
||||
relativeClassName: List<Name>,
|
||||
member: PsiMember,
|
||||
memberName: String,
|
||||
): KtDeclaration? {
|
||||
val topClassOrObject = file.declarations.singleOrNull() as? KtClassOrObject
|
||||
val container: KtClassOrObject = if (relativeClassName.isEmpty())
|
||||
topClassOrObject
|
||||
else {
|
||||
relativeClassName.fold(topClassOrObject) { classOrObject, name ->
|
||||
classOrObject?.declarations?.singleOrNull { it.name == name.asString() } as? KtClassOrObject
|
||||
}
|
||||
} ?: return null
|
||||
|
||||
return if (member is PsiMethod && member.isConstructor) {
|
||||
container.takeIf { it.name == memberName }?.allConstructors?.singleOrNull()
|
||||
} else {
|
||||
val declarations = container.declarations
|
||||
val names: Collection<String> = if (member is PsiMethod)
|
||||
member.syntheticAccessors.map(Name::asString) + memberName
|
||||
else
|
||||
listOf(memberName)
|
||||
|
||||
declarations.singleOrNull { declaration -> declaration.name in names }
|
||||
}
|
||||
}
|
||||
|
||||
+54
-1
@@ -1,18 +1,71 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.analysis.decompiled.light.classes.origin
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.psi.PsiField
|
||||
import com.intellij.psi.PsiMember
|
||||
import com.intellij.psi.PsiMethod
|
||||
import org.jetbrains.kotlin.analysis.decompiler.psi.file.KtClsFile
|
||||
import org.jetbrains.kotlin.asJava.syntheticAccessors
|
||||
import org.jetbrains.kotlin.load.kotlin.MemberSignature
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.allConstructors
|
||||
import org.jetbrains.kotlin.type.MapPsiToAsmDesc
|
||||
|
||||
abstract class KotlinDeclarationInCompiledFileSearcher {
|
||||
abstract fun findDeclarationInCompiledFile(file: KtClsFile, member: PsiMember, signature: MemberSignature): KtDeclaration?
|
||||
fun findDeclarationInCompiledFile(file: KtClsFile, member: PsiMember): KtDeclaration? {
|
||||
val signature = when (member) {
|
||||
is PsiField -> {
|
||||
val desc = MapPsiToAsmDesc.typeDesc(member.type)
|
||||
MemberSignature.fromFieldNameAndDesc(member.name, desc)
|
||||
}
|
||||
|
||||
is PsiMethod -> {
|
||||
val desc = MapPsiToAsmDesc.methodDesc(member)
|
||||
val name = if (member.isConstructor) "<init>" else member.name
|
||||
MemberSignature.fromMethodNameAndDesc(name, desc)
|
||||
}
|
||||
|
||||
else -> null
|
||||
} ?: return null
|
||||
|
||||
return findDeclarationInCompiledFile(file, member, signature)
|
||||
}
|
||||
|
||||
protected fun findByStubs(
|
||||
file: KtClsFile,
|
||||
relativeClassName: List<Name>,
|
||||
member: PsiMember,
|
||||
memberName: String,
|
||||
): KtDeclaration? {
|
||||
val topClassOrObject = file.declarations.singleOrNull() as? KtClassOrObject
|
||||
val container: KtClassOrObject = if (relativeClassName.isEmpty())
|
||||
topClassOrObject
|
||||
else {
|
||||
relativeClassName.fold(topClassOrObject) { classOrObject, name ->
|
||||
classOrObject?.declarations?.singleOrNull { it.name == name.asString() } as? KtClassOrObject
|
||||
}
|
||||
} ?: return null
|
||||
|
||||
return if (member is PsiMethod && member.isConstructor) {
|
||||
container.takeIf { it.name == memberName }?.allConstructors?.singleOrNull()
|
||||
} else {
|
||||
val declarations = container.declarations
|
||||
val names: Collection<String> = if (member is PsiMethod)
|
||||
member.syntheticAccessors.map(Name::asString) + memberName
|
||||
else
|
||||
listOf(memberName)
|
||||
|
||||
declarations.singleOrNull { declaration -> declaration.name in names }
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getInstance(): KotlinDeclarationInCompiledFileSearcher =
|
||||
|
||||
+2
-9
@@ -13,10 +13,8 @@ import com.intellij.psi.PsiMethod
|
||||
import org.jetbrains.kotlin.analysis.decompiler.psi.file.KtClsFile
|
||||
import org.jetbrains.kotlin.asJava.builder.LightMemberOrigin
|
||||
import org.jetbrains.kotlin.asJava.classes.lazyPub
|
||||
import org.jetbrains.kotlin.load.kotlin.MemberSignature
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOriginKind
|
||||
import org.jetbrains.kotlin.type.MapPsiToAsmDesc
|
||||
|
||||
interface LightMemberOriginForCompiledElement<T : PsiMember> : LightMemberOrigin {
|
||||
val member: T
|
||||
@@ -49,9 +47,7 @@ data class LightMemberOriginForCompiledField(val psiField: PsiField, val file: K
|
||||
}
|
||||
|
||||
override val originalElement: KtDeclaration? by lazyPub {
|
||||
val desc = MapPsiToAsmDesc.typeDesc(psiField.type)
|
||||
val signature = MemberSignature.fromFieldNameAndDesc(psiField.name, desc)
|
||||
KotlinDeclarationInCompiledFileSearcher.getInstance().findDeclarationInCompiledFile(file, psiField, signature)
|
||||
KotlinDeclarationInCompiledFileSearcher.getInstance().findDeclarationInCompiledFile(file, psiField)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,9 +67,6 @@ data class LightMemberOriginForCompiledMethod(val psiMethod: PsiMethod, val file
|
||||
}
|
||||
|
||||
override val originalElement: KtDeclaration? by lazyPub {
|
||||
val desc = MapPsiToAsmDesc.methodDesc(psiMethod)
|
||||
val name = if (psiMethod.isConstructor) "<init>" else psiMethod.name
|
||||
val signature = MemberSignature.fromMethodNameAndDesc(name, desc)
|
||||
KotlinDeclarationInCompiledFileSearcher.getInstance().findDeclarationInCompiledFile(file, psiMethod, signature)
|
||||
KotlinDeclarationInCompiledFileSearcher.getInstance().findDeclarationInCompiledFile(file, psiMethod)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user