LightAnnotations don't try to get kotlinOrigin if not building from sources

This commit is contained in:
Nicolay Mitropolsky
2018-07-06 13:54:11 +03:00
parent eaa58c3ec2
commit be611cd154
4 changed files with 19 additions and 5 deletions
@@ -21,6 +21,7 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiModifierList
import com.intellij.psi.PsiModifierListOwner
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
import org.jetbrains.kotlin.asJava.builder.LightMemberOriginForDeclaration
import org.jetbrains.kotlin.asJava.classes.KtLightClassForSourceDeclaration
import org.jetbrains.kotlin.asJava.classes.lazyPub
import org.jetbrains.kotlin.descriptors.ClassDescriptor
@@ -30,6 +31,7 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isPropertyParameter
import org.jetbrains.kotlin.resolve.AnnotationChecker
import org.jetbrains.kotlin.resolve.BindingContext
@@ -94,6 +96,9 @@ private fun computeAnnotations(lightModifierList: KtLightModifierList<*>): List<
private fun lightAnnotationsForEntries(lightModifierList: KtLightModifierList<*>): List<KtLightAnnotationForSourceEntry> {
val lightModifierListOwner = lightModifierList.parent
if (!isFromSources(lightModifierList)) return emptyList()
val annotatedKtDeclaration = lightModifierListOwner.kotlinOrigin as? KtDeclaration
if (annotatedKtDeclaration == null || !annotatedKtDeclaration.isValid || !hasAnnotationsInSource(annotatedKtDeclaration)) {
@@ -118,6 +123,15 @@ private fun lightAnnotationsForEntries(lightModifierList: KtLightModifierList<*>
}
}
fun isFromSources(lightElement: KtLightElement<*, *>): Boolean {
if (lightElement is KtLightClassForSourceDeclaration) return true
if (lightElement.parent is KtLightClassForSourceDeclaration) return true
val ktLightMember = lightElement.getParentOfType<KtLightMember<*>>(false) ?: return true // hope it will never happen
if (ktLightMember.lightMemberOrigin !is LightMemberOriginForDeclaration) return false
return true
}
private fun getAnnotationDescriptors(declaration: KtDeclaration, annotatedLightElement: KtLightElement<*, *>): List<AnnotationDescriptor> {
val context = LightClassGenerationSupport.getInstance(declaration.project).analyze(declaration)
@@ -30,7 +30,7 @@ class KtLightParameter(
KtLightDeclaration<KtParameter, PsiParameter> {
private val lightModifierList by lazyPub { KtLightSimpleModifierList(this, emptySet()) }
private var lightIdentifier: KtLightIdentifier? = null
override val kotlinOrigin: KtParameter?
@@ -233,7 +233,7 @@ class KtLightNullabilityAnnotation(val member: KtLightElement<*, PsiModifierList
override fun findAttributeValue(attributeName: String?) = null
override fun getQualifiedName(): String? {
val annotatedElement = member.kotlinOrigin
val annotatedElement = member.takeIf(::isFromSources)?.kotlinOrigin
?: // it is out of our hands
return getClsNullabilityAnnotation(member)?.qualifiedName
@@ -309,7 +309,7 @@ class KtLightNullabilityAnnotation(val member: KtLightElement<*, PsiModifierList
}
private fun getClsNullabilityAnnotation(member: KtLightElement<*, PsiModifierListOwner>): PsiAnnotation? {
if (!accessAnnotationsClsDelegateIsAllowed && ApplicationManager.getApplication().isUnitTestMode && member.kotlinOrigin != null)
if (!accessAnnotationsClsDelegateIsAllowed && ApplicationManager.getApplication().isUnitTestMode && isFromSources(member) && member.kotlinOrigin != null)
LOG.error("nullability should be retrieved from `kotlinOrigin`")
return member.clsDelegate.modifierList?.annotations?.findLast {
isNullabilityAnnotation(it.qualifiedName)
@@ -235,7 +235,7 @@ class KtLightNullabilityAnnotation(val member: KtLightElement<*, PsiModifierList
override fun findAttributeValue(attributeName: String?) = null
override fun getQualifiedName(): String? {
val annotatedElement = member.kotlinOrigin
val annotatedElement = member.takeIf(::isFromSources)?.kotlinOrigin
?: // it is out of our hands
return getClsNullabilityAnnotation(member)?.qualifiedName
@@ -311,7 +311,7 @@ class KtLightNullabilityAnnotation(val member: KtLightElement<*, PsiModifierList
}
private fun getClsNullabilityAnnotation(member: KtLightElement<*, PsiModifierListOwner>): PsiAnnotation? {
if (!accessAnnotationsClsDelegateIsAllowed && ApplicationManager.getApplication().isUnitTestMode && member.kotlinOrigin != null)
if (!accessAnnotationsClsDelegateIsAllowed && ApplicationManager.getApplication().isUnitTestMode && isFromSources(member) && member.kotlinOrigin != null)
LOG.error("nullability should be retrieved from `kotlinOrigin`")
return member.clsDelegate.modifierList?.annotations?.findLast {
isNullabilityAnnotation(it.qualifiedName)