Skip to content

Commit ac08271

Browse files
committed
Add intent action for change deprecated entries
1 parent 79a3f51 commit ac08271

10 files changed

Lines changed: 83 additions & 114 deletions

File tree

app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import info.appdev.charting.components.Legend
1919
import info.appdev.charting.components.XAxis
2020
import info.appdev.charting.data.BubbleData
2121
import info.appdev.charting.data.BubbleDataSet
22+
import info.appdev.charting.data.BubbleEntry
2223
import info.appdev.charting.data.BubbleEntryFloat
2324
import info.appdev.charting.data.EntryFloat
2425
import info.appdev.charting.highlight.Highlight

chartLib/src/main/kotlin/info/appdev/charting/data/BarEntry.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package info.appdev.charting.data
33
import android.graphics.drawable.Drawable
44

55

6-
@Deprecated("The replacement is BarEntryFloat, or use BarEntryDouble for higher precision. BarEntry is retained for backward compatibility but will be removed in a future version.")
6+
@Deprecated(
7+
message = "The replacement is BarEntryFloat, or use BarEntryDouble for higher precision. BarEntry is retained for backward compatibility but will be removed in a future version.",
8+
replaceWith = ReplaceWith("BarEntryFloat", "info.appdev.charting.data.BarEntryFloat")
9+
)
710
open class BarEntry : BarEntryFloat {
811

912
/**

chartLib/src/main/kotlin/info/appdev/charting/data/BubbleEntry.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package info.appdev.charting.data
33
import android.annotation.SuppressLint
44
import android.graphics.drawable.Drawable
55

6-
@Deprecated("The replacement is BubbleEntryFloat, or use BubbleEntryDouble for higher precision. BubbleEntry is retained for backward compatibility but will be removed in a future version.")
6+
@Deprecated(
7+
message = "The replacement is BubbleEntryFloat, or use BubbleEntryDouble for higher precision. BubbleEntry is retained for backward compatibility but will be removed in a future version.",
8+
replaceWith = ReplaceWith("BubbleEntryFloat", "info.appdev.charting.data.BubbleEntryFloat")
9+
)
710
class BubbleEntry : BubbleEntryFloat {
811

912
/**

chartLib/src/main/kotlin/info/appdev/charting/data/CandleEntry.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package info.appdev.charting.data
22

33
import android.graphics.drawable.Drawable
44

5-
@Deprecated("The replacement is CandleEntryFloat, or use CandleEntryDouble for higher precision. CandleEntry is retained for backward compatibility but will be removed in a future version.")
5+
@Deprecated(
6+
message = "The replacement is CandleEntryFloat, or use CandleEntryDouble for higher precision. CandleEntry is retained for backward compatibility but will be removed in a future version.",
7+
replaceWith = ReplaceWith("CandleEntryFloat", "info.appdev.charting.data.CandleEntryFloat")
8+
)
69
class CandleEntry : CandleEntryFloat {
710

811
/**

chartLib/src/main/kotlin/info/appdev/charting/data/Entry.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import kotlin.math.abs
1616
* Class representing one entry in the chart. Might contain multiple values.
1717
* Might only contain a single value depending on the used constructor.
1818
*/
19-
@Deprecated("The replacement is EntryFloat, or use EntryDouble for higher precision. Entry is retained for backward compatibility but will be removed in a future version.")
19+
@Deprecated(
20+
message = "The replacement is EntryFloat, or use EntryDouble for higher precision. Entry is retained for backward compatibility but will be removed in a future version.",
21+
replaceWith = ReplaceWith("EntryFloat", "info.appdev.charting.data.EntryFloat")
22+
)
2023
class Entry : EntryFloat {
2124

2225
constructor()

chartLib/src/main/kotlin/info/appdev/charting/data/PieEntry.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package info.appdev.charting.data
22

33
import android.graphics.drawable.Drawable
44

5-
@Deprecated("The replacement is PieEntryFloat, or use PieEntryDouble for higher precision. PieEntry is retained for backward compatibility but will be removed in a future version.")
5+
@Deprecated(
6+
message = "The replacement is PieEntryFloat, or use PieEntryDouble for higher precision. PieEntry is retained for backward compatibility but will be removed in a future version.",
7+
replaceWith = ReplaceWith("PieEntryFloat", "info.appdev.charting.data.PieEntryFloat")
8+
)
69
class PieEntry : PieEntryFloat {
710

811
constructor(value: Float) : super(0f)
Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +0,0 @@
1-
package info.appdev.charting.data
2-
3-
import android.annotation.SuppressLint
4-
import android.graphics.drawable.Drawable
5-
import timber.log.Timber
6-
7-
/**
8-
* High-precision pie entry that stores the value as Double, extending [PieEntryFloat]
9-
* so it works seamlessly in the existing pie chart rendering pipeline.
10-
* Use [valueDouble] for full-precision access.
11-
* Pie entries have no meaningful x-axis value.
12-
*/
13-
@SuppressLint("ParcelCreator")
14-
open class PieEntryDouble : PieEntryFloat {
15-
16-
var valueDouble: Double = 0.0
17-
18-
override var y: Float
19-
get() = valueDouble.toFloat()
20-
set(value) { valueDouble = value.toDouble() }
21-
22-
@get:Deprecated("")
23-
@set:Deprecated("")
24-
@Suppress("DEPRECATION")
25-
override var x: Float
26-
get() {
27-
Timber.i("Pie entries do not have x values")
28-
return super.x
29-
}
30-
set(x) {
31-
super.x = x
32-
Timber.i("Pie entries do not have x values")
33-
}
34-
35-
constructor(value: Double) : super(value.toFloat()) { valueDouble = value }
36-
37-
constructor(value: Double, data: Any?) : super(value.toFloat(), data) { valueDouble = value }
38-
39-
constructor(value: Double, icon: Drawable?) : super(value.toFloat(), icon) { valueDouble = value }
40-
41-
constructor(value: Double, icon: Drawable?, data: Any?) : super(value.toFloat(), icon, data) { valueDouble = value }
42-
43-
constructor(value: Double, label: String?) : super(value.toFloat(), label) { valueDouble = value }
44-
45-
constructor(value: Double, label: String?, data: Any?) : super(value.toFloat(), label, data) { valueDouble = value }
46-
47-
constructor(value: Double, label: String?, icon: Drawable?) : super(value.toFloat(), label, icon) { valueDouble = value }
48-
49-
constructor(value: Double, label: String?, icon: Drawable?, data: Any?) : super(value.toFloat(), label, icon, data) { valueDouble = value }
50-
51-
/** Full-precision value (same as [valueDouble]). */
52-
val valuePrecise: Double get() = valueDouble
53-
54-
override fun copy(): PieEntryDouble = PieEntryDouble(valueDouble, label, data)
55-
56-
override fun toString(): String = "PieEntryDouble valueDouble=$valueDouble label=$label"
57-
}
58-
59-

chartLib/src/main/kotlin/info/appdev/charting/data/RadarEntry.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package info.appdev.charting.data
22

3-
@Deprecated("The replacement is RadarEntryFloat, or use RadarEntryDouble for higher precision. RadarEntry is retained for backward compatibility but will be removed in a future version.")
3+
@Deprecated(
4+
message = "The replacement is RadarEntryFloat, or use RadarEntryDouble for higher precision. RadarEntry is retained for backward compatibility but will be removed in a future version.",
5+
replaceWith = ReplaceWith("RadarEntryFloat", "info.appdev.charting.data.RadarEntryFloat")
6+
)
47
class RadarEntry : RadarEntryFloat {
58
constructor(value: Float) : super(value)
69

chartLib/src/main/kotlin/info/appdev/charting/data/RadarEntryDouble.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.annotation.SuppressLint
88
* Use [valueDouble] for full-precision access.
99
* Radar entries have no meaningful x-axis value.
1010
*/
11+
@Suppress("DEPRECATION")
1112
@SuppressLint("ParcelCreator")
1213
open class RadarEntryDouble : RadarEntryFloat {
1314

@@ -19,7 +20,6 @@ open class RadarEntryDouble : RadarEntryFloat {
1920

2021
@get:Deprecated("")
2122
@set:Deprecated("")
22-
@Suppress("DEPRECATION")
2323
override var x: Float
2424
get() = super.x
2525
set(x) { super.x = x }
@@ -36,4 +36,3 @@ open class RadarEntryDouble : RadarEntryFloat {
3636
override fun toString(): String = "RadarEntryDouble valueDouble=$valueDouble"
3737
}
3838

39-

lint/src/main/kotlin/info/appdev/charting/lint/EntryUsageDetector.kt

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.android.tools.lint.detector.api.Detector
55
import com.android.tools.lint.detector.api.Implementation
66
import com.android.tools.lint.detector.api.Issue
77
import com.android.tools.lint.detector.api.JavaContext
8+
import com.android.tools.lint.detector.api.LintFix
89
import com.android.tools.lint.detector.api.Scope
910
import com.android.tools.lint.detector.api.Severity
1011
import com.android.tools.lint.detector.api.SourceCodeScanner
@@ -15,27 +16,24 @@ import org.jetbrains.uast.UImportStatement
1516
import org.jetbrains.uast.getParentOfType
1617

1718
/**
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.
19+
* Lint detector that flags all usages of deprecated legacy entry classes and offers
20+
* auto-fixes to replace them with either the `*Float` or `*Double` equivalent.
2021
*
21-
* Covered classes → replacements:
22-
* - Entry → EntryFloat
23-
* - BarEntry → BarEntryFloat
24-
* - BubbleEntry → BubbleEntryFloat
25-
* - CandleEntry → CandleEntryFloat
26-
* - PieEntry → PieEntryFloat
27-
* - RadarEntry → RadarEntryFloat
22+
* Deprecated → Float replacement → Double replacement:
23+
* Entry → EntryFloat → EntryDouble
24+
* BarEntry → BarEntryFloat → BarEntryDouble
25+
* BubbleEntry → BubbleEntryFloat → BubbleEntryDouble
26+
* CandleEntry → CandleEntryFloat → CandleEntryDouble
27+
* PieEntry → PieEntryFloat → PieEntryDouble
28+
* RadarEntry → RadarEntryFloat → RadarEntryDouble
2829
*/
2930
class EntryUsageDetector : Detector(), SourceCodeScanner {
3031

3132
companion object {
3233
private const val PKG = "info.appdev.charting.data"
3334

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(
35+
/** Deprecated FQN → Float replacement FQN */
36+
private val FLOAT_REPLACEMENTS: Map<String, String> = mapOf(
3937
"$PKG.Entry" to "$PKG.EntryFloat",
4038
"$PKG.BarEntry" to "$PKG.BarEntryFloat",
4139
"$PKG.BubbleEntry" to "$PKG.BubbleEntryFloat",
@@ -44,15 +42,25 @@ class EntryUsageDetector : Detector(), SourceCodeScanner {
4442
"$PKG.RadarEntry" to "$PKG.RadarEntryFloat",
4543
)
4644

45+
/** Deprecated FQN → Double replacement FQN */
46+
private val DOUBLE_REPLACEMENTS: Map<String, String> = mapOf(
47+
"$PKG.Entry" to "$PKG.EntryDouble",
48+
"$PKG.BarEntry" to "$PKG.BarEntryDouble",
49+
"$PKG.BubbleEntry" to "$PKG.BubbleEntryDouble",
50+
"$PKG.CandleEntry" to "$PKG.CandleEntryDouble",
51+
"$PKG.PieEntry" to "$PKG.PieEntryDouble",
52+
"$PKG.RadarEntry" to "$PKG.RadarEntryDouble",
53+
)
54+
4755
@JvmField
4856
val ISSUE: Issue = Issue.create(
4957
id = "LegacyEntryUsage",
50-
briefDescription = "Replace deprecated legacy entry class with its `*Float` equivalent",
58+
briefDescription = "Replace deprecated legacy entry class with its `*Float` or `*Double` equivalent",
5159
explanation = """
5260
Several entry classes (`Entry`, `BarEntry`, `BubbleEntry`, `CandleEntry`, \
5361
`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.
62+
Replace them with their `*Float` equivalents for identical precision, or \
63+
`*Double` equivalents for higher precision.
5664
""",
5765
category = Category.CORRECTNESS,
5866
priority = 6,
@@ -63,14 +71,10 @@ class EntryUsageDetector : Detector(), SourceCodeScanner {
6371
)
6472
)
6573

66-
/** Pre-computed set of simple names for fast look-up in visitReference(). */
6774
private val DEPRECATED_SIMPLE_NAMES: Set<String> =
68-
REPLACEMENTS.keys.map { it.substringAfterLast('.') }.toSet()
75+
FLOAT_REPLACEMENTS.keys.map { it.substringAfterLast('.') }.toSet()
6976
}
7077

71-
// -----------------------------------------------------------------------
72-
// Tell Lint to invoke visitReference() for every one of these identifiers.
73-
// -----------------------------------------------------------------------
7478
override fun getApplicableReferenceNames(): List<String> =
7579
DEPRECATED_SIMPLE_NAMES.toList()
7680

@@ -81,39 +85,45 @@ class EntryUsageDetector : Detector(), SourceCodeScanner {
8185
) {
8286
if (referenced !is PsiClass) return
8387
val deprecatedFqn = referenced.qualifiedName ?: return
84-
val replacementFqn = REPLACEMENTS[deprecatedFqn] ?: return
88+
val floatFqn = FLOAT_REPLACEMENTS[deprecatedFqn] ?: return
89+
val doubleFqn = DOUBLE_REPLACEMENTS[deprecatedFqn] ?: return
8590

86-
val deprecatedSimple = deprecatedFqn.substringAfterLast('.')
87-
val replacementSimple = replacementFqn.substringAfterLast('.')
91+
val deprecatedSimple = deprecatedFqn.substringAfterLast('.')
92+
val floatSimple = floatFqn.substringAfterLast('.')
93+
val doubleSimple = doubleFqn.substringAfterLast('.')
8894

89-
// --- Import statement: replace the whole FQN ---
9095
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()
96+
val combinedFix: LintFix = if (importNode != null) {
97+
// For import statements replace the full FQN
98+
fix().alternatives(
99+
fix().replace().name("Replace with $floatSimple")
100+
.text(deprecatedFqn).with(floatFqn).autoFix().build(),
101+
fix().replace().name("Replace with $doubleSimple (high precision)")
102+
.text(deprecatedFqn).with(doubleFqn).autoFix().build()
103+
)
104+
} else {
105+
// For type refs / constructor calls replace only the simple name
106+
fix().alternatives(
107+
fix().replace().name("Replace with $floatSimple")
108+
.text(deprecatedSimple).with(floatSimple).autoFix().build(),
109+
fix().replace().name("Replace with $doubleSimple (high precision)")
110+
.text(deprecatedSimple).with(doubleSimple).autoFix().build()
102111
)
103-
return
104112
}
105113

106-
// --- All other references (type annotations, constructor calls, …) ---
114+
val location = if (importNode != null)
115+
context.getLocation(importNode)
116+
else
117+
context.getLocation(reference)
118+
119+
val element = importNode ?: reference
120+
107121
context.report(
108122
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()
123+
element,
124+
location,
125+
"Replace deprecated `$deprecatedSimple` — use `$floatSimple` (same precision) or `$doubleSimple` (higher precision)",
126+
combinedFix
117127
)
118128
}
119129
}

0 commit comments

Comments
 (0)