[AA] KtAnnotated: split hasAnnotation to two extensions

This commit is contained in:
Dmitrii Gridin
2023-01-06 16:19:44 +01:00
committed by Space Team
parent 2c305b46df
commit 5a74fec3ac
11 changed files with 90 additions and 44 deletions
@@ -45,13 +45,15 @@ internal class KtFe10AnnotationsList private constructor(
}
}
override fun hasAnnotation(classId: ClassId): Boolean = withValidityAssertion {
fe10Annotations.hasAnnotation(classId.asSingleFqName())
}
override fun hasAnnotation(
classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean,
): Boolean = withValidityAssertion {
fe10Annotations.hasAnnotation(classId.asSingleFqName())
}
acceptAnnotationsWithoutUseSite: Boolean,
): Boolean = hasAnnotation(classId)
override fun annotationsByClassId(classId: ClassId): List<KtAnnotationApplication> = withValidityAssertion {
if (classId in annotationsToIgnore) return@withValidityAssertion emptyList()
@@ -33,10 +33,17 @@ internal class KtFirAnnotationListForDeclaration private constructor(
override fun hasAnnotation(
classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean,
acceptAnnotationsWithoutUseSite: Boolean,
): Boolean = withValidityAssertion {
firSymbol.resolvedAnnotationsWithClassIds.any {
(it.useSiteTarget == useSiteTarget || !strictUseSite && it.useSiteTarget == null) && it.fullyExpandedClassId(useSiteSession) == classId
(it.useSiteTarget == useSiteTarget || acceptAnnotationsWithoutUseSite && it.useSiteTarget == null) &&
it.fullyExpandedClassId(useSiteSession) == classId
}
}
override fun hasAnnotation(classId: ClassId): Boolean = withValidityAssertion {
firSymbol.resolvedAnnotationsWithClassIds.any {
it.fullyExpandedClassId(useSiteSession) == classId
}
}
@@ -38,10 +38,17 @@ internal class KtFirAnnotationListForReceiverParameter private constructor(
override fun hasAnnotation(
classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean,
acceptAnnotationsWithoutUseSite: Boolean,
): Boolean = withValidityAssertion {
receiverParameter.resolvedAnnotationsWithClassIds(firCallableSymbol).any {
(it.useSiteTarget == useSiteTarget || !strictUseSite && it.useSiteTarget == null) && it.fullyExpandedClassId(useSiteSession) == classId
(it.useSiteTarget == useSiteTarget || acceptAnnotationsWithoutUseSite && it.useSiteTarget == null) &&
it.fullyExpandedClassId(useSiteSession) == classId
}
}
override fun hasAnnotation(classId: ClassId): Boolean = withValidityAssertion {
receiverParameter.resolvedAnnotationsWithClassIds(firCallableSymbol).any {
it.fullyExpandedClassId(useSiteSession) == classId
}
}
@@ -29,10 +29,17 @@ internal class KtFirAnnotationListForType private constructor(
override fun hasAnnotation(
classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean,
acceptAnnotationsWithoutUseSite: Boolean,
): Boolean = withValidityAssertion {
coneType.customAnnotations.any {
(it.useSiteTarget == useSiteTarget || !strictUseSite && it.useSiteTarget == null) && it.fullyExpandedClassId(useSiteSession) == classId
(it.useSiteTarget == useSiteTarget || acceptAnnotationsWithoutUseSite && it.useSiteTarget == null) &&
it.fullyExpandedClassId(useSiteSession) == classId
}
}
override fun hasAnnotation(classId: ClassId): Boolean = withValidityAssertion {
coneType.customAnnotations.any {
it.fullyExpandedClassId(useSiteSession) == classId
}
}
@@ -19,9 +19,11 @@ class KtEmptyAnnotationsList(override val token: KtLifetimeToken) : KtAnnotation
override fun hasAnnotation(
classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean,
acceptAnnotationsWithoutUseSite: Boolean,
): Boolean = withValidityAssertion { false }
override fun hasAnnotation(classId: ClassId): Boolean = withValidityAssertion { false }
override fun annotationsByClassId(classId: ClassId): List<KtAnnotationApplication> =
withValidityAssertion { emptyList() }
@@ -28,11 +28,18 @@ public val KtAnnotated.annotations: List<KtAnnotationApplication>
*
* @see [KtAnnotationsList.hasAnnotation]
*/
public fun KtAnnotated.hasAnnotation(classId: ClassId): Boolean = annotationsList.hasAnnotation(classId)
/**
* Checks if entity has annotation with specified [classId] and [useSiteTarget].
*
* @see [KtAnnotationsList.hasAnnotation]
*/
public fun KtAnnotated.hasAnnotation(
classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget? = null,
strictUseSite: Boolean = true,
): Boolean = annotationsList.hasAnnotation(classId, useSiteTarget, strictUseSite)
useSiteTarget: AnnotationUseSiteTarget?,
acceptAnnotationsWithoutUseSite: Boolean = false,
): Boolean = annotationsList.hasAnnotation(classId, useSiteTarget, acceptAnnotationsWithoutUseSite)
/**
* A list of annotations applied with specified [classId].
@@ -30,16 +30,29 @@ public abstract class KtAnnotationsList : KtLifetimeOwner {
* The semantic is equivalent to
* ```
* annotationsList.hasAnnotation(classId) == annotationsList.annotations.any { it.classId == classId }
* ```
*/
public abstract fun hasAnnotation(classId: ClassId): Boolean
/**
* Checks if entity contains annotation with specified [classId] and [useSiteTarget].
* If [useSiteTarget] is **null** [acceptAnnotationsWithoutUseSite] effectively unused.
*
* The semantic is equivalent to
* ```
* annotationsList.hasAnnotation(classId, useSiteTarget, strictUseSite) == annotationsList.annotations.any {
* (it.useSiteTarget == useSiteTarget || !strictUseSite && it.useSiteTarget == null) && it.classId == classId
* (it.useSiteTarget == useSiteTarget || acceptAnnotationsWithoutUseSite && it.useSiteTarget == null) && it.classId == classId
* }
* ```
*
* @param classId [ClassId] to search
* @param useSiteTarget specific [AnnotationUseSiteTarget]
* @param acceptAnnotationsWithoutUseSite add [ClassId] without specified [AnnotationUseSiteTarget] to search
*/
public abstract fun hasAnnotation(
classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget? = null,
strictUseSite: Boolean = true,
useSiteTarget: AnnotationUseSiteTarget?,
acceptAnnotationsWithoutUseSite: Boolean = false,
): Boolean
/**
@@ -36,11 +36,11 @@ import java.lang.annotation.ElementType
internal fun KtAnnotatedSymbol.hasJvmSyntheticAnnotation(
annotationUseSiteTarget: AnnotationUseSiteTarget? = null,
strictUseSite: Boolean = true,
): Boolean = hasAnnotation(JVM_SYNTHETIC_ANNOTATION_CLASS_ID, annotationUseSiteTarget, strictUseSite)
acceptAnnotationsWithoutUseSite: Boolean = false,
): Boolean = hasAnnotation(JVM_SYNTHETIC_ANNOTATION_CLASS_ID, annotationUseSiteTarget, acceptAnnotationsWithoutUseSite)
internal fun KtAnnotatedSymbol.getJvmNameFromAnnotation(annotationUseSiteTarget: AnnotationUseSiteTarget? = null): String? {
val annotation = findAnnotation(StandardClassIds.Annotations.JvmName, annotationUseSiteTarget, strictUseSite = false)
val annotation = findAnnotation(StandardClassIds.Annotations.JvmName, annotationUseSiteTarget, acceptAnnotationsWithoutUseSite = true)
return annotation?.let {
(it.arguments.firstOrNull()?.expression as? KtConstantAnnotationValue)?.constantValue?.value as? String
}
@@ -55,8 +55,9 @@ internal fun isHiddenByDeprecation(
context(KtAnalysisSession)
internal fun KtAnnotatedSymbol.isHiddenOrSynthetic(
annotationUseSiteTarget: AnnotationUseSiteTarget? = null,
strictUseSite: Boolean = true,
) = isHiddenByDeprecation(this, annotationUseSiteTarget) || hasJvmSyntheticAnnotation(annotationUseSiteTarget, strictUseSite)
acceptAnnotationsWithoutUseSite: Boolean = false,
) = isHiddenByDeprecation(this, annotationUseSiteTarget) ||
hasJvmSyntheticAnnotation(annotationUseSiteTarget, acceptAnnotationsWithoutUseSite)
internal fun KtAnnotatedSymbol.hasJvmFieldAnnotation(): Boolean = hasAnnotation(StandardClassIds.Annotations.JvmField)
@@ -65,28 +66,28 @@ internal fun KtAnnotatedSymbol.hasPublishedApiAnnotation(annotationUseSiteTarget
internal fun KtAnnotatedSymbol.hasDeprecatedAnnotation(
annotationUseSiteTarget: AnnotationUseSiteTarget? = null,
strictUseSite: Boolean = true,
): Boolean = hasAnnotation(StandardClassIds.Annotations.Deprecated, annotationUseSiteTarget, strictUseSite)
acceptAnnotationsWithoutUseSite: Boolean = false,
): Boolean = hasAnnotation(StandardClassIds.Annotations.Deprecated, annotationUseSiteTarget, acceptAnnotationsWithoutUseSite)
internal fun KtAnnotatedSymbol.hasJvmOverloadsAnnotation(): Boolean = hasAnnotation(JVM_OVERLOADS_CLASS_ID)
internal fun KtAnnotatedSymbol.hasJvmStaticAnnotation(
annotationUseSiteTarget: AnnotationUseSiteTarget? = null,
strictUseSite: Boolean = true,
): Boolean = hasAnnotation(StandardClassIds.Annotations.JvmStatic, annotationUseSiteTarget, strictUseSite)
acceptAnnotationsWithoutUseSite: Boolean = false,
): Boolean = hasAnnotation(StandardClassIds.Annotations.JvmStatic, annotationUseSiteTarget, acceptAnnotationsWithoutUseSite)
internal fun KtAnnotatedSymbol.hasInlineOnlyAnnotation(): Boolean = hasAnnotation(StandardClassIds.Annotations.InlineOnly)
internal fun KtAnnotatedSymbol.findAnnotation(
classId: ClassId,
annotationUseSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean = true,
acceptAnnotationsWithoutUseSite: Boolean = false,
): KtAnnotationApplication? {
if (!hasAnnotation(classId, annotationUseSiteTarget, strictUseSite)) return null
if (!hasAnnotation(classId, annotationUseSiteTarget, acceptAnnotationsWithoutUseSite)) return null
return annotations.find {
val useSiteTarget = it.useSiteTarget
(useSiteTarget == annotationUseSiteTarget || !strictUseSite && useSiteTarget == null) && it.classId == classId
(useSiteTarget == annotationUseSiteTarget || acceptAnnotationsWithoutUseSite && useSiteTarget == null) && it.classId == classId
}
}
@@ -291,14 +292,14 @@ internal fun KtAnnotatedSymbol.computeThrowsList(
annotationUseSiteTarget: AnnotationUseSiteTarget?,
useSitePosition: PsiElement,
containingClass: SymbolLightClassBase,
strictUseSite: Boolean = true,
acceptAnnotationsWithoutUseSite: Boolean = false,
) {
if (containingClass.isEnum && this is KtFunctionSymbol && name == StandardNames.ENUM_VALUE_OF && isStatic) {
builder.addReference(java.lang.IllegalArgumentException::class.qualifiedName)
builder.addReference(java.lang.NullPointerException::class.qualifiedName)
}
val annoApp = findAnnotation(StandardClassIds.Annotations.Throws, annotationUseSiteTarget, strictUseSite) ?: return
val annoApp = findAnnotation(StandardClassIds.Annotations.Throws, annotationUseSiteTarget, acceptAnnotationsWithoutUseSite) ?: return
fun handleAnnotationValue(annotationValue: KtAnnotationValue) = when (annotationValue) {
is KtArrayAnnotationValue -> {
@@ -292,14 +292,14 @@ internal fun SymbolLightClassBase.createPropertyAccessors(
fun KtPropertyAccessorSymbol.needToCreateAccessor(siteTarget: AnnotationUseSiteTarget): Boolean {
if (onlyJvmStatic &&
!hasJvmStaticAnnotation(siteTarget, strictUseSite = false) &&
!declaration.hasJvmStaticAnnotation(siteTarget, strictUseSite = false)
!hasJvmStaticAnnotation(siteTarget, acceptAnnotationsWithoutUseSite = true) &&
!declaration.hasJvmStaticAnnotation(siteTarget, acceptAnnotationsWithoutUseSite = true)
) return false
if (declaration.hasReifiedParameters) return false
if (!hasBody && visibility.isPrivateOrPrivateToThis()) return false
if (declaration.isHiddenOrSynthetic(siteTarget)) return false
return !isHiddenOrSynthetic(siteTarget, strictUseSite = false)
return !isHiddenOrSynthetic(siteTarget, acceptAnnotationsWithoutUseSite = true)
}
val originalElement = declaration.sourcePsiSafe<KtDeclaration>()
@@ -384,7 +384,7 @@ private fun hasBackingField(property: KtPropertySymbol): Boolean {
}
if (property.modality == Modality.ABSTRACT ||
property.isHiddenOrSynthetic(AnnotationUseSiteTarget.FIELD, strictUseSite = false)
property.isHiddenOrSynthetic(AnnotationUseSiteTarget.FIELD, acceptAnnotationsWithoutUseSite = true)
) return false
return hasBackingFieldByPsi ?: property.hasBackingField
@@ -83,7 +83,7 @@ internal class SymbolLightFieldForProperty private constructor(
private val _isDeprecated: Boolean by lazyPub {
withPropertySymbol { propertySymbol ->
propertySymbol.hasDeprecatedAnnotation(AnnotationUseSiteTarget.FIELD, strictUseSite = false)
propertySymbol.hasDeprecatedAnnotation(AnnotationUseSiteTarget.FIELD, acceptAnnotationsWithoutUseSite = true)
}
}
@@ -115,12 +115,12 @@ internal class SymbolLightFieldForProperty private constructor(
}
PsiModifier.VOLATILE -> withPropertySymbol { propertySymbol ->
val hasAnnotation = propertySymbol.hasAnnotation(VOLATILE_ANNOTATION_CLASS_ID)
val hasAnnotation = propertySymbol.hasAnnotation(VOLATILE_ANNOTATION_CLASS_ID, null)
mapOf(modifier to hasAnnotation)
}
PsiModifier.TRANSIENT -> withPropertySymbol { propertySymbol ->
val hasAnnotation = propertySymbol.hasAnnotation(TRANSIENT_ANNOTATION_CLASS_ID)
val hasAnnotation = propertySymbol.hasAnnotation(TRANSIENT_ANNOTATION_CLASS_ID, null)
mapOf(modifier to hasAnnotation)
}
@@ -1,5 +1,5 @@
/*
* 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.
*/
@@ -136,7 +136,7 @@ internal class SymbolLightAccessorMethod private constructor(
accessorSite,
this@SymbolLightAccessorMethod,
containingClass,
strictUseSite = false
acceptAnnotationsWithoutUseSite = true,
)
}
}
@@ -197,8 +197,8 @@ internal class SymbolLightAccessorMethod private constructor(
private fun isStatic(): Boolean = analyzeForLightClasses(ktModule) {
val propertySymbol = propertySymbol()
propertySymbol.isStatic ||
propertySymbol.hasJvmStaticAnnotation(accessorSite, strictUseSite = false) ||
propertyAccessorSymbol().hasJvmStaticAnnotation(accessorSite, strictUseSite = false)
propertySymbol.hasJvmStaticAnnotation(accessorSite, acceptAnnotationsWithoutUseSite = true) ||
propertyAccessorSymbol().hasJvmStaticAnnotation(accessorSite, acceptAnnotationsWithoutUseSite = true)
}
private val _modifierList: PsiModifierList by lazyPub {
@@ -215,8 +215,8 @@ internal class SymbolLightAccessorMethod private constructor(
private val _isDeprecated: Boolean by lazyPub {
analyzeForLightClasses(ktModule) {
propertySymbol().hasDeprecatedAnnotation(accessorSite, strictUseSite = false) ||
propertyAccessorSymbol().hasDeprecatedAnnotation(accessorSite, strictUseSite = false)
propertySymbol().hasDeprecatedAnnotation(accessorSite, acceptAnnotationsWithoutUseSite = true) ||
propertyAccessorSymbol().hasDeprecatedAnnotation(accessorSite, acceptAnnotationsWithoutUseSite = true)
}
}