Light Classes: Do not compute annotations for light elements with invalid

origin (possible fix for KT-13199)
This commit is contained in:
Alexey Sedunov
2016-08-09 13:33:48 +03:00
parent 4ab25bd0bb
commit ab7d48a34e
@@ -93,11 +93,10 @@ class KtLightModifierList(
internal fun computeAnnotations(lightElement: PsiModifierList, internal fun computeAnnotations(lightElement: PsiModifierList,
delegate: PsiAnnotationOwner): CachedValue<Array<out PsiAnnotation>> { delegate: PsiAnnotationOwner): CachedValue<Array<out PsiAnnotation>> {
val cacheManager = CachedValuesManager.getManager(lightElement.project) fun doCompute(): Array<PsiAnnotation> {
return cacheManager.createCachedValue<Array<out PsiAnnotation>>(
{
val lightOwner = lightElement.parent as? KtLightElement<*, *> val lightOwner = lightElement.parent as? KtLightElement<*, *>
val declaration = lightOwner?.kotlinOrigin as? KtDeclaration 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 descriptor = declaration?.let { LightClassGenerationSupport.getInstance(lightElement.project).resolveToDescriptor(it) }
val annotatedDescriptor = when { val annotatedDescriptor = when {
descriptor !is PropertyDescriptor || lightOwner !is KtLightMethod -> descriptor descriptor !is PropertyDescriptor || lightOwner !is KtLightMethod -> descriptor
@@ -107,7 +106,8 @@ internal fun computeAnnotations(lightElement: PsiModifierList,
} }
val ktAnnotations = annotatedDescriptor?.annotations?.getAllAnnotations() ?: emptyList() val ktAnnotations = annotatedDescriptor?.annotations?.getAllAnnotations() ?: emptyList()
var nextIndex = 0 var nextIndex = 0
val result = delegate.annotations val result = delegate
.annotations
.map { clsAnnotation -> .map { clsAnnotation ->
val currentIndex = ktAnnotations.indexOfFirst(nextIndex) { val currentIndex = ktAnnotations.indexOfFirst(nextIndex) {
it.annotation.type.constructor.declarationDescriptor?.fqNameUnsafe?.asString() == clsAnnotation.qualifiedName it.annotation.type.constructor.declarationDescriptor?.fqNameUnsafe?.asString() == clsAnnotation.qualifiedName
@@ -121,9 +121,11 @@ internal fun computeAnnotations(lightElement: PsiModifierList,
else clsAnnotation else clsAnnotation
} }
.toTypedArray() .toTypedArray()
return result
}
CachedValueProvider.Result.create(result, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT) return CachedValuesManager.getManager(lightElement.project).createCachedValue<Array<out PsiAnnotation>>(
}, { CachedValueProvider.Result.create(doCompute(), PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT) },
false false
) )
} }