[SLC] introduce AnnotationFilter
^KT-56046
This commit is contained in:
committed by
Space Team
parent
48ed72c391
commit
ba3ea40a70
+13
@@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* 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.light.classes.symbol.annotations
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiAnnotation
|
||||||
|
|
||||||
|
internal object AlwaysAllowedAnnotationFilter : AnnotationFilter {
|
||||||
|
override fun isAllowed(qualifiedName: String): Boolean = true
|
||||||
|
override fun filtered(annotations: Collection<PsiAnnotation>): Collection<PsiAnnotation> = annotations
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* 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.light.classes.symbol.annotations
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiAnnotation
|
||||||
|
|
||||||
|
internal sealed interface AnnotationFilter {
|
||||||
|
fun isAllowed(qualifiedName: String): Boolean
|
||||||
|
fun filtered(annotations: Collection<PsiAnnotation>): Collection<PsiAnnotation>
|
||||||
|
}
|
||||||
+7
-1
@@ -15,6 +15,7 @@ import java.util.concurrent.atomic.AtomicReference
|
|||||||
internal class LazyAnnotationsBox(
|
internal class LazyAnnotationsBox(
|
||||||
private val annotationsProvider: AnnotationsProvider,
|
private val annotationsProvider: AnnotationsProvider,
|
||||||
private val additionalAnnotationsProvider: AdditionalAnnotationsProvider = EmptyAdditionalAnnotationsProvider,
|
private val additionalAnnotationsProvider: AdditionalAnnotationsProvider = EmptyAdditionalAnnotationsProvider,
|
||||||
|
private val annotationFilter: AnnotationFilter = AlwaysAllowedAnnotationFilter,
|
||||||
) : AnnotationsBox {
|
) : AnnotationsBox {
|
||||||
private val annotationsArray: AtomicReference<Array<PsiAnnotation>?> = AtomicReference()
|
private val annotationsArray: AtomicReference<Array<PsiAnnotation>?> = AtomicReference()
|
||||||
private var specialAnnotations: SmartList<PsiAnnotation>? = null
|
private var specialAnnotations: SmartList<PsiAnnotation>? = null
|
||||||
@@ -42,8 +43,9 @@ internal class LazyAnnotationsBox(
|
|||||||
val foundQualifiers = annotations.mapNotNullTo(hashSetOf()) { it.qualifiedName }
|
val foundQualifiers = annotations.mapNotNullTo(hashSetOf()) { it.qualifiedName }
|
||||||
additionalAnnotationsProvider.addAllAnnotations(annotations, foundQualifiers, owner)
|
additionalAnnotationsProvider.addAllAnnotations(annotations, foundQualifiers, owner)
|
||||||
|
|
||||||
|
val resultAnnotations = annotationFilter.filtered(annotations)
|
||||||
specialAnnotations = null
|
specialAnnotations = null
|
||||||
setAnnotationsArray(if (annotations.isNotEmpty()) annotations.toTypedArray() else PsiAnnotation.EMPTY_ARRAY)
|
setAnnotationsArray(if (resultAnnotations.isNotEmpty()) resultAnnotations.toTypedArray() else PsiAnnotation.EMPTY_ARRAY)
|
||||||
}
|
}
|
||||||
|
|
||||||
return valueToReturn
|
return valueToReturn
|
||||||
@@ -62,6 +64,8 @@ internal class LazyAnnotationsBox(
|
|||||||
): PsiAnnotation? = findAnnotation(owner, qualifiedName, withAdditionalAnnotations = true)
|
): PsiAnnotation? = findAnnotation(owner, qualifiedName, withAdditionalAnnotations = true)
|
||||||
|
|
||||||
fun findAnnotation(owner: PsiModifierList, qualifiedName: String, withAdditionalAnnotations: Boolean): PsiAnnotation? {
|
fun findAnnotation(owner: PsiModifierList, qualifiedName: String, withAdditionalAnnotations: Boolean): PsiAnnotation? {
|
||||||
|
if (!annotationFilter.isAllowed(qualifiedName)) return null
|
||||||
|
|
||||||
annotationsArray.get()?.let { array ->
|
annotationsArray.get()?.let { array ->
|
||||||
return array.find { it.qualifiedName == qualifiedName }
|
return array.find { it.qualifiedName == qualifiedName }
|
||||||
}
|
}
|
||||||
@@ -108,6 +112,8 @@ internal class LazyAnnotationsBox(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun hasAnnotation(owner: PsiModifierList, qualifiedName: String): Boolean {
|
override fun hasAnnotation(owner: PsiModifierList, qualifiedName: String): Boolean {
|
||||||
|
if (!annotationFilter.isAllowed(qualifiedName)) return false
|
||||||
|
|
||||||
annotationsArray.get()?.let { array ->
|
annotationsArray.get()?.let { array ->
|
||||||
return array.any { it.qualifiedName == qualifiedName }
|
return array.any { it.qualifiedName == qualifiedName }
|
||||||
}
|
}
|
||||||
|
|||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
* 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.light.classes.symbol.annotations
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiAnnotation
|
||||||
|
|
||||||
|
internal class SimpleAnnotationFilter(private val allowedQualifiers: Collection<String>) : AnnotationFilter {
|
||||||
|
override fun isAllowed(qualifiedName: String): Boolean = qualifiedName in allowedQualifiers
|
||||||
|
override fun filtered(annotations: Collection<PsiAnnotation>): Collection<PsiAnnotation> = annotations.filter { annotation ->
|
||||||
|
annotation.qualifiedName?.let(::isAllowed) == true
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user