[kotlin-analysis] light elements for decompiled declaration should return original...
[idea/community.git] / plugins / kotlin / analysis / src / org / jetbrains / kotlin / idea / caches / lightClasses / decompiledDeclarations / KtLightMethodForDecompiledDeclaration.kt
1 // Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2
3 package org.jetbrains.kotlin.idea.caches.lightClasses.decompiledDeclarations
4
5 import com.intellij.psi.*
6 import com.intellij.psi.impl.PsiSuperMethodImplUtil
7 import com.intellij.psi.javadoc.PsiDocComment
8 import com.intellij.psi.util.MethodSignature
9 import com.intellij.psi.util.MethodSignatureBackedByPsiMethod
10 import org.jetbrains.kotlin.asJava.classes.KtLightClass
11 import org.jetbrains.kotlin.asJava.elements.KtLightElementBase
12 import org.jetbrains.kotlin.asJava.elements.KtLightMember
13 import org.jetbrains.kotlin.asJava.elements.KtLightMethod
14 import org.jetbrains.kotlin.asJava.propertyNameByAccessor
15 import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper
16 import org.jetbrains.kotlin.idea.caches.lightClasses.LightMemberOriginForCompiledMethod
17 import org.jetbrains.kotlin.psi.KtDeclaration
18
19 class KtLightMethodForDecompiledDeclaration(
20     private val funDelegate: PsiMethod,
21     private val funParent: KtLightClass,
22     override val lightMemberOrigin: LightMemberOriginForCompiledMethod,
23 ) : KtLightElementBase(funParent), PsiMethod, KtLightMethod, KtLightMember<PsiMethod> {
24
25     override val kotlinOrigin: KtDeclaration? get() = lightMemberOrigin.originalElement
26
27     override val isMangled: Boolean get() = checkIsMangled()
28
29     override fun hasModifierProperty(name: String): Boolean = funDelegate.hasModifierProperty(name)
30
31     override fun getReturnTypeElement(): PsiTypeElement? = funDelegate.returnTypeElement
32
33     override fun getContainingClass(): KtLightClass = funParent
34
35     override fun getTypeParameters(): Array<PsiTypeParameter> = funDelegate.typeParameters
36
37     override fun getThrowsList(): PsiReferenceList = funDelegate.throwsList
38
39     override fun getReturnType(): PsiType? = funDelegate.returnType
40
41     override fun hasTypeParameters(): Boolean = funDelegate.hasTypeParameters()
42
43     override fun getTypeParameterList(): PsiTypeParameterList? = funDelegate.typeParameterList
44
45     override fun isVarArgs(): Boolean = funDelegate.isVarArgs
46
47     override fun isConstructor(): Boolean = funDelegate.isConstructor
48
49     override fun getNameIdentifier(): PsiIdentifier? = funDelegate.nameIdentifier
50
51     override fun getName(): String = funDelegate.name
52
53     override fun getDocComment(): PsiDocComment? = funDelegate.docComment
54
55     override fun getModifierList(): PsiModifierList = funDelegate.modifierList
56
57     override fun getBody(): PsiCodeBlock? = null
58
59     override fun getDefaultValue(): PsiAnnotationMemberValue? = (funDelegate as? PsiAnnotationMethod)?.defaultValue
60
61     override fun isDeprecated(): Boolean = funDelegate.isDeprecated
62
63     override fun setName(name: String): PsiElement = funDelegate.setName(name)
64
65     override fun getParameterList(): PsiParameterList = funDelegate.parameterList
66
67     override fun getHierarchicalMethodSignature() = PsiSuperMethodImplUtil.getHierarchicalMethodSignature(this)
68
69     override fun findSuperMethodSignaturesIncludingStatic(checkAccess: Boolean): List<MethodSignatureBackedByPsiMethod> =
70         PsiSuperMethodImplUtil.findSuperMethodSignaturesIncludingStatic(this, checkAccess)
71
72     override fun findDeepestSuperMethod() = PsiSuperMethodImplUtil.findDeepestSuperMethod(this)
73
74     override fun findDeepestSuperMethods(): Array<out PsiMethod> = PsiSuperMethodImplUtil.findDeepestSuperMethods(this)
75
76     override fun findSuperMethods(): Array<out PsiMethod> = PsiSuperMethodImplUtil.findSuperMethods(this)
77
78     override fun findSuperMethods(checkAccess: Boolean): Array<out PsiMethod> =
79         PsiSuperMethodImplUtil.findSuperMethods(this, checkAccess)
80
81     override fun findSuperMethods(parentClass: PsiClass?): Array<out PsiMethod> =
82         PsiSuperMethodImplUtil.findSuperMethods(this, parentClass)
83
84     override fun getSignature(substitutor: PsiSubstitutor): MethodSignature =
85         MethodSignatureBackedByPsiMethod.create(this, substitutor)
86
87     override fun equals(other: Any?): Boolean = other is KtLightMethodForDecompiledDeclaration &&
88             name == other.name &&
89             funParent == other.funParent &&
90             funDelegate == other.funDelegate
91
92     override fun hashCode(): Int = name.hashCode()
93
94     override fun copy(): PsiElement = this
95
96     override fun clone(): Any = this
97
98     override fun toString(): String = "${this.javaClass.simpleName} of $funParent"
99
100     override val clsDelegate: PsiMethod = funDelegate
101
102     override fun isValid(): Boolean = parent.isValid
103
104     override fun getOriginalElement() = funDelegate
105 }
106
107 private fun KtLightMethod.checkIsMangled(): Boolean {
108     val demangledName = KotlinTypeMapper.InternalNameMapper.demangleInternalName(name) ?: return false
109     val originalName = propertyNameByAccessor(demangledName, this) ?: demangledName
110     return originalName == kotlinOrigin?.name
111 }