FIR/UAST: commonize UAnnotationMethod

This commit is contained in:
Jinseong Jeon
2021-06-18 23:34:18 -07:00
committed by Ilya Kirillov
parent 638b0beadb
commit cda1a7edbe
21 changed files with 113 additions and 62 deletions
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.uast.kotlin
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.uast.*
abstract class BaseKotlinUAnnotationMethod(
psi: KtLightMethod,
givenParent: UElement?
) : BaseKotlinUMethod(psi, psi.kotlinOrigin, givenParent), UAnnotationMethod {
override val psi: KtLightMethod = unwrap<UMethod, KtLightMethod>(psi)
override val uastDefaultValue: UExpression? by lz {
val annotationParameter = sourcePsi as? KtParameter ?: return@lz null
val defaultValue = annotationParameter.defaultValue ?: return@lz null
languagePlugin?.convertElement(defaultValue, this) as? UExpression
}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.uast.kotlin
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
import org.jetbrains.uast.UElement
class FirKotlinUAnnotationMethod(
psi: KtLightMethod,
givenParent: UElement?
) : BaseKotlinUAnnotationMethod(psi, givenParent), FirKotlinUMethodParametersProducer {
override val uastParameters by lz { produceUastParameters(this, receiverTypeReference) }
}
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.asJava.LightClassUtil
import org.jetbrains.kotlin.asJava.elements.*
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.uast.*
class FirKotlinUMethod(
@@ -29,10 +30,12 @@ class FirKotlinUMethod(
psi: KtLightMethod,
givenParent: UElement?
): UMethod {
return when (val kotlinOrigin = psi.kotlinOrigin) {
is KtConstructor<*> ->
val kotlinOrigin = psi.kotlinOrigin
return when {
kotlinOrigin is KtConstructor<*> ->
FirKotlinConstructorUMethod(kotlinOrigin.containingClassOrObject, psi, givenParent)
// TODO: FirKotlinUAnnotationMethod
kotlinOrigin is KtParameter && kotlinOrigin.getParentOfType<KtClass>(true)?.isAnnotation() == true ->
FirKotlinUAnnotationMethod(psi, givenParent)
else ->
FirKotlinUMethod(psi, givenParent)
}
@@ -1,10 +1,10 @@
Annotation -> UClass (name = Annotation)
strings -> UMethod (name = strings)
strings -> UAnnotationMethod (name = strings)
String -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
Annotation -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
A -> UClass (name = A)
AnnotationInner -> UClass (name = AnnotationInner)
value -> UMethod (name = value)
value -> UAnnotationMethod (name = value)
Annotation -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
AnnotationArray -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
Annotation -> [!] UnknownKotlinExpression (CALL_EXPRESSION)
@@ -14,7 +14,7 @@ value -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
Annotation -> [!] UnknownKotlinExpression (CALL_EXPRESSION)
B2 -> UClass (name = B2)
AnnotationArray -> UClass (name = AnnotationArray)
value -> UMethod (name = value)
value -> UAnnotationMethod (name = value)
Annotation -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
AnnotationArray -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
Annotation -> [!] UnknownKotlinExpression (CALL_EXPRESSION)
@@ -1,20 +1,20 @@
IntRange -> UClass (name = IntRange)
from -> UMethod (name = from)
from -> UAnnotationMethod (name = from)
Long -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
to -> UMethod (name = to)
to -> UAnnotationMethod (name = to)
Long -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
RequiresPermission -> UClass (name = RequiresPermission)
anyOf -> UMethod (name = anyOf)
anyOf -> UAnnotationMethod (name = anyOf)
IntArray -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
RequiresStrPermission -> UClass (name = RequiresStrPermission)
strs -> UMethod (name = strs)
strs -> UAnnotationMethod (name = strs)
Array -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
String -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
WithDefaultValue -> UClass (name = WithDefaultValue)
value -> UMethod (name = value)
value -> UAnnotationMethod (name = value)
Int -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
SuppressLint -> UClass (name = SuppressLint)
value -> UMethod (name = value)
value -> UAnnotationMethod (name = value)
String -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
RequiresPermission -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
anyOf -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
@@ -1,7 +1,7 @@
Test -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
A -> UClass (name = A)
MyAnnotation -> UClass (name = MyAnnotation)
text -> UMethod (name = text)
text -> UAnnotationMethod (name = text)
String -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
MyAnnotation -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
B -> UClass (name = B)
@@ -1,5 +1,5 @@
MyReceiverAnnotation -> UClass (name = MyReceiverAnnotation)
name -> UMethod (name = name)
name -> UAnnotationMethod (name = name)
String -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
MyReceiverAnnotation -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
String -> [!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
@@ -1,11 +1,11 @@
UFile (package = )
UClass (name = Annotation)
UMethod (name = strings)
UAnnotationMethod (name = strings)
UClass (name = A)
UAnnotation (fqName = not-implemented-annotation)
UMethod (name = A)
UClass (name = AnnotationInner)
UMethod (name = value)
UAnnotationMethod (name = value)
UClass (name = B1)
UAnnotation (fqName = not-implemented-annotation)
UMethod (name = B1)
@@ -13,7 +13,7 @@ UFile (package = )
UAnnotation (fqName = not-implemented-annotation)
UMethod (name = B2)
UClass (name = AnnotationArray)
UMethod (name = value)
UAnnotationMethod (name = value)
UClass (name = C)
UAnnotation (fqName = not-implemented-annotation)
UMethod (name = C)
@@ -17,13 +17,14 @@ UFile (package = )
UReturnExpression
ULiteralExpression (value = "not-yet-compile-time-constant")
UClass (name = IntRange)
UMethod (name = from)
UMethod (name = to)
UAnnotationMethod (name = from)
UAnnotationMethod (name = to)
UClass (name = RequiresPermission)
UMethod (name = anyOf)
UAnnotationMethod (name = anyOf)
UClass (name = RequiresStrPermission)
UMethod (name = strs)
UAnnotationMethod (name = strs)
UClass (name = WithDefaultValue)
UMethod (name = value)
UAnnotationMethod (name = value)
ULiteralExpression (value = "not-yet-compile-time-constant")
UClass (name = SuppressLint)
UMethod (name = value)
UAnnotationMethod (name = value)
@@ -3,7 +3,7 @@ UFile (package = )
UAnnotation (fqName = not-implemented-annotation)
UMethod (name = A)
UClass (name = MyAnnotation)
UMethod (name = text)
UAnnotationMethod (name = text)
UClass (name = B)
UAnnotation (fqName = not-implemented-annotation)
UField (name = Companion)
@@ -11,4 +11,5 @@ UFile (package = )
UReturnExpression
[!] UnknownKotlinExpression (CALL_EXPRESSION)
UClass (name = MyReceiverAnnotation)
UMethod (name = name)
UAnnotationMethod (name = name)
ULiteralExpression (value = "")
@@ -1,11 +1,11 @@
UFile (package = ) [public abstract annotation Annotation {...]
UClass (name = Annotation) [public abstract annotation Annotation {...}]
UMethod (name = strings) [public abstract fun strings() : java.lang.String[] = UastEmptyExpression]
UAnnotationMethod (name = strings) [public abstract fun strings() : java.lang.String[] = UastEmptyExpression]
UClass (name = A) [public final class A {...}]
UAnnotation (fqName = not-implemented-annotation) [@not-implemented-annotation]
UMethod (name = A) [public fun A() = UastEmptyExpression]
UClass (name = AnnotationInner) [public abstract annotation AnnotationInner {...}]
UMethod (name = value) [public abstract fun value() : Annotation = UastEmptyExpression]
UAnnotationMethod (name = value) [public abstract fun value() : Annotation = UastEmptyExpression]
UClass (name = B1) [public final class B1 {...}]
UAnnotation (fqName = not-implemented-annotation) [@not-implemented-annotation]
UMethod (name = B1) [public fun B1() = UastEmptyExpression]
@@ -13,7 +13,7 @@ UFile (package = ) [public abstract annotation Annotation {...]
UAnnotation (fqName = not-implemented-annotation) [@not-implemented-annotation]
UMethod (name = B2) [public fun B2() = UastEmptyExpression]
UClass (name = AnnotationArray) [public abstract annotation AnnotationArray {...}]
UMethod (name = value) [public abstract fun value() : Annotation[] = UastEmptyExpression]
UAnnotationMethod (name = value) [public abstract fun value() : Annotation[] = UastEmptyExpression]
UClass (name = C) [public final class C {...}]
UAnnotation (fqName = not-implemented-annotation) [@not-implemented-annotation]
UMethod (name = C) [public fun C() = UastEmptyExpression]
@@ -17,13 +17,14 @@ UFile (package = ) [public final class AnnotationParametersKt {...]
UReturnExpression [return "not-yet-compile-time-constant"]
ULiteralExpression (value = "not-yet-compile-time-constant") ["not-yet-compile-time-constant"] : PsiType:int
UClass (name = IntRange) [public abstract annotation IntRange {...}]
UMethod (name = from) [public abstract fun from() : long = UastEmptyExpression]
UMethod (name = to) [public abstract fun to() : long = UastEmptyExpression]
UAnnotationMethod (name = from) [public abstract fun from() : long = UastEmptyExpression]
UAnnotationMethod (name = to) [public abstract fun to() : long = UastEmptyExpression]
UClass (name = RequiresPermission) [public abstract annotation RequiresPermission {...}]
UMethod (name = anyOf) [public abstract fun anyOf() : int[] = UastEmptyExpression]
UAnnotationMethod (name = anyOf) [public abstract fun anyOf() : int[] = UastEmptyExpression]
UClass (name = RequiresStrPermission) [public abstract annotation RequiresStrPermission {...}]
UMethod (name = strs) [public abstract fun strs() : java.lang.String[] = UastEmptyExpression]
UAnnotationMethod (name = strs) [public abstract fun strs() : java.lang.String[] = UastEmptyExpression]
UClass (name = WithDefaultValue) [public abstract annotation WithDefaultValue {...}]
UMethod (name = value) [public abstract fun value() : int = UastEmptyExpression]
UAnnotationMethod (name = value) [public abstract fun value() : int = UastEmptyExpression]
ULiteralExpression (value = "not-yet-compile-time-constant") ["not-yet-compile-time-constant"] : PsiType:int
UClass (name = SuppressLint) [public abstract annotation SuppressLint {...}]
UMethod (name = value) [public abstract fun value() : java.lang.String[] = UastEmptyExpression]
UAnnotationMethod (name = value) [public abstract fun value() : java.lang.String[] = UastEmptyExpression]
@@ -3,7 +3,7 @@ UFile (package = ) [public final class A {...]
UAnnotation (fqName = not-implemented-annotation) [@not-implemented-annotation]
UMethod (name = A) [public fun A() = UastEmptyExpression]
UClass (name = MyAnnotation) [public abstract annotation MyAnnotation {...}]
UMethod (name = text) [public abstract fun text() : java.lang.String = UastEmptyExpression]
UAnnotationMethod (name = text) [public abstract fun text() : java.lang.String = UastEmptyExpression]
UClass (name = B) [public final class B {...}]
UAnnotation (fqName = not-implemented-annotation) [@not-implemented-annotation]
UField (name = Companion) [public static final var Companion: B.Companion]
@@ -11,4 +11,5 @@ UFile (package = ) [public final class ReceiverFunKt {...]
UReturnExpression [return [!] UnknownKotlinExpression (CALL_EXPRESSION)] : PsiType:Void
[!] UnknownKotlinExpression (CALL_EXPRESSION) [[!] UnknownKotlinExpression (CALL_EXPRESSION)]
UClass (name = MyReceiverAnnotation) [public abstract annotation MyReceiverAnnotation {...}]
UMethod (name = name) [public abstract fun name() : java.lang.String = UastEmptyExpression]
UAnnotationMethod (name = name) [public abstract fun name() : java.lang.String = UastEmptyExpression]
ULiteralExpression (value = "") [""] : PsiType:String
@@ -1,11 +1,11 @@
UFile (package = ) [public abstract annotation Annotation {...]
UClass (name = Annotation) [public abstract annotation Annotation {...}]
UMethod (name = strings) [public abstract fun strings() : java.lang.String[] = UastEmptyExpression]
UAnnotationMethod (name = strings) [public abstract fun strings() : java.lang.String[] = UastEmptyExpression]
UClass (name = A) [public final class A {...}]
UAnnotation (fqName = not-implemented-annotation) [@not-implemented-annotation]
UMethod (name = A) [public fun A() = UastEmptyExpression]
UClass (name = AnnotationInner) [public abstract annotation AnnotationInner {...}]
UMethod (name = value) [public abstract fun value() : Annotation = UastEmptyExpression]
UAnnotationMethod (name = value) [public abstract fun value() : Annotation = UastEmptyExpression]
UClass (name = B1) [public final class B1 {...}]
UAnnotation (fqName = not-implemented-annotation) [@not-implemented-annotation]
UMethod (name = B1) [public fun B1() = UastEmptyExpression]
@@ -13,7 +13,7 @@ UFile (package = ) [public abstract annotation Annotation {...]
UAnnotation (fqName = not-implemented-annotation) [@not-implemented-annotation]
UMethod (name = B2) [public fun B2() = UastEmptyExpression]
UClass (name = AnnotationArray) [public abstract annotation AnnotationArray {...}]
UMethod (name = value) [public abstract fun value() : Annotation[] = UastEmptyExpression]
UAnnotationMethod (name = value) [public abstract fun value() : Annotation[] = UastEmptyExpression]
UClass (name = C) [public final class C {...}]
UAnnotation (fqName = not-implemented-annotation) [@not-implemented-annotation]
UMethod (name = C) [public fun C() = UastEmptyExpression]
@@ -17,13 +17,14 @@ UFile (package = ) [public final class AnnotationParametersKt {...]
UReturnExpression [return "not-yet-compile-time-constant"] = Nothing
ULiteralExpression (value = "not-yet-compile-time-constant") ["not-yet-compile-time-constant"] = "not-yet-compile-time-constant"
UClass (name = IntRange) [public abstract annotation IntRange {...}]
UMethod (name = from) [public abstract fun from() : long = UastEmptyExpression]
UMethod (name = to) [public abstract fun to() : long = UastEmptyExpression]
UAnnotationMethod (name = from) [public abstract fun from() : long = UastEmptyExpression]
UAnnotationMethod (name = to) [public abstract fun to() : long = UastEmptyExpression]
UClass (name = RequiresPermission) [public abstract annotation RequiresPermission {...}]
UMethod (name = anyOf) [public abstract fun anyOf() : int[] = UastEmptyExpression]
UAnnotationMethod (name = anyOf) [public abstract fun anyOf() : int[] = UastEmptyExpression]
UClass (name = RequiresStrPermission) [public abstract annotation RequiresStrPermission {...}]
UMethod (name = strs) [public abstract fun strs() : java.lang.String[] = UastEmptyExpression]
UAnnotationMethod (name = strs) [public abstract fun strs() : java.lang.String[] = UastEmptyExpression]
UClass (name = WithDefaultValue) [public abstract annotation WithDefaultValue {...}]
UMethod (name = value) [public abstract fun value() : int = UastEmptyExpression]
UAnnotationMethod (name = value) [public abstract fun value() : int = UastEmptyExpression]
ULiteralExpression (value = "not-yet-compile-time-constant") ["not-yet-compile-time-constant"] = "not-yet-compile-time-constant"
UClass (name = SuppressLint) [public abstract annotation SuppressLint {...}]
UMethod (name = value) [public abstract fun value() : java.lang.String[] = UastEmptyExpression]
UAnnotationMethod (name = value) [public abstract fun value() : java.lang.String[] = UastEmptyExpression]
@@ -3,7 +3,7 @@ UFile (package = ) [public final class A {...]
UAnnotation (fqName = not-implemented-annotation) [@not-implemented-annotation]
UMethod (name = A) [public fun A() = UastEmptyExpression]
UClass (name = MyAnnotation) [public abstract annotation MyAnnotation {...}]
UMethod (name = text) [public abstract fun text() : java.lang.String = UastEmptyExpression]
UAnnotationMethod (name = text) [public abstract fun text() : java.lang.String = UastEmptyExpression]
UClass (name = B) [public final class B {...}]
UAnnotation (fqName = not-implemented-annotation) [@not-implemented-annotation]
UField (name = Companion) [public static final var Companion: B.Companion]
@@ -11,4 +11,5 @@ UFile (package = ) [public final class ReceiverFunKt {...]
UReturnExpression [return [!] UnknownKotlinExpression (CALL_EXPRESSION)] = Nothing
[!] UnknownKotlinExpression (CALL_EXPRESSION) [[!] UnknownKotlinExpression (CALL_EXPRESSION)] = Undetermined
UClass (name = MyReceiverAnnotation) [public abstract annotation MyReceiverAnnotation {...}]
UMethod (name = name) [public abstract fun name() : java.lang.String = UastEmptyExpression]
UAnnotationMethod (name = name) [public abstract fun name() : java.lang.String = UastEmptyExpression]
ULiteralExpression (value = "") [""] = ""
@@ -0,0 +1,17 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.uast.kotlin
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
import org.jetbrains.uast.UElement
class KotlinUAnnotationMethod(
psi: KtLightMethod,
givenParent: UElement?
) : BaseKotlinUAnnotationMethod(psi, givenParent), KotlinUMethodParametersProducer {
override val uastParameters by lz { produceUastParameters(this, receiverTypeReference) }
}
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.uast.*
import org.jetbrains.uast.kotlin.*
import org.jetbrains.uast.kotlin.psi.UastFakeLightMethod
open class KotlinUMethod(
@@ -54,20 +53,6 @@ open class KotlinUMethod(
}
}
open class KotlinUAnnotationMethod(
psi: KtLightMethod,
givenParent: UElement?
) : KotlinUMethod(psi, psi.kotlinOrigin, givenParent), UAnnotationMethod {
override val psi: KtLightMethod = unwrap<UMethod, KtLightMethod>(psi)
override val uastDefaultValue by lz {
val annotationParameter = sourcePsi as? KtParameter ?: return@lz null
val defaultValue = annotationParameter.defaultValue ?: return@lz null
getLanguagePlugin().convertElement(defaultValue, this) as? UExpression
}
}
class KotlinUMethodWithFakeLightDelegate internal constructor(
val original: KtFunction,
fakePsi: UastFakeLightMethod,