diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt.172 b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt.172 index 52b28bd93cb..069e5301106 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt.172 +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinAbstractUElement.kt.172 @@ -18,7 +18,6 @@ package org.jetbrains.uast.kotlin import com.intellij.psi.PsiElement import org.jetbrains.kotlin.asJava.LightClassUtil -import org.jetbrains.kotlin.asJava.classes.KtLightClassForLocalDeclaration import org.jetbrains.kotlin.asJava.toLightGetter import org.jetbrains.kotlin.asJava.toLightSetter import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget @@ -41,14 +40,6 @@ abstract class KotlinAbstractUElement(private val givenParent: UElement?) : UEle val psi = psi var parent = psi?.parent ?: psi?.containingFile - if (psi is KtLightClassForLocalDeclaration) { - val originParent = psi.kotlinOrigin.parent - parent = when (originParent) { - is KtClassBody -> originParent.parent - else -> originParent - } - } - if (psi is KtAnnotationEntry) { val parentUnwrapped = KotlinConverter.unwrapElements(parent) ?: return null val target = psi.useSiteTarget?.getAnnotationUseSiteTarget() diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/KotlinUClass.kt.172 b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/KotlinUClass.kt.172 index 21f29976598..cc9a7addc4b 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/KotlinUClass.kt.172 +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/declarations/KotlinUClass.kt.172 @@ -18,6 +18,7 @@ package org.jetbrains.uast.kotlin import com.intellij.psi.* import org.jetbrains.kotlin.asJava.classes.KtLightClass +import org.jetbrains.kotlin.asJava.classes.KtLightClassForLocalDeclaration import org.jetbrains.kotlin.asJava.classes.KtLightClassForScript import org.jetbrains.kotlin.asJava.elements.KtLightMethod import org.jetbrains.kotlin.load.java.JvmAbi @@ -26,38 +27,21 @@ import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments import org.jetbrains.kotlin.utils.SmartList import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import org.jetbrains.uast.* +import org.jetbrains.uast.java.AbstractJavaUClass import org.jetbrains.uast.kotlin.declarations.KotlinUMethod import org.jetbrains.uast.kotlin.declarations.UastLightIdentifier -abstract class AbstractKotlinUClass(givenParent: UElement?) : KotlinAbstractUElement(givenParent), UClass, JvmDeclarationUElement { - - override val uastDeclarations by lz { - mutableListOf().apply { - addAll(fields) - addAll(initializers) - addAll(methods) - addAll(innerClasses) - } - } - - override val uastSuperTypes: List - get() { - val ktClass = (psi as? KtLightClass)?.kotlinOrigin ?: return emptyList() - return ktClass.superTypeListEntries.mapNotNull { it.typeReference }.map { - LazyKotlinUTypeReferenceExpression(it, this) - } - } - - override val uastAnchor: UElement? - get() = UIdentifier(psi.nameIdentifier, this) - - override val annotations: List by lz { - (sourcePsi as? KtModifierListOwner)?.annotationEntries.orEmpty().map { KotlinUAnnotation(it, this) } - } - - override fun equals(other: Any?) = other is AbstractKotlinUClass && psi == other.psi - override fun hashCode() = psi.hashCode() +abstract class AbstractKotlinUClass(private val givenParent: UElement?) : AbstractJavaUClass(), JvmDeclarationUElement { + override val uastParent: UElement? by lz { givenParent ?: convertParent() } + //TODO: should be merged with KotlinAbstractUElement.convertParent() after detaching from AbstractJavaUClass + override fun convertParent(): UElement? = + (this.psi as? KtLightClassForLocalDeclaration)?.kotlinOrigin?.parent?.let { + when (it) { + is KtClassBody -> it.parent.toUElement() // TODO: it seems that `class_body`-s are never created in kotlin uast in top-down walk, probably they should be completely skipped and always unwrapped + else -> it.toUElement() + } + } ?: (psi.parent ?: psi.containingFile).toUElement() } open class KotlinUClass private constructor( @@ -81,6 +65,9 @@ open class KotlinUClass private constructor( override fun getContainingFile(): PsiFile? = unwrapFakeFileForLightClass(psi.containingFile) + override val annotations: List + get() = ktClass?.annotationEntries?.map { KotlinUAnnotation(it, this) } ?: emptyList() + override val uastAnchor: UElement get() = UIdentifier(nameIdentifier, this) @@ -223,8 +210,8 @@ class KotlinUAnonymousClass( class KotlinScriptUClass( psi: KtLightClassForScript, - givenParent: UElement? -) : AbstractKotlinUClass(givenParent), PsiClass by psi { + override val uastParent: UElement? +) : AbstractJavaUClass(), PsiClass by psi { override fun getContainingFile(): PsiFile = unwrapFakeFileForLightClass(psi.containingFile) override fun getNameIdentifier(): PsiIdentifier? = UastLightIdentifier(psi, psi.kotlinOrigin) diff --git a/plugins/uast-kotlin/testData/ClassAnnotation.kt.172 b/plugins/uast-kotlin/testData/ClassAnnotation.kt.172 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/plugins/uast-kotlin/testData/ClassAnnotation.log.txt.172 b/plugins/uast-kotlin/testData/ClassAnnotation.log.txt.172 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/plugins/uast-kotlin/testData/ClassAnnotation.render.txt.172 b/plugins/uast-kotlin/testData/ClassAnnotation.render.txt.172 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/plugins/uast-kotlin/testData/SimpleScript.render.txt.172 b/plugins/uast-kotlin/testData/SimpleScript.render.txt.172 new file mode 100644 index 00000000000..8f780ccfcbf --- /dev/null +++ b/plugins/uast-kotlin/testData/SimpleScript.render.txt.172 @@ -0,0 +1,25 @@ +public class SimpleScript : kotlin.script.templates.standard.ScriptTemplateWithArgs { + public final fun getBarOrNull(@org.jetbrains.annotations.NotNull flag: boolean) : SimpleScript.Bar { + return if (flag) (42) else null + } + public fun SimpleScript(@null p: java.lang.String[]) { + println("Hello World!") + getBarOrNull(true) + println("Goodbye World!") + } + public static final class Bar { + @org.jetbrains.annotations.NotNull private final var b: int = 0 + @org.jetbrains.annotations.NotNull private final var a: int + public final fun getB() : int = UastEmptyExpression + public final fun getAPlusB() : int { + return a + b + } + public final fun getA() : int = UastEmptyExpression + public fun Bar(@org.jetbrains.annotations.NotNull a: int) = UastEmptyExpression + public static final class Baz { + public final fun doSomething() : void { + } + public fun Baz() = UastEmptyExpression + } + } +} diff --git a/plugins/uast-kotlin/testData/UnexpectedContainerException.types.txt.172 b/plugins/uast-kotlin/testData/UnexpectedContainerException.types.txt.172 new file mode 100644 index 00000000000..abfcfc69e45 --- /dev/null +++ b/plugins/uast-kotlin/testData/UnexpectedContainerException.types.txt.172 @@ -0,0 +1,39 @@ +UFile (package = ) [public abstract interface Callback {...] + UClass (name = Callback) [public abstract interface Callback {...}] + UAnnotationMethod (name = onError) [public abstract fun onError(@org.jetbrains.annotations.NotNull throwable: java.lang.Throwable) : void = UastEmptyExpression] + UParameter (name = throwable) [@org.jetbrains.annotations.NotNull var throwable: java.lang.Throwable] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UClass (name = Model) [public final class Model {...}] + UAnnotationMethod (name = crashMe) [public final fun crashMe(@org.jetbrains.annotations.NotNull clazz: java.lang.Class, @org.jetbrains.annotations.NotNull factory: kotlin.jvm.functions.Function0) : void {...}] + UParameter (name = clazz) [@org.jetbrains.annotations.NotNull var clazz: java.lang.Class] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UParameter (name = factory) [@org.jetbrains.annotations.NotNull var factory: kotlin.jvm.functions.Function0] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UBlockExpression [{...}] : PsiType:Void + UThrowExpression [throw ()] : PsiType:Void + UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 0)) [()] : PsiType:UnsupportedOperationException + UIdentifier (Identifier (UnsupportedOperationException)) [UIdentifier (Identifier (UnsupportedOperationException))] + USimpleNameReferenceExpression (identifier = ) [] : PsiType:UnsupportedOperationException + UAnnotationMethod (name = Model) [public fun Model() {...}] + UBlockExpression [{...}] + UBlockExpression [{...}] : PsiType:Unit + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) [crashMe(Callback.java, { ...})] : PsiType:Unit + UIdentifier (Identifier (crashMe)) [UIdentifier (Identifier (crashMe))] + USimpleNameReferenceExpression (identifier = crashMe) [crashMe] : PsiType:Unit + UQualifiedReferenceExpression [Callback.java] : PsiType:Class + UClassLiteralExpression [Callback] : PsiType:KClass + USimpleNameReferenceExpression (identifier = java) [java] : PsiType:Class + ULambdaExpression [{ ...}] : PsiType: + UBlockExpression [{...}] + UObjectLiteralExpression [anonymous object : Callback {... }] : PsiType:Callback + UClass (name = null) [final class null {...}] + UAnnotationMethod (name = onError) [public fun onError(@org.jetbrains.annotations.NotNull throwable: java.lang.Throwable) : void {...}] + UParameter (name = throwable) [@org.jetbrains.annotations.NotNull var throwable: java.lang.Throwable] + UAnnotation (fqName = org.jetbrains.annotations.NotNull) [@org.jetbrains.annotations.NotNull] + UBlockExpression [{...}] : PsiType:Void + UThrowExpression [throw ("")] : PsiType:Void + UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 1)) [("")] : PsiType:UnsupportedOperationException + UIdentifier (Identifier (UnsupportedOperationException)) [UIdentifier (Identifier (UnsupportedOperationException))] + USimpleNameReferenceExpression (identifier = ) [] : PsiType:UnsupportedOperationException + ULiteralExpression (value = "") [""] : PsiType:String + UAnnotationMethod (name = Model$1$1) [fun Model$1$1() = UastEmptyExpression]