Additional fixes for consistent parent conversion

This commit is contained in:
Dmitry Jemerov
2017-10-06 17:21:07 +02:00
parent bde7a657cf
commit 703d053157
5 changed files with 91 additions and 11 deletions
@@ -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<KotlinPrimaryConstructorUMethod>()?.firstOrNull()?.let {
return it.uastBody
}
}
}
val result = KotlinUastLanguagePlugin().convertElementWithParent(parentUnwrapped, null)
if (result is UEnumConstant && element is UDeclaration) {
@@ -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) }
}
}
@@ -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 = <init>)
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 = <init>)
ULiteralExpression (value = "")
UAnnotationMethod (name = Model$1$1)
@@ -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<T>, factory: kotlin.jvm.functions.Function0<? extends T>) : void {
throw <init>()
}
public fun Model() {
{
crashMe(Callback.java, {
anonymous object : Callback {
override fun onError(throwable: Throwable) {
throw UnsupportedOperationException("")
}
}
})
}
}
}
@@ -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) }
}