From ab7d48a34ec520a4ce47f9af0d396862e44490ed Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Tue, 9 Aug 2016 13:33:48 +0300 Subject: [PATCH] Light Classes: Do not compute annotations for light elements with invalid origin (possible fix for KT-13199) --- ...tLightModifierListWithExplicitModifiers.kt | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListWithExplicitModifiers.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListWithExplicitModifiers.kt index 7cf2e01b540..331aaa2503f 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListWithExplicitModifiers.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListWithExplicitModifiers.kt @@ -93,37 +93,39 @@ class KtLightModifierList( internal fun computeAnnotations(lightElement: PsiModifierList, delegate: PsiAnnotationOwner): CachedValue> { - val cacheManager = CachedValuesManager.getManager(lightElement.project) - return cacheManager.createCachedValue>( - { - val lightOwner = lightElement.parent as? KtLightElement<*, *> - val declaration = lightOwner?.kotlinOrigin as? KtDeclaration - val descriptor = declaration?.let { LightClassGenerationSupport.getInstance(lightElement.project).resolveToDescriptor(it) } - val annotatedDescriptor = when { - descriptor !is PropertyDescriptor || lightOwner !is KtLightMethod -> descriptor - JvmAbi.isGetterName(lightOwner.name) -> descriptor.getter - JvmAbi.isSetterName(lightOwner.name) -> descriptor.setter - else -> descriptor + fun doCompute(): Array { + val lightOwner = lightElement.parent as? KtLightElement<*, *> + val declaration = lightOwner?.kotlinOrigin as? KtDeclaration + if (declaration != null && !declaration.isValid) return PsiAnnotation.EMPTY_ARRAY + val descriptor = declaration?.let { LightClassGenerationSupport.getInstance(lightElement.project).resolveToDescriptor(it) } + val annotatedDescriptor = when { + descriptor !is PropertyDescriptor || lightOwner !is KtLightMethod -> descriptor + JvmAbi.isGetterName(lightOwner.name) -> descriptor.getter + JvmAbi.isSetterName(lightOwner.name) -> descriptor.setter + else -> descriptor + } + val ktAnnotations = annotatedDescriptor?.annotations?.getAllAnnotations() ?: emptyList() + var nextIndex = 0 + val result = delegate + .annotations + .map { clsAnnotation -> + val currentIndex = ktAnnotations.indexOfFirst(nextIndex) { + it.annotation.type.constructor.declarationDescriptor?.fqNameUnsafe?.asString() == clsAnnotation.qualifiedName + } + if (currentIndex >= 0) { + nextIndex = currentIndex + 1 + val ktAnnotation = ktAnnotations[currentIndex] + val entry = ktAnnotation.annotation.source.getPsi() as? KtAnnotationEntry ?: return@map clsAnnotation + KtLightAnnotation(clsAnnotation, entry, lightElement) + } + else clsAnnotation } - val ktAnnotations = annotatedDescriptor?.annotations?.getAllAnnotations() ?: emptyList() - var nextIndex = 0 - val result = delegate.annotations - .map { clsAnnotation -> - val currentIndex = ktAnnotations.indexOfFirst(nextIndex) { - it.annotation.type.constructor.declarationDescriptor?.fqNameUnsafe?.asString() == clsAnnotation.qualifiedName - } - if (currentIndex >= 0) { - nextIndex = currentIndex + 1 - val ktAnnotation = ktAnnotations[currentIndex] - val entry = ktAnnotation.annotation.source.getPsi() as? KtAnnotationEntry ?: return@map clsAnnotation - KtLightAnnotation(clsAnnotation, entry, lightElement) - } - else clsAnnotation - } - .toTypedArray() + .toTypedArray() + return result + } - CachedValueProvider.Result.create(result, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT) - }, + return CachedValuesManager.getManager(lightElement.project).createCachedValue>( + { CachedValueProvider.Result.create(doCompute(), PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT) }, false ) }