Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / lint-rules / src / main / java / com / vayunmathur / lint / Room2UsageDetector.kt

Room2UsageDetector.kt102 lines · 5.1 KiBRaw
1package com.vayunmathur.lint
2
3import com.android.tools.lint.client.api.UElementHandler
4import com.android.tools.lint.detector.api.Category
5import com.android.tools.lint.detector.api.Detector
6import com.android.tools.lint.detector.api.Implementation
7import com.android.tools.lint.detector.api.Issue
8import com.android.tools.lint.detector.api.JavaContext
9import com.android.tools.lint.detector.api.Location
10import com.android.tools.lint.detector.api.Scope
11import com.android.tools.lint.detector.api.Severity
12import com.android.tools.lint.detector.api.SourceCodeScanner
13import org.jetbrains.kotlin.psi.KtFile
14import org.jetbrains.uast.UElement
15import org.jetbrains.uast.UFile
16
17/**
18 * Forbids first-party use of Room 2 (`androidx.room.*`). Room 3 (`androidx.room3.*`) is the
19 * only version this repo targets; Room 2 is in maintenance mode and its `SupportSQLite` API
20 * has no SQLCipher driver path.
21 *
22 * Room 2 stays in the *runtime* classpath regardless, because `androidx.work:work-runtime`
23 * depends on it for its internal `WorkDatabase` and 13 modules use WorkManager. That is
24 * harmless — Room 3 took a new maven group and package precisely so the two can coexist — so
25 * the achievable goal, and what this rule enforces, is zero *first-party* Room 2 usage.
26 *
27 * Matching is done on the file's source text rather than on resolved imports or method calls,
28 * because several databases reference Room 2 by fully-qualified name with no import at all
29 * (`object : androidx.room.migration.Migration(1, 2)`), which an import-only or
30 * `getApplicableMethodNames()` check would miss entirely.
31 *
32 * Matches inside comments (including KDoc) are skipped, so a doc comment that
33 * mentions `androidx.room.` while explaining the Room 2 → Room 3 migration does
34 * not break the build.
35 */
36class Room2UsageDetector : Detector(), SourceCodeScanner {
37
38 override fun getApplicableUastTypes(): List<Class<out UElement>> =
39 listOf(UFile::class.java)
40
41 override fun createUastHandler(context: JavaContext): UElementHandler =
42 object : UElementHandler() {
43 override fun visitFile(node: UFile) {
44 val source = node.sourcePsi?.text ?: return
45 val file = context.file ?: return
46 // Skip matches inside comments/KDoc: a doc comment explaining the
47 // Room 2 -> Room 3 migration must not break the build.
48 val ktFile = node.sourcePsi as? KtFile
49 val commentRanges = ktFile?.let { CommentOnlyMatcher.commentRangesIn(it) }.orEmpty()
50
51 var from = source.indexOf(ROOM2_PREFIX)
52 while (from >= 0) {
53 if (!CommentOnlyMatcher.isInRanges(from, commentRanges)) {
54 context.report(
55 ISSUE,
56 node,
57 Location.create(file, source, from, from + ROOM2_PREFIX.length),
58 "Room 2 is not used in this repo. Replace `androidx.room.` with " +
59 "`androidx.room3.` (`@TypeConverter`/`@TypeConverters` became " +
60 "`@ColumnTypeConverter`/`@ColumnTypeConverters`, and " +
61 "`RoomDatabase.withTransaction` became `withWriteTransaction`).",
62 )
63 }
64 from = source.indexOf(ROOM2_PREFIX, from + ROOM2_PREFIX.length)
65 }
66 }
67 }
68
69 companion object {
70 // The trailing dot is what keeps `androidx.room3.` from matching.
71 private const val ROOM2_PREFIX = "androidx.room."
72
73 val ISSUE: Issue = Issue.create(
74 id = "Room2Usage",
75 briefDescription = "Room 2 (androidx.room) used instead of Room 3",
76 explanation = """
77 This repo is on Room 3 (`androidx.room3`). Room 2 (`androidx.room`) is in \
78 maintenance mode and, more importantly, its `SupportSQLite` API is the one \
79 Room 3 dropped — `openHelperFactory(...)` no longer exists, so the SQLCipher \
80 encryption used by every database here is wired through \
81 `setDriver(SQLCipherDriver(...))` instead.
82
83 Replace `androidx.room.` with `androidx.room3.`. Three renames are not \
84 mechanical: `@TypeConverter` and `@TypeConverters` are `@ColumnTypeConverter` \
85 and `@ColumnTypeConverters`; `RoomDatabase.withTransaction { }` is \
86 `withWriteTransaction { }` (or `withReadTransaction`); and `Migration.migrate` \
87 takes a suspending `androidx.sqlite.SQLiteConnection` rather than a \
88 `SupportSQLiteDatabase`.
89
90 Room 2 remaining on the runtime classpath via `androidx.work:work-runtime` is \
91 expected and must not be excluded — WorkManager needs it for `WorkDatabase`.
92 """.trimIndent(),
93 category = Category.CORRECTNESS,
94 priority = 8,
95 severity = Severity.ERROR,
96 implementation = Implementation(
97 Room2UsageDetector::class.java,
98 Scope.JAVA_FILE_SCOPE,
99 ),
100 )
101 }
102}