|
| 1 | +package com.firebaseui.lint |
| 2 | + |
| 3 | +import com.android.tools.lint.client.api.UElementHandler |
| 4 | +import com.android.tools.lint.detector.api.Category.Companion.CORRECTNESS |
| 5 | +import com.android.tools.lint.detector.api.Detector |
| 6 | +import com.android.tools.lint.detector.api.Implementation |
| 7 | +import com.android.tools.lint.detector.api.Issue |
| 8 | +import com.android.tools.lint.detector.api.JavaContext |
| 9 | +import com.android.tools.lint.detector.api.Scope |
| 10 | +import com.android.tools.lint.detector.api.Severity.WARNING |
| 11 | +import org.jetbrains.uast.UCallExpression |
| 12 | +import org.jetbrains.uast.UClass |
| 13 | +import org.jetbrains.uast.UField |
| 14 | +import org.jetbrains.uast.visitor.AbstractUastVisitor |
| 15 | +import java.util.EnumSet |
| 16 | + |
| 17 | +class FirestoreRecyclerAdapterLifecycleDetector : Detector(), Detector.UastScanner { |
| 18 | + |
| 19 | + override fun getApplicableUastTypes() = listOf(UClass::class.java) |
| 20 | + |
| 21 | + override fun createUastHandler(context: JavaContext) = MissingLifecycleMethodsVisitor(context) |
| 22 | + |
| 23 | + class MissingLifecycleMethodsVisitor( |
| 24 | + private val context: JavaContext |
| 25 | + ) : UElementHandler() { |
| 26 | + private val FIRESTORE_RECYCLER_ADAPTER_TYPE = |
| 27 | + "FirestoreRecyclerAdapter" |
| 28 | + |
| 29 | + override fun visitClass(node: UClass) { |
| 30 | + val adapterReferences = node |
| 31 | + .fields |
| 32 | + .filter { FIRESTORE_RECYCLER_ADAPTER_TYPE == it.type.canonicalText } |
| 33 | + .map { AdapterReference(it) } |
| 34 | + |
| 35 | + node.accept(AdapterStartListeningMethodVisitor(adapterReferences)) |
| 36 | + node.accept(AdapterStopListeningMethodVisitor(adapterReferences)) |
| 37 | + |
| 38 | + adapterReferences.forEach { |
| 39 | + if (it.hasCalledStart && !it.hasCalledStop) { |
| 40 | + context.report( |
| 41 | + ISSUE_MISSING_LISTENING_STOP_METHOD, |
| 42 | + it.uField, |
| 43 | + context.getLocation(it.uField), |
| 44 | + "Have called .startListening() without .stopListening()." |
| 45 | + ) |
| 46 | + } else if (!it.hasCalledStart) { |
| 47 | + context.report( |
| 48 | + ISSUE_MISSING_LISTENING_START_METHOD, |
| 49 | + it.uField, |
| 50 | + context.getLocation(it.uField), |
| 51 | + "Have not called .startListening()." |
| 52 | + ) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + class AdapterStartListeningMethodVisitor( |
| 59 | + private val adapterReferences: List<AdapterReference> |
| 60 | + ) : AbstractUastVisitor() { |
| 61 | + private val START_LISTENING_METHOD_NAME = "startListening" |
| 62 | + |
| 63 | + override fun visitCallExpression(node: UCallExpression): Boolean = |
| 64 | + if (START_LISTENING_METHOD_NAME == node.methodName) { |
| 65 | + adapterReferences |
| 66 | + .find { it.uField.name == node.receiver?.asRenderString() } |
| 67 | + ?.let { |
| 68 | + it.hasCalledStart = true |
| 69 | + } |
| 70 | + true |
| 71 | + } else { |
| 72 | + super.visitCallExpression(node) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + class AdapterStopListeningMethodVisitor( |
| 77 | + private val adapterReferences: List<AdapterReference> |
| 78 | + ) : AbstractUastVisitor() { |
| 79 | + private val STOP_LISTENING_METHOD_NAME = "stopListening" |
| 80 | + |
| 81 | + override fun visitCallExpression(node: UCallExpression): Boolean = |
| 82 | + if (STOP_LISTENING_METHOD_NAME == node.methodName) { |
| 83 | + adapterReferences |
| 84 | + .find { it.uField.name == node.receiver?.asRenderString() } |
| 85 | + ?.let { |
| 86 | + it.hasCalledStop = true |
| 87 | + } |
| 88 | + true |
| 89 | + } else { |
| 90 | + super.visitCallExpression(node) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + companion object { |
| 95 | + val ISSUE_MISSING_LISTENING_START_METHOD = Issue.create( |
| 96 | + "FirestoreAdapterMissingStartListeningMethod", |
| 97 | + "Checks if FirestoreAdapter has called .startListening() method.", |
| 98 | + "If a class is using a FirestoreAdapter and does not call startListening it won't be " + |
| 99 | + "notified on changes.", |
| 100 | + CORRECTNESS, 10, WARNING, |
| 101 | + Implementation( |
| 102 | + FirestoreRecyclerAdapterLifecycleDetector::class.java, |
| 103 | + EnumSet.of(Scope.JAVA_FILE) |
| 104 | + ) |
| 105 | + ) |
| 106 | + |
| 107 | + val ISSUE_MISSING_LISTENING_STOP_METHOD = Issue.create( |
| 108 | + "FirestoreAdapterMissingStopListeningMethod", |
| 109 | + "Checks if FirestoreAdapter has called .stopListening() method.", |
| 110 | + "If a class is using a FirestoreAdapter and has called .startListening() but missing " + |
| 111 | + " .stopListening() might cause issues with RecyclerView data changes.", |
| 112 | + CORRECTNESS, 10, WARNING, |
| 113 | + Implementation( |
| 114 | + FirestoreRecyclerAdapterLifecycleDetector::class.java, |
| 115 | + EnumSet.of(Scope.JAVA_FILE) |
| 116 | + ) |
| 117 | + ) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +data class AdapterReference( |
| 122 | + val uField: UField, |
| 123 | + var hasCalledStart: Boolean = false, |
| 124 | + var hasCalledStop: Boolean = false |
| 125 | +) |
0 commit comments