From d976c1d5946f4553325174b027ce871776a83e4e Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 15 Sep 2017 18:12:26 +0200 Subject: [PATCH] Correctly convert KtProperty with no backing field to UAST --- .../uast/kotlin/KotlinUastLanguagePlugin.kt | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt index 481744cd37a..a7c5d92e4ed 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt @@ -126,7 +126,7 @@ class KotlinUastLanguagePlugin : UastLanguagePlugin { return UastLanguagePlugin.ResolvedConstructor(uExpression, method, containingClass) } - private fun convertDeclaration(element: PsiElement, + internal fun convertDeclaration(element: PsiElement, givenParent: UElement?, requiredType: Class?): UElement? { fun

build(ctor: (P, UElement?) -> UElement): () -> UElement? { @@ -180,14 +180,12 @@ class KotlinUastLanguagePlugin : UastLanguagePlugin { val lightMethod = LightClassUtil.getLightClassAccessorMethod(original) ?: return null convertDeclaration(lightMethod, givenParent, requiredType) } - is KtProperty -> el { + is KtProperty -> el { if (original.isLocal) { KotlinConverter.convertPsiElement(element, givenParent, requiredType) } else { - LightClassUtil.getLightClassBackingField(original)?.let { - KotlinUField(it, givenParent) - } + convertNonLocalProperty(original, givenParent, requiredType) } } is KtParameter -> el { @@ -236,6 +234,17 @@ internal inline fun Class?.expr(f: () return if (this == null || isAssignableFrom(ActualT::class.java)) f() else null } +private fun convertNonLocalProperty(property: KtProperty, + givenParent: UElement?, + requiredType: Class?): UElement? { + val methods = LightClassUtil.getLightClassPropertyMethods(property) + return methods.backingField?.let { backingField -> + KotlinUField(backingField, givenParent) + } ?: methods.getter?.let { getter -> + KotlinUastLanguagePlugin().convertDeclaration(getter, givenParent, requiredType) + } +} + internal object KotlinConverter { internal tailrec fun unwrapElements(element: PsiElement?): PsiElement? = when (element) { is KtValueArgumentList -> unwrapElements(element.parent) @@ -265,9 +274,20 @@ internal object KotlinConverter { } is KtClassBody -> el(build(KotlinUExpressionList.Companion::createClassBody)) is KtCatchClause -> el(build(::KotlinUCatchClause)) - is KtVariableDeclaration -> el { - convertVariablesDeclaration(element, givenParent).declarations.singleOrNull() - } + is KtVariableDeclaration -> + if (element is KtProperty && !element.isLocal) { + el { + LightClassUtil.getLightClassBackingField(element)?.let { + KotlinUField(it, givenParent) + } + } + } + else { + el { + convertVariablesDeclaration(element, givenParent).declarations.singleOrNull() + } + } + is KtExpression -> KotlinConverter.convertExpression(element, givenParent, requiredType) is KtLambdaArgument -> KotlinConverter.convertExpression(element.getLambdaExpression(), givenParent, requiredType) is KtLightAnnotationForSourceEntry.LightExpressionValue<*> -> {