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

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

DirectBuildDatabaseDetector.kt82 lines · 3.8 KiBRaw
1package com.vayunmathur.lint
2
3import com.android.tools.lint.detector.api.Category
4import com.android.tools.lint.detector.api.Detector
5import com.android.tools.lint.detector.api.Implementation
6import com.android.tools.lint.detector.api.Issue
7import com.android.tools.lint.detector.api.JavaContext
8import com.android.tools.lint.detector.api.Scope
9import com.android.tools.lint.detector.api.Severity
10import com.android.tools.lint.detector.api.SourceCodeScanner
11import com.intellij.psi.PsiMethod
12import org.jetbrains.uast.UCallExpression
13import org.jetbrains.uast.getContainingUFile
14
15/**
16 * Forbids constructing a Room database directly via the shared
17 * `com.vayunmathur.library.room.buildDatabase(...)` (or `buildDatabaseUncached(...)`)
18 * helpers anywhere outside the `library` module.
19 *
20 * Every database must have a single application-scoped owner: subclass
21 * `com.vayunmathur.library.room.RoomRepository<DB>` and obtain data through the
22 * repository. Scattered `buildDatabase` calls across Activities, Services,
23 * Workers, Receivers, widgets, and assistant intents are exactly the anti-pattern
24 * this rule exists to prevent (duplicated DAO holders, no single source of truth,
25 * leaked component contexts).
26 */
27class DirectBuildDatabaseDetector : Detector(), SourceCodeScanner {
28
29 override fun getApplicableMethodNames(): List<String> =
30 listOf("buildDatabase", "buildDatabaseUncached")
31
32 override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
33 // Only flag the library's own helpers — not unrelated methods that happen
34 // to share the name (e.g. a private file-conversion helper).
35 val declaringPackage = context.evaluator.getPackage(method)?.qualifiedName
36 if (declaringPackage != LIBRARY_ROOM_PACKAGE) return
37
38 // The library itself (RoomRepository + the helpers) is allowed to call them.
39 val callSitePackage = node.getContainingUFile()?.packageName.orEmpty()
40 if (callSitePackage == LIBRARY_ROOM_PACKAGE || callSitePackage.startsWith("$LIBRARY_PACKAGE.")) {
41 return
42 }
43
44 context.report(
45 ISSUE,
46 node,
47 context.getLocation(node),
48 "Do not build databases directly. Subclass " +
49 "`com.vayunmathur.library.room.RoomRepository` and access data through the " +
50 "repository instead of calling `${method.name}(...)`.",
51 )
52 }
53
54 companion object {
55 private const val LIBRARY_PACKAGE = "com.vayunmathur.library"
56 private const val LIBRARY_ROOM_PACKAGE = "com.vayunmathur.library.room"
57
58 val ISSUE: Issue = Issue.create(
59 id = "DirectBuildDatabase",
60 briefDescription = "Database built outside a RoomRepository",
61 explanation = """
62 Databases must have a single application-scoped owner. Subclass \
63 `com.vayunmathur.library.room.RoomRepository<DB>` and expose the DAOs / \
64 Flows / suspend operations from it, then obtain that repository (as a \
65 process-wide singleton) from Activities, Services, Workers, Receivers, \
66 widgets, and assistant intents.
67
68 Calling `buildDatabase(...)` directly scatters database construction across \
69 the app, duplicates DAO holders, loses the single source of truth between \
70 the UI and background components, and risks leaking a component Context into \
71 a long-lived database instance.
72 """.trimIndent(),
73 category = Category.CORRECTNESS,
74 priority = 8,
75 severity = Severity.ERROR,
76 implementation = Implementation(
77 DirectBuildDatabaseDetector::class.java,
78 Scope.JAVA_FILE_SCOPE,
79 ),
80 )
81 }
82}