Fix convering parents of annotations on primary constructor parameters

This commit is contained in:
Dmitry Jemerov
2017-09-26 15:49:18 +02:00
parent 56168b4a09
commit a4f5a1335a
6 changed files with 38 additions and 8 deletions
@@ -119,6 +119,10 @@ private fun KtParameter.toAnnotationLightMethod(): PsiMethod? {
return LightClassUtil.getLightClassMethod(this)
}
fun KtParameter.toLightGetter(): PsiMethod? = LightClassUtil.getLightClassPropertyMethods(this).getter
fun KtParameter.toLightSetter(): PsiMethod? = LightClassUtil.getLightClassPropertyMethods(this).setter
fun KtTypeParameter.toPsiTypeParameters(): List<PsiTypeParameter> {
val paramList = getNonStrictParentOfType<KtTypeParameterList>() ?: return listOf()
@@ -17,6 +17,8 @@
package org.jetbrains.uast.kotlin
import com.intellij.psi.PsiElement
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.getStrictParentOfType
@@ -38,10 +40,14 @@ abstract class KotlinAbstractUElement(private val givenParent: UElement?) : UEle
val target = psi.useSiteTarget?.getAnnotationUseSiteTarget()
when (target) {
AnnotationUseSiteTarget.PROPERTY_GETTER ->
parent = (parentUnwrapped as? KtProperty)?.getter ?: parent
parent = (parentUnwrapped as? KtProperty)?.getter
?: (parentUnwrapped as? KtParameter)?.toLightGetter()
?: parent
AnnotationUseSiteTarget.PROPERTY_SETTER ->
parent = (parentUnwrapped as? KtProperty)?.setter ?: parent
parent = (parentUnwrapped as? KtProperty)?.setter
?: (parentUnwrapped as? KtParameter)?.toLightSetter()
?: parent
}
}
@@ -1,5 +1,9 @@
annotation class MyAnnotation
annotation class MyAnnotation2
annotation class MyAnnotation3
annotation class MyAnnotation4
annotation class MyAnnotation5
class Test1(@MyAnnotation var bar: Int)
class Test2(@get:MyAnnotation @set:MyAnnotation @setparam:MyAnnotation @property:MyAnnotation @field:MyAnnotation var bar: Int)
class Test2(@get:MyAnnotation @set:MyAnnotation2 @setparam:MyAnnotation3 @property:MyAnnotation4 @field:MyAnnotation5 var bar: Int)
@@ -1,5 +1,9 @@
UFile (package = )
UClass (name = MyAnnotation)
UClass (name = MyAnnotation2)
UClass (name = MyAnnotation3)
UClass (name = MyAnnotation4)
UClass (name = MyAnnotation5)
UClass (name = Test1)
UField (name = bar)
UAnnotation (fqName = null)
@@ -13,14 +17,14 @@ UFile (package = )
UAnnotation (fqName = null)
UClass (name = Test2)
UField (name = bar)
UAnnotation (fqName = MyAnnotation)
UAnnotation (fqName = MyAnnotation5)
UAnnotation (fqName = null)
UAnnotationMethod (name = getBar)
UAnnotation (fqName = MyAnnotation)
UAnnotationMethod (name = setBar)
UAnnotation (fqName = MyAnnotation)
UAnnotation (fqName = MyAnnotation2)
UParameter (name = p)
UAnnotation (fqName = MyAnnotation)
UAnnotation (fqName = MyAnnotation3)
UAnnotation (fqName = null)
UAnnotationMethod (name = Test2)
UParameter (name = bar)
@@ -1,6 +1,18 @@
public abstract annotation MyAnnotation {
}
public abstract annotation MyAnnotation2 {
}
public abstract annotation MyAnnotation3 {
}
public abstract annotation MyAnnotation4 {
}
public abstract annotation MyAnnotation5 {
}
public final class Test1 {
private var bar: int
public final fun getBar() : int = UastEmptyExpression
@@ -12,7 +24,7 @@ public final class Test2 {
private var bar: int
@MyAnnotation
public final fun getBar() : int = UastEmptyExpression
@MyAnnotation
@MyAnnotation2
public final fun setBar(p: int) : void = UastEmptyExpression
public fun Test2(bar: int) = UastEmptyExpression
}
@@ -60,7 +60,7 @@ abstract class AbstractKotlinRenderLogTest : AbstractKotlinUastTest(), RenderLog
if (expectedParents != null) {
assertNotNull("Expected to be able to convert PSI element $element", uElement)
val parents = generateSequence(uElement!!.uastParent) { it.uastParent }.joinToString { it.asLogString() }
assertEquals("Inconsistent parents for $uElement (converted from $element)", expectedParents, parents)
assertEquals("Inconsistent parents for ${uElement.asLogString()} (converted from $element)", expectedParents, parents)
}
super.visitElement(element)
}