[AA] introduce KtAnnotationApplication

^KT-56046
This commit is contained in:
Dmitrii Gridin
2023-01-27 17:22:14 +01:00
committed by Space Team
parent 59f07c2197
commit 6e25b45f7d
27 changed files with 170 additions and 166 deletions
@@ -20,16 +20,16 @@ public interface KtAnnotated {
*
* @see [KtAnnotationsList.annotations]
*/
public val KtAnnotated.annotations: List<KtAnnotationApplication>
public val KtAnnotated.annotations: List<KtAnnotationApplicationWithArgumentsInfo>
get() = annotationsList.annotations
/**
* A list of annotation overviews.
*
* @see [KtAnnotationsList.annotationOverviews]
* @see [KtAnnotationsList.annotationInfos]
*/
public val KtAnnotated.annotationOverviews: List<KtAnnotationOverview>
get() = annotationsList.annotationOverviews
public val KtAnnotated.annotationInfos: List<KtAnnotationApplicationInfo>
get() = annotationsList.annotationInfos
/**
* Checks if entity has annotation with specified [classId].
@@ -54,7 +54,7 @@ public fun KtAnnotated.hasAnnotation(
*
* @see [KtAnnotationsList.annotationClassIds]
*/
public fun KtAnnotated.annotationsByClassId(classId: ClassId): List<KtAnnotationApplication> =
public fun KtAnnotated.annotationsByClassId(classId: ClassId): List<KtAnnotationApplicationWithArgumentsInfo> =
annotationsList.annotationsByClassId(classId)
/**
@@ -17,33 +17,33 @@ import org.jetbrains.kotlin.psi.KtCallElement
* - For types: `fun foo(x: List<@A Int>){}`
* - Inside other annotation (`B` is annotation here): `@A(B()) fun foo(){}
*/
public data class KtAnnotationApplication(
public sealed interface KtAnnotationApplication {
/**
* The [ClassId] of applied annotation. [ClassId] is a fully qualified name on annotation class.
*/
public val classId: ClassId?,
public val classId: ClassId?
/**
* PsiElement which was used to apply annotation to declaration/type.
*
* Present only for declarations from sources. For declarations from other places (libraries, stdlib) it's `null`
*/
public val psi: KtCallElement?,
public val psi: KtCallElement?
/**
* [AnnotationUseSiteTarget] to which annotation was applied. May be not-null only for annotation applications for declarations.
*
* See in more details in [Kotlin Documentation](https://kotlinlang.org/docs/annotations.html#annotation-use-site-targets) for more information about annotation targets.
*/
public val useSiteTarget: AnnotationUseSiteTarget?,
public val useSiteTarget: AnnotationUseSiteTarget?
/**
* A list of annotation arguments which were applied when constructing annotation. Every argument is [KtAnnotationValue]
* **true** if the annotation is a call with arguments
*/
public val arguments: List<KtNamedAnnotationValue>,
public val isCallWithArguments: Boolean
/**
* An index of the annotation in an owner. Can be `-1` in case of annotation arguments
*/
public val index: Int,
)
public val index: Int
}
@@ -16,33 +16,33 @@ import org.jetbrains.kotlin.psi.KtCallElement
* - For declarations: `@Deprecated("Should not be used") fun foo(){}`
* - For types: `fun foo(x: List<@A Int>){}`
*/
public data class KtAnnotationOverview(
public data class KtAnnotationApplicationInfo(
/**
* The [ClassId] of applied annotation. [ClassId] is a fully qualified name on annotation class.
*/
public val classId: ClassId?,
public override val classId: ClassId?,
/**
* PsiElement which was used to apply annotation to declaration/type.
* [com.intellij.psi.PsiElement] which was used to apply annotation to declaration/type.
*
* Present only for declarations from sources. For declarations from other places (libraries, stdlib) it's `null`
*/
public val psi: KtCallElement?,
public override val psi: KtCallElement?,
/**
* [AnnotationUseSiteTarget] to which annotation was applied. May be not-null only for annotation applications for declarations.
*
* See in more details in [Kotlin Documentation](https://kotlinlang.org/docs/annotations.html#annotation-use-site-targets) for more information about annotation targets.
*/
public val useSiteTarget: AnnotationUseSiteTarget?,
public override val useSiteTarget: AnnotationUseSiteTarget?,
/**
* **true** if the annotation has any arguments
*/
public val hasArguments: Boolean,
public override val isCallWithArguments: Boolean,
/**
* An index of the annotation in an owner
*/
public val index: Int,
)
public override val index: Int,
) : KtAnnotationApplication
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2023 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.kotlin.analysis.api.annotations
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.psi.KtCallElement
public data class KtAnnotationApplicationWithArgumentsInfo(
override val classId: ClassId?,
override val psi: KtCallElement?,
override val useSiteTarget: AnnotationUseSiteTarget?,
/**
* A list of annotation arguments which were applied when constructing annotation. Every argument is [KtAnnotationValue]
*/
public val arguments: List<KtNamedAnnotationValue>,
override val index: Int,
) : KtAnnotationApplication {
override val isCallWithArguments: Boolean get() = arguments.isNotEmpty()
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2023 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.
*/
@@ -50,7 +50,7 @@ public class KtArrayAnnotationValue(
* Other annotation used as argument. E.g: `@A(B)` where `B` is annotation too
*/
public class KtAnnotationApplicationValue(
public val annotationValue: KtAnnotationApplication,
public val annotationValue: KtAnnotationApplicationWithArgumentsInfo,
) : KtAnnotationValue() {
override val sourcePsi: KtElement? get() = annotationValue.psi
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2023 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.
*/
@@ -56,7 +56,7 @@ internal object KtAnnotationValueRenderer {
renderAnnotationApplication(application.annotationValue)
}
private fun StringBuilder.renderAnnotationApplication(value: KtAnnotationApplication) {
private fun StringBuilder.renderAnnotationApplication(value: KtAnnotationApplicationWithArgumentsInfo) {
append(value.classId)
if (value.arguments.isNotEmpty()) {
append("(")
@@ -22,16 +22,16 @@ public abstract class KtAnnotationsList : KtLifetimeOwner {
*
* @see KtAnnotationApplication
*/
public abstract val annotations: List<KtAnnotationApplication>
public abstract val annotations: List<KtAnnotationApplicationWithArgumentsInfo>
/**
* A list of annotation overviews.
*
* Can be used instead of [annotations] if applicable to reduce resolve.
*
* @see KtAnnotationOverview
* @see KtAnnotationApplicationInfo
*/
public abstract val annotationOverviews: List<KtAnnotationOverview>
public abstract val annotationInfos: List<KtAnnotationApplicationInfo>
/**
* Checks if entity contains annotation with specified [classId].
@@ -74,9 +74,9 @@ public abstract class KtAnnotationsList : KtLifetimeOwner {
* annotationsList.annotationsByClassId(classId) == annotationsList.annotations.filter { it.classId == classId }
* ```
*
* @see KtAnnotationApplication
* @see KtAnnotationApplicationWithArgumentsInfo
*/
public abstract fun annotationsByClassId(classId: ClassId): List<KtAnnotationApplication>
public abstract fun annotationsByClassId(classId: ClassId): List<KtAnnotationApplicationWithArgumentsInfo>
/**
* A list of annotations [ClassId].
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.analysis.api.renderer.base.annotations.renderers
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotated
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationApplication
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationApplicationWithArgumentsInfo
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationValueRenderer
import org.jetbrains.kotlin.analysis.api.renderer.base.annotations.KtAnnotationRenderer
import org.jetbrains.kotlin.analysis.utils.printer.PrettyPrinter
@@ -30,6 +31,8 @@ public interface KtAnnotationArgumentsRenderer {
owner: KtAnnotated,
printer: PrettyPrinter
) {
if (annotation !is KtAnnotationApplicationWithArgumentsInfo) return
if (annotation.arguments.isEmpty()) return
printer.printCollection(annotation.arguments, prefix = "(", postfix = ")") { argument ->
append(argument.name.render())
@@ -1,11 +1,11 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2023 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.kotlin.analysis.api.signatures
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationApplication
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationApplicationWithArgumentsInfo
import org.jetbrains.kotlin.analysis.api.annotations.KtConstantAnnotationValue
import org.jetbrains.kotlin.analysis.api.annotations.annotationsByClassId
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
@@ -81,7 +81,7 @@ public class KtVariableLikeSignature<out S : KtVariableLikeSymbol>(
return (constantArgumentValue.constantValue.value as? String)?.let(Name::identifier)
}
private fun findParameterNameAnnotation(): KtAnnotationApplication? {
private fun findParameterNameAnnotation(): KtAnnotationApplicationWithArgumentsInfo? {
val allParameterNameAnnotations = returnType.annotationsByClassId(StandardNames.FqNames.parameterNameClassId)
val (explicitAnnotations, implicitAnnotations) = allParameterNameAnnotations.partition { it.psi != null }
@@ -16,7 +16,10 @@ import org.jetbrains.kotlin.analysis.api.contracts.description.KtContractEffectD
import org.jetbrains.kotlin.analysis.api.contracts.description.renderKtContractEffectDeclaration
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtNamedSymbol
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtPossiblyNamedSymbol
import org.jetbrains.kotlin.analysis.api.types.*
import org.jetbrains.kotlin.analysis.api.types.KtClassErrorType
import org.jetbrains.kotlin.analysis.api.types.KtClassTypeQualifier
import org.jetbrains.kotlin.analysis.api.types.KtNonErrorClassType
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.analysis.utils.printer.PrettyPrinter
import org.jetbrains.kotlin.analysis.utils.printer.prettyPrint
@@ -246,11 +249,15 @@ public class DebugSymbolRenderer(
private fun PrettyPrinter.renderAnnotationApplication(call: KtAnnotationApplication) {
renderValue(call.classId, renderSymbolsFully = false)
append('(')
call.arguments.sortedBy { it.name }.forEachIndexed { index, value ->
if (index > 0) {
append(", ")
if (call is KtAnnotationApplicationWithArgumentsInfo) {
call.arguments.sortedBy { it.name }.forEachIndexed { index, value ->
if (index > 0) {
append(", ")
}
renderValue(value, renderSymbolsFully = false)
}
renderValue(value, renderSymbolsFully = false)
} else {
append("isCallWithArguments=${call.isCallWithArguments}")
}
append(')')