From 299a4b7f691274b9c10d741314a9641e3c62d602 Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Sun, 22 Oct 2017 10:50:30 +0300 Subject: [PATCH] UAST: Fix for unsafe cast for annotation (EA-109738) --- .../uast/kotlin/KotlinAbstractUElement.kt | 2 +- .../tests/KotlinDetachedUastTest.kt | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) 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 52daba8c5f0..83fdd372c27 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt @@ -143,7 +143,7 @@ private fun findAnnotationClassFromConstructorParameter(parameter: KtParameter): val primaryConstructor = parameter.getStrictParentOfType() ?: return null val containingClass = primaryConstructor.getContainingClassOrObject() if (containingClass.isAnnotation()) { - return KotlinUastLanguagePlugin().convertElementWithParent(containingClass, null) as UClass + return KotlinUastLanguagePlugin().convertElementWithParent(containingClass, null) as? UClass } return null } diff --git a/plugins/uast-kotlin/tests/KotlinDetachedUastTest.kt b/plugins/uast-kotlin/tests/KotlinDetachedUastTest.kt index 5de28db16ac..aaefb4b5fef 100644 --- a/plugins/uast-kotlin/tests/KotlinDetachedUastTest.kt +++ b/plugins/uast-kotlin/tests/KotlinDetachedUastTest.kt @@ -25,10 +25,10 @@ import org.jetbrains.kotlin.asJava.elements.KtLightElement import org.jetbrains.kotlin.idea.core.copied import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor +import org.jetbrains.kotlin.psi.KtPsiFactory import org.jetbrains.kotlin.psi.psiUtil.getParentOfType -import org.jetbrains.uast.UClass -import org.jetbrains.uast.getParentOfType -import org.jetbrains.uast.toUElement +import org.jetbrains.uast.* +import org.jetbrains.uast.test.env.findUElementByTextFromPsi class KotlinDetachedUastTest : KotlinLightCodeInsightFixtureTestCase() { @@ -61,6 +61,19 @@ class KotlinDetachedUastTest : KotlinLightCodeInsightFixtureTestCase() { copied.originalFile.virtualFile) } + fun testParameterInAnnotationClassFromFactory() { + + val detachedClass = KtPsiFactory(project).createClass(""" + annotation class MyAnnotation(val myParam: String = "default") + """) + + detachedClass.findUElementByTextFromPsi("default") + .getParentOfType().let { + TestCase.assertNotNull("it should return something at least", it) + } + + } + } fun T?.orFail(msg: String): T = this ?: error(msg) \ No newline at end of file