diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/KotlinUClass.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/KotlinUClass.kt index 42d529e25bd..f4a3d55c8b6 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/KotlinUClass.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/KotlinUClass.kt @@ -20,7 +20,6 @@ import com.intellij.psi.* import org.jetbrains.kotlin.asJava.classes.KtLightClass import org.jetbrains.kotlin.asJava.classes.KtLightClassForScript import org.jetbrains.kotlin.asJava.elements.KtLightMethod -import org.jetbrains.kotlin.asJava.toLightMethods import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.utils.SmartList @@ -73,12 +72,10 @@ open class KotlinUClass private constructor( override fun getInitializers(): Array = super.getInitializers() override fun getMethods(): Array { - val primaryConstructor = ktClass?.primaryConstructor?.toLightMethods()?.firstOrNull() fun createUMethod(psiMethod: PsiMethod): UMethod { return if (psiMethod is KtLightMethod && - psiMethod.isConstructor && - (primaryConstructor == null || psiMethod == primaryConstructor)) { + psiMethod.isConstructor) { KotlinPrimaryConstructorUMethod(ktClass, psiMethod, this) } else { getLanguagePlugin().convert(psiMethod, this) @@ -111,22 +108,27 @@ class KotlinPrimaryConstructorUMethod( override val uastParent: UElement? ): KotlinUMethod(psi, uastParent) { override val uastBody: UExpression? by lz { - val superTypeCallEntry = ktClass?.superTypeListEntries?.firstIsInstanceOrNull() - val initializers = ktClass?.getAnonymousInitializers()?.takeIf { it.isNotEmpty() } - if (superTypeCallEntry != null || initializers != null) - KotlinUBlockExpression.KotlinLazyUBlockExpression(this) { uastParent -> - SmartList().apply { - superTypeCallEntry?.let { - add(KotlinUFunctionCallExpression(it, uastParent)) - } - val languagePlugin = uastParent.getLanguagePlugin() - initializers?.forEach { - addIfNotNull(languagePlugin.convertOpt(it.body, uastParent)) - } + val delegationCall: KtCallElement? = psi.kotlinOrigin.let { + when (it) { + is KtPrimaryConstructor, is KtObjectDeclaration -> + ktClass?.superTypeListEntries?.firstIsInstanceOrNull() + is KtSecondaryConstructor -> it.getDelegationCall() + else -> null + } + } + val initializers = ktClass?.getAnonymousInitializers().orEmpty() + if (delegationCall == null && initializers.isEmpty()) return@lz null + KotlinUBlockExpression.KotlinLazyUBlockExpression(this) { uastParent -> + SmartList().apply { + delegationCall?.let { + add(KotlinUFunctionCallExpression(it, uastParent)) + } + val languagePlugin = uastParent.getLanguagePlugin() + initializers.forEach { + addIfNotNull(languagePlugin.convertOpt(it.body, uastParent)) } } - else - null + } } } diff --git a/plugins/uast-kotlin/testData/SuperCalls.log.txt b/plugins/uast-kotlin/testData/SuperCalls.log.txt index 341e9a94b30..cca4e2911e5 100644 --- a/plugins/uast-kotlin/testData/SuperCalls.log.txt +++ b/plugins/uast-kotlin/testData/SuperCalls.log.txt @@ -23,6 +23,15 @@ UFile (package = ) UAnnotationMethod (name = A) UParameter (name = i) UAnnotation (fqName = null) + UBlockExpression + UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 1)) + UIdentifier (Identifier (this)) + USimpleNameReferenceExpression (identifier = ) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = i) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0)) + UIdentifier (Identifier (toString)) + USimpleNameReferenceExpression (identifier = toString) UClass (name = B) UAnnotationMethod (name = B) UParameter (name = param) @@ -46,9 +55,19 @@ UFile (package = ) UAnnotationMethod (name = C) UParameter (name = p) UAnnotation (fqName = org.jetbrains.annotations.NotNull) + UBlockExpression + UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 1)) + UIdentifier (Identifier (super)) + USimpleNameReferenceExpression (identifier = ) + USimpleNameReferenceExpression (identifier = p) UAnnotationMethod (name = C) UParameter (name = i) UAnnotation (fqName = null) + UBlockExpression + UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 1)) + UIdentifier (Identifier (super)) + USimpleNameReferenceExpression (identifier = ) + USimpleNameReferenceExpression (identifier = i) UClass (name = O) UField (name = INSTANCE) UAnnotation (fqName = null) diff --git a/plugins/uast-kotlin/testData/SuperCalls.render.txt b/plugins/uast-kotlin/testData/SuperCalls.render.txt index 8fb878a4f49..9b291b2ea8a 100644 --- a/plugins/uast-kotlin/testData/SuperCalls.render.txt +++ b/plugins/uast-kotlin/testData/SuperCalls.render.txt @@ -1,6 +1,6 @@ public final class SuperCallsKt { - private static final var anon: A = anonymous object : A ("textForAnon"){ - fun bar(){} + private static final var anon: A = anonymous object : A("textForAnon") { + fun bar() {} } public static final fun getAnon() : A = UastEmptyExpression } @@ -11,7 +11,9 @@ public class A { } public final fun getStr() : java.lang.String = UastEmptyExpression public fun A(str: java.lang.String) = UastEmptyExpression - public fun A(i: int) = UastEmptyExpression + public fun A(i: int) { + (i.toString()) + } } public final class B : A { @@ -24,8 +26,12 @@ public final class C : A { public fun foo(a: long) : void { super.foo(a) } - public fun C(p: java.lang.String) = UastEmptyExpression - public fun C(i: int) = UastEmptyExpression + public fun C(p: java.lang.String) { + (p) + } + public fun C(i: int) { + (i) + } } public final class O : A {