Additional minor fixes for KT-33594: avoid using hard-coded annotation name, simplify hasAnnotationToSuppressDeprecation()
This commit is contained in:
+28
-10
@@ -20,8 +20,11 @@ import com.intellij.codeInsight.intention.HighPriorityAction
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.builtins.StandardNames
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
|
||||
import org.jetbrains.kotlin.idea.codeInliner.UsageReplacementStrategy
|
||||
@@ -29,10 +32,7 @@ import org.jetbrains.kotlin.idea.core.moveCaret
|
||||
import org.jetbrains.kotlin.idea.core.targetDescriptors
|
||||
import org.jetbrains.kotlin.idea.quickfix.CleanupFix
|
||||
import org.jetbrains.kotlin.idea.quickfix.KotlinSingleIntentionActionFactory
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtImportDirective
|
||||
import org.jetbrains.kotlin.psi.KtReferenceExpression
|
||||
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getCalleeExpressionIfAny
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
|
||||
@@ -66,16 +66,34 @@ class DeprecatedSymbolUsageFix(
|
||||
}
|
||||
|
||||
private fun KtFile.hasAnnotationToSuppressDeprecation(): Boolean {
|
||||
val suppressAnnotationEntry = annotationEntries.firstOrNull {
|
||||
it.shortName?.asString() == "Suppress"
|
||||
&& it.resolveToCall()?.resultingDescriptor?.containingDeclaration?.fqNameSafe == KotlinBuiltIns.FQ_NAMES.suppress
|
||||
} ?: return false
|
||||
return suppressAnnotationEntry.valueArguments.any {
|
||||
val suppressAnnotation = annotationEntries.firstOrNull { it.isSuppress() } ?: return false
|
||||
|
||||
for (valueArgument in suppressAnnotation.valueArguments) {
|
||||
val template = valueArgument.getArgumentExpression() as? KtStringTemplateExpression ?: continue
|
||||
val text = template.entries.singleOrNull()?.text ?: continue
|
||||
|
||||
if (text == Errors.DEPRECATION.name || text == Errors.DEPRECATION_ERROR.name) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return suppressAnnotation.valueArguments.any {
|
||||
val text = (it.getArgumentExpression() as? KtStringTemplateExpression)?.entries?.singleOrNull()?.text ?: return@any false
|
||||
text.equals("DEPRECATION", ignoreCase = true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtAnnotationEntry.isSuppress(): Boolean {
|
||||
val suppressName = StandardNames.FqNames.suppress
|
||||
|
||||
if (shortName != suppressName.shortName()) {
|
||||
return false
|
||||
}
|
||||
|
||||
val annotationDescriptor = resolveToCall()?.resultingDescriptor?.containingDeclaration as? ClassDescriptor ?: return false
|
||||
return annotationDescriptor.kind == ClassKind.ANNOTATION_CLASS && annotationDescriptor.fqNameSafe == suppressName
|
||||
}
|
||||
|
||||
private fun isImportToBeRemoved(import: KtImportDirective): Boolean {
|
||||
if (import.isAllUnder) return false
|
||||
|
||||
|
||||
Reference in New Issue
Block a user