|
| 1 | +package info.appdev.charting.lint |
| 2 | + |
| 3 | +import com.android.tools.lint.detector.api.Category |
| 4 | +import com.android.tools.lint.detector.api.Detector |
| 5 | +import com.android.tools.lint.detector.api.Implementation |
| 6 | +import com.android.tools.lint.detector.api.Issue |
| 7 | +import com.android.tools.lint.detector.api.JavaContext |
| 8 | +import com.android.tools.lint.detector.api.Scope |
| 9 | +import com.android.tools.lint.detector.api.Severity |
| 10 | +import com.android.tools.lint.detector.api.SourceCodeScanner |
| 11 | +import com.intellij.psi.PsiClass |
| 12 | +import com.intellij.psi.PsiElement |
| 13 | +import org.jetbrains.uast.UReferenceExpression |
| 14 | +import org.jetbrains.uast.UImportStatement |
| 15 | +import org.jetbrains.uast.getParentOfType |
| 16 | + |
| 17 | +/** |
| 18 | + * Lint detector that flags all usages of deprecated legacy entry classes and offers an |
| 19 | + * auto-fix to replace them with their `*Float` equivalents. |
| 20 | + * |
| 21 | + * Covered classes → replacements: |
| 22 | + * - Entry → EntryFloat |
| 23 | + * - BarEntry → BarEntryFloat |
| 24 | + * - BubbleEntry → BubbleEntryFloat |
| 25 | + * - CandleEntry → CandleEntryFloat |
| 26 | + * - PieEntry → PieEntryFloat |
| 27 | + * - RadarEntry → RadarEntryFloat |
| 28 | + */ |
| 29 | +class EntryUsageDetector : Detector(), SourceCodeScanner { |
| 30 | + |
| 31 | + companion object { |
| 32 | + private const val PKG = "info.appdev.charting.data" |
| 33 | + |
| 34 | + /** |
| 35 | + * Maps deprecated FQN → replacement FQN. |
| 36 | + * Add future deprecated classes here; everything else is derived automatically. |
| 37 | + */ |
| 38 | + private val REPLACEMENTS: Map<String, String> = mapOf( |
| 39 | + "$PKG.Entry" to "$PKG.EntryFloat", |
| 40 | + "$PKG.BarEntry" to "$PKG.BarEntryFloat", |
| 41 | + "$PKG.BubbleEntry" to "$PKG.BubbleEntryFloat", |
| 42 | + "$PKG.CandleEntry" to "$PKG.CandleEntryFloat", |
| 43 | + "$PKG.PieEntry" to "$PKG.PieEntryFloat", |
| 44 | + "$PKG.RadarEntry" to "$PKG.RadarEntryFloat", |
| 45 | + ) |
| 46 | + |
| 47 | + @JvmField |
| 48 | + val ISSUE: Issue = Issue.create( |
| 49 | + id = "LegacyEntryUsage", |
| 50 | + briefDescription = "Replace deprecated legacy entry class with its `*Float` equivalent", |
| 51 | + explanation = """ |
| 52 | + Several entry classes (`Entry`, `BarEntry`, `BubbleEntry`, `CandleEntry`, \ |
| 53 | + `PieEntry`, `RadarEntry`) are deprecated and will be removed in a future version. \ |
| 54 | + Replace them with their `*Float` equivalents (e.g. `BarEntryFloat`) for identical \ |
| 55 | + precision, or with `*Double` equivalents for higher precision. |
| 56 | + """, |
| 57 | + category = Category.CORRECTNESS, |
| 58 | + priority = 6, |
| 59 | + severity = Severity.WARNING, |
| 60 | + implementation = Implementation( |
| 61 | + EntryUsageDetector::class.java, |
| 62 | + Scope.JAVA_FILE_SCOPE |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + /** Pre-computed set of simple names for fast look-up in visitReference(). */ |
| 67 | + private val DEPRECATED_SIMPLE_NAMES: Set<String> = |
| 68 | + REPLACEMENTS.keys.map { it.substringAfterLast('.') }.toSet() |
| 69 | + } |
| 70 | + |
| 71 | + // ----------------------------------------------------------------------- |
| 72 | + // Tell Lint to invoke visitReference() for every one of these identifiers. |
| 73 | + // ----------------------------------------------------------------------- |
| 74 | + override fun getApplicableReferenceNames(): List<String> = |
| 75 | + DEPRECATED_SIMPLE_NAMES.toList() |
| 76 | + |
| 77 | + override fun visitReference( |
| 78 | + context: JavaContext, |
| 79 | + reference: UReferenceExpression, |
| 80 | + referenced: PsiElement |
| 81 | + ) { |
| 82 | + if (referenced !is PsiClass) return |
| 83 | + val deprecatedFqn = referenced.qualifiedName ?: return |
| 84 | + val replacementFqn = REPLACEMENTS[deprecatedFqn] ?: return |
| 85 | + |
| 86 | + val deprecatedSimple = deprecatedFqn.substringAfterLast('.') |
| 87 | + val replacementSimple = replacementFqn.substringAfterLast('.') |
| 88 | + |
| 89 | + // --- Import statement: replace the whole FQN --- |
| 90 | + val importNode = reference.getParentOfType<UImportStatement>() |
| 91 | + if (importNode != null) { |
| 92 | + context.report( |
| 93 | + ISSUE, |
| 94 | + importNode, |
| 95 | + context.getLocation(importNode), |
| 96 | + "Replace deprecated `$deprecatedSimple` import with `$replacementSimple`", |
| 97 | + fix().replace() |
| 98 | + .text(deprecatedFqn) |
| 99 | + .with(replacementFqn) |
| 100 | + .autoFix() |
| 101 | + .build() |
| 102 | + ) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + // --- All other references (type annotations, constructor calls, …) --- |
| 107 | + context.report( |
| 108 | + ISSUE, |
| 109 | + reference, |
| 110 | + context.getLocation(reference), |
| 111 | + "Replace deprecated `$deprecatedSimple` with `$replacementSimple`", |
| 112 | + fix().replace() |
| 113 | + .text(deprecatedSimple) |
| 114 | + .with(replacementSimple) |
| 115 | + .autoFix() |
| 116 | + .build() |
| 117 | + ) |
| 118 | + } |
| 119 | +} |
0 commit comments