LightAnnotations don't try to get kotlinOrigin if not building from sources
This commit is contained in:
+14
@@ -21,6 +21,7 @@ import com.intellij.psi.PsiElement
|
|||||||
import com.intellij.psi.PsiModifierList
|
import com.intellij.psi.PsiModifierList
|
||||||
import com.intellij.psi.PsiModifierListOwner
|
import com.intellij.psi.PsiModifierListOwner
|
||||||
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
|
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.KtLightClassForSourceDeclaration
|
||||||
import org.jetbrains.kotlin.asJava.classes.lazyPub
|
import org.jetbrains.kotlin.asJava.classes.lazyPub
|
||||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
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.AnnotationWithTarget
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.isPropertyParameter
|
import org.jetbrains.kotlin.psi.psiUtil.isPropertyParameter
|
||||||
import org.jetbrains.kotlin.resolve.AnnotationChecker
|
import org.jetbrains.kotlin.resolve.AnnotationChecker
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
@@ -94,6 +96,9 @@ private fun computeAnnotations(lightModifierList: KtLightModifierList<*>): List<
|
|||||||
|
|
||||||
private fun lightAnnotationsForEntries(lightModifierList: KtLightModifierList<*>): List<KtLightAnnotationForSourceEntry> {
|
private fun lightAnnotationsForEntries(lightModifierList: KtLightModifierList<*>): List<KtLightAnnotationForSourceEntry> {
|
||||||
val lightModifierListOwner = lightModifierList.parent
|
val lightModifierListOwner = lightModifierList.parent
|
||||||
|
|
||||||
|
if (!isFromSources(lightModifierList)) return emptyList()
|
||||||
|
|
||||||
val annotatedKtDeclaration = lightModifierListOwner.kotlinOrigin as? KtDeclaration
|
val annotatedKtDeclaration = lightModifierListOwner.kotlinOrigin as? KtDeclaration
|
||||||
|
|
||||||
if (annotatedKtDeclaration == null || !annotatedKtDeclaration.isValid || !hasAnnotationsInSource(annotatedKtDeclaration)) {
|
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> {
|
private fun getAnnotationDescriptors(declaration: KtDeclaration, annotatedLightElement: KtLightElement<*, *>): List<AnnotationDescriptor> {
|
||||||
val context = LightClassGenerationSupport.getInstance(declaration.project).analyze(declaration)
|
val context = LightClassGenerationSupport.getInstance(declaration.project).analyze(declaration)
|
||||||
|
|
||||||
|
|||||||
@@ -233,7 +233,7 @@ class KtLightNullabilityAnnotation(val member: KtLightElement<*, PsiModifierList
|
|||||||
override fun findAttributeValue(attributeName: String?) = null
|
override fun findAttributeValue(attributeName: String?) = null
|
||||||
|
|
||||||
override fun getQualifiedName(): String? {
|
override fun getQualifiedName(): String? {
|
||||||
val annotatedElement = member.kotlinOrigin
|
val annotatedElement = member.takeIf(::isFromSources)?.kotlinOrigin
|
||||||
?: // it is out of our hands
|
?: // it is out of our hands
|
||||||
return getClsNullabilityAnnotation(member)?.qualifiedName
|
return getClsNullabilityAnnotation(member)?.qualifiedName
|
||||||
|
|
||||||
@@ -309,7 +309,7 @@ class KtLightNullabilityAnnotation(val member: KtLightElement<*, PsiModifierList
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getClsNullabilityAnnotation(member: KtLightElement<*, PsiModifierListOwner>): PsiAnnotation? {
|
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`")
|
LOG.error("nullability should be retrieved from `kotlinOrigin`")
|
||||||
return member.clsDelegate.modifierList?.annotations?.findLast {
|
return member.clsDelegate.modifierList?.annotations?.findLast {
|
||||||
isNullabilityAnnotation(it.qualifiedName)
|
isNullabilityAnnotation(it.qualifiedName)
|
||||||
|
|||||||
+2
-2
@@ -235,7 +235,7 @@ class KtLightNullabilityAnnotation(val member: KtLightElement<*, PsiModifierList
|
|||||||
override fun findAttributeValue(attributeName: String?) = null
|
override fun findAttributeValue(attributeName: String?) = null
|
||||||
|
|
||||||
override fun getQualifiedName(): String? {
|
override fun getQualifiedName(): String? {
|
||||||
val annotatedElement = member.kotlinOrigin
|
val annotatedElement = member.takeIf(::isFromSources)?.kotlinOrigin
|
||||||
?: // it is out of our hands
|
?: // it is out of our hands
|
||||||
return getClsNullabilityAnnotation(member)?.qualifiedName
|
return getClsNullabilityAnnotation(member)?.qualifiedName
|
||||||
|
|
||||||
@@ -311,7 +311,7 @@ class KtLightNullabilityAnnotation(val member: KtLightElement<*, PsiModifierList
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getClsNullabilityAnnotation(member: KtLightElement<*, PsiModifierListOwner>): PsiAnnotation? {
|
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`")
|
LOG.error("nullability should be retrieved from `kotlinOrigin`")
|
||||||
return member.clsDelegate.modifierList?.annotations?.findLast {
|
return member.clsDelegate.modifierList?.annotations?.findLast {
|
||||||
isNullabilityAnnotation(it.qualifiedName)
|
isNullabilityAnnotation(it.qualifiedName)
|
||||||
|
|||||||
Reference in New Issue
Block a user