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.
3 package org.jetbrains.kotlin.idea.caches.lightClasses.decompiledDeclarations
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
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> {
25 override val kotlinOrigin: KtDeclaration? get() = lightMemberOrigin.originalElement
27 override val isMangled: Boolean get() = checkIsMangled()
29 override fun hasModifierProperty(name: String): Boolean = funDelegate.hasModifierProperty(name)
31 override fun getReturnTypeElement(): PsiTypeElement? = funDelegate.returnTypeElement
33 override fun getContainingClass(): KtLightClass = funParent
35 override fun getTypeParameters(): Array<PsiTypeParameter> = funDelegate.typeParameters
37 override fun getThrowsList(): PsiReferenceList = funDelegate.throwsList
39 override fun getReturnType(): PsiType? = funDelegate.returnType
41 override fun hasTypeParameters(): Boolean = funDelegate.hasTypeParameters()
43 override fun getTypeParameterList(): PsiTypeParameterList? = funDelegate.typeParameterList
45 override fun isVarArgs(): Boolean = funDelegate.isVarArgs
47 override fun isConstructor(): Boolean = funDelegate.isConstructor
49 override fun getNameIdentifier(): PsiIdentifier? = funDelegate.nameIdentifier
51 override fun getName(): String = funDelegate.name
53 override fun getDocComment(): PsiDocComment? = funDelegate.docComment
55 override fun getModifierList(): PsiModifierList = funDelegate.modifierList
57 override fun getBody(): PsiCodeBlock? = null
59 override fun getDefaultValue(): PsiAnnotationMemberValue? = (funDelegate as? PsiAnnotationMethod)?.defaultValue
61 override fun isDeprecated(): Boolean = funDelegate.isDeprecated
63 override fun setName(name: String): PsiElement = funDelegate.setName(name)
65 override fun getParameterList(): PsiParameterList = funDelegate.parameterList
67 override fun getHierarchicalMethodSignature() = PsiSuperMethodImplUtil.getHierarchicalMethodSignature(this)
69 override fun findSuperMethodSignaturesIncludingStatic(checkAccess: Boolean): List<MethodSignatureBackedByPsiMethod> =
70 PsiSuperMethodImplUtil.findSuperMethodSignaturesIncludingStatic(this, checkAccess)
72 override fun findDeepestSuperMethod() = PsiSuperMethodImplUtil.findDeepestSuperMethod(this)
74 override fun findDeepestSuperMethods(): Array<out PsiMethod> = PsiSuperMethodImplUtil.findDeepestSuperMethods(this)
76 override fun findSuperMethods(): Array<out PsiMethod> = PsiSuperMethodImplUtil.findSuperMethods(this)
78 override fun findSuperMethods(checkAccess: Boolean): Array<out PsiMethod> =
79 PsiSuperMethodImplUtil.findSuperMethods(this, checkAccess)
81 override fun findSuperMethods(parentClass: PsiClass?): Array<out PsiMethod> =
82 PsiSuperMethodImplUtil.findSuperMethods(this, parentClass)
84 override fun getSignature(substitutor: PsiSubstitutor): MethodSignature =
85 MethodSignatureBackedByPsiMethod.create(this, substitutor)
87 override fun equals(other: Any?): Boolean = other is KtLightMethodForDecompiledDeclaration &&
89 funParent == other.funParent &&
90 funDelegate == other.funDelegate
92 override fun hashCode(): Int = name.hashCode()
94 override fun copy(): PsiElement = this
96 override fun clone(): Any = this
98 override fun toString(): String = "${this.javaClass.simpleName} of $funParent"
100 override val clsDelegate: PsiMethod = funDelegate
102 override fun isValid(): Boolean = parent.isValid
104 override fun getOriginalElement() = funDelegate
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