[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( override fun hasAnnotation(
classId: ClassId, classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget?, useSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean, acceptAnnotationsWithoutUseSite: Boolean,
): Boolean = withValidityAssertion { ): Boolean = hasAnnotation(classId)
fe10Annotations.hasAnnotation(classId.asSingleFqName())
}
override fun annotationsByClassId(classId: ClassId): List<KtAnnotationApplication> = withValidityAssertion { override fun annotationsByClassId(classId: ClassId): List<KtAnnotationApplication> = withValidityAssertion {
if (classId in annotationsToIgnore) return@withValidityAssertion emptyList() if (classId in annotationsToIgnore) return@withValidityAssertion emptyList()
@@ -33,10 +33,17 @@ internal class KtFirAnnotationListForDeclaration private constructor(
override fun hasAnnotation( override fun hasAnnotation(
classId: ClassId, classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget?, useSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean, acceptAnnotationsWithoutUseSite: Boolean,
): Boolean = withValidityAssertion { ): Boolean = withValidityAssertion {
firSymbol.resolvedAnnotationsWithClassIds.any { 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( override fun hasAnnotation(
classId: ClassId, classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget?, useSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean, acceptAnnotationsWithoutUseSite: Boolean,
): Boolean = withValidityAssertion { ): Boolean = withValidityAssertion {
receiverParameter.resolvedAnnotationsWithClassIds(firCallableSymbol).any { 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( override fun hasAnnotation(
classId: ClassId, classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget?, useSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean, acceptAnnotationsWithoutUseSite: Boolean,
): Boolean = withValidityAssertion { ): Boolean = withValidityAssertion {
coneType.customAnnotations.any { 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( override fun hasAnnotation(
classId: ClassId, classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget?, useSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean, acceptAnnotationsWithoutUseSite: Boolean,
): Boolean = withValidityAssertion { false } ): Boolean = withValidityAssertion { false }
override fun hasAnnotation(classId: ClassId): Boolean = withValidityAssertion { false }
override fun annotationsByClassId(classId: ClassId): List<KtAnnotationApplication> = override fun annotationsByClassId(classId: ClassId): List<KtAnnotationApplication> =
withValidityAssertion { emptyList() } withValidityAssertion { emptyList() }
@@ -28,11 +28,18 @@ public val KtAnnotated.annotations: List<KtAnnotationApplication>
* *
* @see [KtAnnotationsList.hasAnnotation] * @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( public fun KtAnnotated.hasAnnotation(
classId: ClassId, classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget? = null, useSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean = true, acceptAnnotationsWithoutUseSite: Boolean = false,
): Boolean = annotationsList.hasAnnotation(classId, useSiteTarget, strictUseSite) ): Boolean = annotationsList.hasAnnotation(classId, useSiteTarget, acceptAnnotationsWithoutUseSite)
/** /**
* A list of annotations applied with specified [classId]. * A list of annotations applied with specified [classId].
@@ -30,16 +30,29 @@ public abstract class KtAnnotationsList : KtLifetimeOwner {
* The semantic is equivalent to * The semantic is equivalent to
* ``` * ```
* annotationsList.hasAnnotation(classId) == annotationsList.annotations.any { it.classId == classId } * 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 { * 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( public abstract fun hasAnnotation(
classId: ClassId, classId: ClassId,
useSiteTarget: AnnotationUseSiteTarget? = null, useSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean = true, acceptAnnotationsWithoutUseSite: Boolean = false,
): Boolean ): Boolean
/** /**
@@ -36,11 +36,11 @@ import java.lang.annotation.ElementType
internal fun KtAnnotatedSymbol.hasJvmSyntheticAnnotation( internal fun KtAnnotatedSymbol.hasJvmSyntheticAnnotation(
annotationUseSiteTarget: AnnotationUseSiteTarget? = null, annotationUseSiteTarget: AnnotationUseSiteTarget? = null,
strictUseSite: Boolean = true, acceptAnnotationsWithoutUseSite: Boolean = false,
): Boolean = hasAnnotation(JVM_SYNTHETIC_ANNOTATION_CLASS_ID, annotationUseSiteTarget, strictUseSite) ): Boolean = hasAnnotation(JVM_SYNTHETIC_ANNOTATION_CLASS_ID, annotationUseSiteTarget, acceptAnnotationsWithoutUseSite)
internal fun KtAnnotatedSymbol.getJvmNameFromAnnotation(annotationUseSiteTarget: AnnotationUseSiteTarget? = null): String? { 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 { return annotation?.let {
(it.arguments.firstOrNull()?.expression as? KtConstantAnnotationValue)?.constantValue?.value as? String (it.arguments.firstOrNull()?.expression as? KtConstantAnnotationValue)?.constantValue?.value as? String
} }
@@ -55,8 +55,9 @@ internal fun isHiddenByDeprecation(
context(KtAnalysisSession) context(KtAnalysisSession)
internal fun KtAnnotatedSymbol.isHiddenOrSynthetic( internal fun KtAnnotatedSymbol.isHiddenOrSynthetic(
annotationUseSiteTarget: AnnotationUseSiteTarget? = null, annotationUseSiteTarget: AnnotationUseSiteTarget? = null,
strictUseSite: Boolean = true, acceptAnnotationsWithoutUseSite: Boolean = false,
) = isHiddenByDeprecation(this, annotationUseSiteTarget) || hasJvmSyntheticAnnotation(annotationUseSiteTarget, strictUseSite) ) = isHiddenByDeprecation(this, annotationUseSiteTarget) ||
hasJvmSyntheticAnnotation(annotationUseSiteTarget, acceptAnnotationsWithoutUseSite)
internal fun KtAnnotatedSymbol.hasJvmFieldAnnotation(): Boolean = hasAnnotation(StandardClassIds.Annotations.JvmField) internal fun KtAnnotatedSymbol.hasJvmFieldAnnotation(): Boolean = hasAnnotation(StandardClassIds.Annotations.JvmField)
@@ -65,28 +66,28 @@ internal fun KtAnnotatedSymbol.hasPublishedApiAnnotation(annotationUseSiteTarget
internal fun KtAnnotatedSymbol.hasDeprecatedAnnotation( internal fun KtAnnotatedSymbol.hasDeprecatedAnnotation(
annotationUseSiteTarget: AnnotationUseSiteTarget? = null, annotationUseSiteTarget: AnnotationUseSiteTarget? = null,
strictUseSite: Boolean = true, acceptAnnotationsWithoutUseSite: Boolean = false,
): Boolean = hasAnnotation(StandardClassIds.Annotations.Deprecated, annotationUseSiteTarget, strictUseSite) ): Boolean = hasAnnotation(StandardClassIds.Annotations.Deprecated, annotationUseSiteTarget, acceptAnnotationsWithoutUseSite)
internal fun KtAnnotatedSymbol.hasJvmOverloadsAnnotation(): Boolean = hasAnnotation(JVM_OVERLOADS_CLASS_ID) internal fun KtAnnotatedSymbol.hasJvmOverloadsAnnotation(): Boolean = hasAnnotation(JVM_OVERLOADS_CLASS_ID)
internal fun KtAnnotatedSymbol.hasJvmStaticAnnotation( internal fun KtAnnotatedSymbol.hasJvmStaticAnnotation(
annotationUseSiteTarget: AnnotationUseSiteTarget? = null, annotationUseSiteTarget: AnnotationUseSiteTarget? = null,
strictUseSite: Boolean = true, acceptAnnotationsWithoutUseSite: Boolean = false,
): Boolean = hasAnnotation(StandardClassIds.Annotations.JvmStatic, annotationUseSiteTarget, strictUseSite) ): Boolean = hasAnnotation(StandardClassIds.Annotations.JvmStatic, annotationUseSiteTarget, acceptAnnotationsWithoutUseSite)
internal fun KtAnnotatedSymbol.hasInlineOnlyAnnotation(): Boolean = hasAnnotation(StandardClassIds.Annotations.InlineOnly) internal fun KtAnnotatedSymbol.hasInlineOnlyAnnotation(): Boolean = hasAnnotation(StandardClassIds.Annotations.InlineOnly)
internal fun KtAnnotatedSymbol.findAnnotation( internal fun KtAnnotatedSymbol.findAnnotation(
classId: ClassId, classId: ClassId,
annotationUseSiteTarget: AnnotationUseSiteTarget?, annotationUseSiteTarget: AnnotationUseSiteTarget?,
strictUseSite: Boolean = true, acceptAnnotationsWithoutUseSite: Boolean = false,
): KtAnnotationApplication? { ): KtAnnotationApplication? {
if (!hasAnnotation(classId, annotationUseSiteTarget, strictUseSite)) return null if (!hasAnnotation(classId, annotationUseSiteTarget, acceptAnnotationsWithoutUseSite)) return null
return annotations.find { return annotations.find {
val useSiteTarget = it.useSiteTarget 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?, annotationUseSiteTarget: AnnotationUseSiteTarget?,
useSitePosition: PsiElement, useSitePosition: PsiElement,
containingClass: SymbolLightClassBase, containingClass: SymbolLightClassBase,
strictUseSite: Boolean = true, acceptAnnotationsWithoutUseSite: Boolean = false,
) { ) {
if (containingClass.isEnum && this is KtFunctionSymbol && name == StandardNames.ENUM_VALUE_OF && isStatic) { if (containingClass.isEnum && this is KtFunctionSymbol && name == StandardNames.ENUM_VALUE_OF && isStatic) {
builder.addReference(java.lang.IllegalArgumentException::class.qualifiedName) builder.addReference(java.lang.IllegalArgumentException::class.qualifiedName)
builder.addReference(java.lang.NullPointerException::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) { fun handleAnnotationValue(annotationValue: KtAnnotationValue) = when (annotationValue) {
is KtArrayAnnotationValue -> { is KtArrayAnnotationValue -> {
@@ -292,14 +292,14 @@ internal fun SymbolLightClassBase.createPropertyAccessors(
fun KtPropertyAccessorSymbol.needToCreateAccessor(siteTarget: AnnotationUseSiteTarget): Boolean { fun KtPropertyAccessorSymbol.needToCreateAccessor(siteTarget: AnnotationUseSiteTarget): Boolean {
if (onlyJvmStatic && if (onlyJvmStatic &&
!hasJvmStaticAnnotation(siteTarget, strictUseSite = false) && !hasJvmStaticAnnotation(siteTarget, acceptAnnotationsWithoutUseSite = true) &&
!declaration.hasJvmStaticAnnotation(siteTarget, strictUseSite = false) !declaration.hasJvmStaticAnnotation(siteTarget, acceptAnnotationsWithoutUseSite = true)
) return false ) return false
if (declaration.hasReifiedParameters) return false if (declaration.hasReifiedParameters) return false
if (!hasBody && visibility.isPrivateOrPrivateToThis()) return false if (!hasBody && visibility.isPrivateOrPrivateToThis()) return false
if (declaration.isHiddenOrSynthetic(siteTarget)) return false if (declaration.isHiddenOrSynthetic(siteTarget)) return false
return !isHiddenOrSynthetic(siteTarget, strictUseSite = false) return !isHiddenOrSynthetic(siteTarget, acceptAnnotationsWithoutUseSite = true)
} }
val originalElement = declaration.sourcePsiSafe<KtDeclaration>() val originalElement = declaration.sourcePsiSafe<KtDeclaration>()
@@ -384,7 +384,7 @@ private fun hasBackingField(property: KtPropertySymbol): Boolean {
} }
if (property.modality == Modality.ABSTRACT || if (property.modality == Modality.ABSTRACT ||
property.isHiddenOrSynthetic(AnnotationUseSiteTarget.FIELD, strictUseSite = false) property.isHiddenOrSynthetic(AnnotationUseSiteTarget.FIELD, acceptAnnotationsWithoutUseSite = true)
) return false ) return false
return hasBackingFieldByPsi ?: property.hasBackingField return hasBackingFieldByPsi ?: property.hasBackingField
@@ -83,7 +83,7 @@ internal class SymbolLightFieldForProperty private constructor(
private val _isDeprecated: Boolean by lazyPub { private val _isDeprecated: Boolean by lazyPub {
withPropertySymbol { propertySymbol -> 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 -> 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) mapOf(modifier to hasAnnotation)
} }
PsiModifier.TRANSIENT -> withPropertySymbol { propertySymbol -> 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) 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. * 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, accessorSite,
this@SymbolLightAccessorMethod, this@SymbolLightAccessorMethod,
containingClass, containingClass,
strictUseSite = false acceptAnnotationsWithoutUseSite = true,
) )
} }
} }
@@ -197,8 +197,8 @@ internal class SymbolLightAccessorMethod private constructor(
private fun isStatic(): Boolean = analyzeForLightClasses(ktModule) { private fun isStatic(): Boolean = analyzeForLightClasses(ktModule) {
val propertySymbol = propertySymbol() val propertySymbol = propertySymbol()
propertySymbol.isStatic || propertySymbol.isStatic ||
propertySymbol.hasJvmStaticAnnotation(accessorSite, strictUseSite = false) || propertySymbol.hasJvmStaticAnnotation(accessorSite, acceptAnnotationsWithoutUseSite = true) ||
propertyAccessorSymbol().hasJvmStaticAnnotation(accessorSite, strictUseSite = false) propertyAccessorSymbol().hasJvmStaticAnnotation(accessorSite, acceptAnnotationsWithoutUseSite = true)
} }
private val _modifierList: PsiModifierList by lazyPub { private val _modifierList: PsiModifierList by lazyPub {
@@ -215,8 +215,8 @@ internal class SymbolLightAccessorMethod private constructor(
private val _isDeprecated: Boolean by lazyPub { private val _isDeprecated: Boolean by lazyPub {
analyzeForLightClasses(ktModule) { analyzeForLightClasses(ktModule) {
propertySymbol().hasDeprecatedAnnotation(accessorSite, strictUseSite = false) || propertySymbol().hasDeprecatedAnnotation(accessorSite, acceptAnnotationsWithoutUseSite = true) ||
propertyAccessorSymbol().hasDeprecatedAnnotation(accessorSite, strictUseSite = false) propertyAccessorSymbol().hasDeprecatedAnnotation(accessorSite, acceptAnnotationsWithoutUseSite = true)
} }
} }