From 49db1bfe5d85ac89466853740a43216a96749274 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Mon, 13 Nov 2017 12:02:08 +0900 Subject: [PATCH] Introduce preliminary check for special annotations (allopen/noarg/samWithReceiver) (KT-21194) --- .../kotlin/extensions/AnnotationBasedExtension.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/extensions/AnnotationBasedExtension.kt b/compiler/frontend/src/org/jetbrains/kotlin/extensions/AnnotationBasedExtension.kt index 8b8e8a90c35..5a15b313b93 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/extensions/AnnotationBasedExtension.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/extensions/AnnotationBasedExtension.kt @@ -27,12 +27,14 @@ interface AnnotationBasedExtension { fun getAnnotationFqNames(modifierListOwner: KtModifierListOwner?): List fun DeclarationDescriptor.hasSpecialAnnotation(modifierListOwner: KtModifierListOwner?): Boolean { - if (annotations.any { it.isASpecialAnnotation(modifierListOwner) }) return true + val specialAnnotations = getAnnotationFqNames(modifierListOwner).takeIf { it.isNotEmpty() } ?: return false + + if (annotations.any { it.isASpecialAnnotation(specialAnnotations) }) return true if (this is ClassDescriptor) { for (superType in TypeUtils.getAllSupertypes(defaultType)) { val superTypeDescriptor = superType.constructor.declarationDescriptor as? ClassDescriptor ?: continue - if (superTypeDescriptor.annotations.any { it.isASpecialAnnotation(modifierListOwner) }) return true + if (superTypeDescriptor.annotations.any { it.isASpecialAnnotation(specialAnnotations) }) return true } } @@ -40,20 +42,20 @@ interface AnnotationBasedExtension { } private fun AnnotationDescriptor.isASpecialAnnotation( - modifierListOwner: KtModifierListOwner?, + specialAnnotations: List, visitedAnnotations: MutableSet = hashSetOf(), allowMetaAnnotations: Boolean = true ): Boolean { val annotationFqName = fqName?.asString() ?: return false if (annotationFqName in visitedAnnotations) return false // Prevent infinite recursion - if (annotationFqName in getAnnotationFqNames(modifierListOwner)) return true + if (annotationFqName in specialAnnotations) return true visitedAnnotations.add(annotationFqName) if (allowMetaAnnotations) { val annotationType = annotationClass ?: return false for (metaAnnotation in annotationType.annotations) { - if (metaAnnotation.isASpecialAnnotation(modifierListOwner, visitedAnnotations, allowMetaAnnotations = true)) { + if (metaAnnotation.isASpecialAnnotation(specialAnnotations, visitedAnnotations, allowMetaAnnotations = true)) { return true } }