From 703d053157ca6eece3938141b5fadc83d9edc261 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 6 Oct 2017 17:21:07 +0200 Subject: [PATCH] Additional fixes for consistent parent conversion --- .../uast/kotlin/KotlinAbstractUElement.kt | 21 +++++++++- .../uast/kotlin/declarations/KotlinUClass.kt | 20 +++++----- .../UnexpectedContainerException.log.txt | 39 +++++++++++++++++++ .../UnexpectedContainerException.render.txt | 20 ++++++++++ .../tests/SimpleKotlinRenderLogTest.kt | 2 + 5 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 plugins/uast-kotlin/testData/UnexpectedContainerException.log.txt create mode 100644 plugins/uast-kotlin/testData/UnexpectedContainerException.render.txt diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt index 35e335a6966..7a94749a1ea 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt @@ -21,9 +21,11 @@ import org.jetbrains.kotlin.asJava.toLightGetter import org.jetbrains.kotlin.asJava.toLightSetter import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.uast.* import org.jetbrains.uast.kotlin.expressions.KotlinUElvisExpression +import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable abstract class KotlinAbstractUElement(private val givenParent: UElement?) : UElement { @@ -50,8 +52,15 @@ abstract class KotlinAbstractUElement(private val givenParent: UElement?) : UEle ?: parent } } + if (psi is UastKotlinPsiVariable && parent != null) { + parent = parent.parent + } - if ((parent is KtStringTemplateExpression && parent.entries.size == 1) || parent is KtWhenConditionWithExpression) { + if (parent is KtStringTemplateEntryWithExpression) { + parent = parent.parent + } + if ((parent is KtStringTemplateExpression && parent.entries.size == 1) || + parent is KtWhenConditionWithExpression) { parent = parent.parent } @@ -88,6 +97,16 @@ fun doConvertParent(element: UElement, parent: PsiElement?): UElement? { } } + if (parent is KtClassInitializer) { + val containingClass = parent.containingClassOrObject + if (containingClass != null) { + val containingUClass = KotlinUastLanguagePlugin().convertElementWithParent(containingClass, null) as KotlinUClass? + containingUClass?.methods?.filterIsInstance()?.firstOrNull()?.let { + return it.uastBody + } + } + } + val result = KotlinUastLanguagePlugin().convertElementWithParent(parentUnwrapped, null) if (result is UEnumConstant && element is UDeclaration) { 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 3e9e0367035..770cf815116 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 @@ -100,17 +100,17 @@ open class KotlinUClass private constructor( else -> KotlinUClass(psi, containingElement) } } +} - class KotlinPrimaryConstructorUMethod( - private val ktClass: KtClassOrObject?, - override val psi: KtLightMethod, - override val uastParent: UElement? - ): KotlinUMethod(psi, uastParent) { - override val uastBody: UExpression? by lz { - ktClass?.getAnonymousInitializers() - ?.takeIf { it.isNotEmpty() } - ?.let { KotlinUBlockExpression.create(it, this) } - } +class KotlinPrimaryConstructorUMethod( + private val ktClass: KtClassOrObject?, + override val psi: KtLightMethod, + override val uastParent: UElement? +): KotlinUMethod(psi, uastParent) { + override val uastBody: UExpression? by lz { + ktClass?.getAnonymousInitializers() + ?.takeIf { it.isNotEmpty() } + ?.let { KotlinUBlockExpression.create(it, this) } } } diff --git a/plugins/uast-kotlin/testData/UnexpectedContainerException.log.txt b/plugins/uast-kotlin/testData/UnexpectedContainerException.log.txt new file mode 100644 index 00000000000..52906b55fa1 --- /dev/null +++ b/plugins/uast-kotlin/testData/UnexpectedContainerException.log.txt @@ -0,0 +1,39 @@ +UFile (package = ) + UClass (name = Callback) + UAnnotationMethod (name = onError) + UParameter (name = throwable) + UAnnotation (fqName = org.jetbrains.annotations.NotNull) + UClass (name = Model) + UAnnotationMethod (name = crashMe) + UParameter (name = clazz) + UAnnotation (fqName = org.jetbrains.annotations.NotNull) + UParameter (name = factory) + UAnnotation (fqName = org.jetbrains.annotations.NotNull) + UBlockExpression + UThrowExpression + UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 0)) + UIdentifier (Identifier (UnsupportedOperationException)) + USimpleNameReferenceExpression (identifier = ) + UAnnotationMethod (name = Model) + UBlockExpression + UBlockExpression + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (crashMe)) + USimpleNameReferenceExpression (identifier = crashMe) + UQualifiedReferenceExpression + UClassLiteralExpression + USimpleNameReferenceExpression (identifier = java) + ULambdaExpression + UBlockExpression + UObjectLiteralExpression + UClass (name = null) + UAnnotationMethod (name = onError) + UParameter (name = throwable) + UAnnotation (fqName = org.jetbrains.annotations.NotNull) + UBlockExpression + UThrowExpression + UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 1)) + UIdentifier (Identifier (UnsupportedOperationException)) + USimpleNameReferenceExpression (identifier = ) + ULiteralExpression (value = "") + UAnnotationMethod (name = Model$1$1) diff --git a/plugins/uast-kotlin/testData/UnexpectedContainerException.render.txt b/plugins/uast-kotlin/testData/UnexpectedContainerException.render.txt new file mode 100644 index 00000000000..7c405437c1d --- /dev/null +++ b/plugins/uast-kotlin/testData/UnexpectedContainerException.render.txt @@ -0,0 +1,20 @@ +public abstract interface Callback { + public abstract fun onError(throwable: java.lang.Throwable) : void = UastEmptyExpression +} + +public final class Model { + public final fun crashMe(clazz: java.lang.Class, factory: kotlin.jvm.functions.Function0) : void { + throw () + } + public fun Model() { + { + crashMe(Callback.java, { + anonymous object : Callback { + override fun onError(throwable: Throwable) { + throw UnsupportedOperationException("") + } + } + }) + } + } +} diff --git a/plugins/uast-kotlin/tests/SimpleKotlinRenderLogTest.kt b/plugins/uast-kotlin/tests/SimpleKotlinRenderLogTest.kt index e0e5ddfe07f..4c0dc41d5e8 100644 --- a/plugins/uast-kotlin/tests/SimpleKotlinRenderLogTest.kt +++ b/plugins/uast-kotlin/tests/SimpleKotlinRenderLogTest.kt @@ -44,4 +44,6 @@ class SimpleKotlinRenderLogTest : AbstractKotlinRenderLogTest() { @Test fun testParameterPropertyWithAnnotation() = doTest("ParameterPropertyWithAnnotation") @Test fun testParametersWithDefaultValues() = doTest("ParametersWithDefaultValues") + + @Test fun testUnexpectedContainer() = doTest("UnexpectedContainerException") { testName, file -> check(testName, file, false) } }