Refactor: extract AnnotationDescriptor.annotationClass utility

This commit is contained in:
Pavel V. Talanov
2017-04-28 18:41:48 +03:00
parent 7ef18fe5ba
commit 55a0e138fc
19 changed files with 55 additions and 33 deletions
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.psi.KtModifierListOwner
import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.types.TypeUtils
@@ -44,7 +45,7 @@ interface AnnotationBasedExtension {
visitedAnnotations: MutableSet<String> = hashSetOf(),
allowMetaAnnotations: Boolean = true
): Boolean {
val annotationType = type.constructor.declarationDescriptor ?: return false
val annotationType = annotationClass ?: return false
val annotationFqName = annotationType.fqNameSafe.asString()
if (annotationFqName in visitedAnnotations) return false // Prevent infinite recursion
if (annotationFqName in getAnnotationFqNames(modifierListOwner)) return true
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getAnnotationEntries
import org.jetbrains.kotlin.resolve.constants.ArrayValue
import org.jetbrains.kotlin.resolve.constants.EnumValue
import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass
import org.jetbrains.kotlin.resolve.descriptorUtil.getAnnotationRetention
import org.jetbrains.kotlin.resolve.descriptorUtil.isRepeatableAnnotation
import org.jetbrains.kotlin.resolve.inline.InlineUtil
@@ -135,7 +136,7 @@ class AnnotationChecker(private val additionalCheckers: Iterable<AdditionalAnnot
if (KotlinTarget.FUNCTION !in applicableTargets) return
val annotatedExpression = entry.parent as? KtAnnotatedExpression ?: return
val descriptor = trace.get(BindingContext.ANNOTATION, entry) ?: return
val retention = descriptor.type.constructor.declarationDescriptor?.getAnnotationRetention()
val retention = descriptor.annotationClass?.getAnnotationRetention()
if (retention == KotlinRetention.SOURCE) return
val functionLiteralExpression = annotatedExpression.baseExpression as? KtLambdaExpression ?: return
@@ -173,7 +174,7 @@ class AnnotationChecker(private val additionalCheckers: Iterable<AdditionalAnnot
}
@JvmStatic fun applicableTargetSet(descriptor: AnnotationDescriptor): Set<KotlinTarget> {
val classDescriptor = descriptor.type.constructor.declarationDescriptor as? ClassDescriptor ?: return emptySet()
val classDescriptor = descriptor.annotationClass ?: return emptySet()
return applicableTargetSet(classDescriptor) ?: KotlinTarget.DEFAULT_TARGET_SET
}
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.resolvedCallUtil.getImplicitReceivers
import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperClassifiers
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
@@ -79,10 +80,10 @@ private fun ReceiverValue.extractDslMarkerFqNames(): Set<FqName> {
}
private fun Annotations.extractDslMarkerFqNames() =
filter(AnnotationDescriptor::isDslMarker).map { it.type.constructor.declarationDescriptor!!.fqNameSafe }
filter(AnnotationDescriptor::isDslMarker).map { it.annotationClass!!.fqNameSafe }
private fun AnnotationDescriptor.isDslMarker(): Boolean {
val classDescriptor = type.constructor.declarationDescriptor as? ClassDescriptor ?: return false
val classDescriptor = annotationClass ?: return false
return classDescriptor.annotations.hasAnnotation(DSL_MARKER_FQ_NAME)
}