-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Firestore adapter lifecycle lint checks #1340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
samtstern
merged 5 commits into
firebase:version-4.1.0-dev
from
pavlospt:firestore_adapter_lifecycle_lint_checks
Jun 12, 2018
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7dcef2b
Move Lint testing helper methods to separate file.
pavlospt 57f2d4d
Introduce FirestoreRecyclerAdapterLifecycleDetector
pavlospt 0689300
Add tests for FirestoreRecyclerAdapterLifecycleDetector
pavlospt cc81652
Remove wrongly commited file
pavlospt e1c7424
Remove setLifecycleOwner related Lint checks
pavlospt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
internal/lint/src/main/java/com/firebaseui/lint/FirestoreRecyclerAdapterLifecycleDetector.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.firebaseui.lint | ||
|
||
import com.android.tools.lint.client.api.UElementHandler | ||
import com.android.tools.lint.detector.api.Category.Companion.CORRECTNESS | ||
import com.android.tools.lint.detector.api.Detector | ||
import com.android.tools.lint.detector.api.Implementation | ||
import com.android.tools.lint.detector.api.Issue | ||
import com.android.tools.lint.detector.api.JavaContext | ||
import com.android.tools.lint.detector.api.Scope | ||
import com.android.tools.lint.detector.api.Severity.WARNING | ||
import org.jetbrains.uast.UCallExpression | ||
import org.jetbrains.uast.UClass | ||
import org.jetbrains.uast.UField | ||
import org.jetbrains.uast.visitor.AbstractUastVisitor | ||
import java.util.EnumSet | ||
|
||
class FirestoreRecyclerAdapterLifecycleDetector : Detector(), Detector.UastScanner { | ||
|
||
override fun getApplicableUastTypes() = listOf(UClass::class.java) | ||
|
||
override fun createUastHandler(context: JavaContext) = MissingLifecycleMethodsVisitor(context) | ||
|
||
class MissingLifecycleMethodsVisitor( | ||
private val context: JavaContext | ||
) : UElementHandler() { | ||
private val FIRESTORE_RECYCLER_ADAPTER_TYPE = | ||
"FirestoreRecyclerAdapter" | ||
|
||
override fun visitClass(node: UClass) { | ||
val adapterReferences = node | ||
.fields | ||
.filter { FIRESTORE_RECYCLER_ADAPTER_TYPE == it.type.canonicalText } | ||
.map { AdapterReference(it) } | ||
|
||
node.accept(AdapterStartListeningMethodVisitor(adapterReferences)) | ||
node.accept(AdapterStopListeningMethodVisitor(adapterReferences)) | ||
|
||
adapterReferences.forEach { | ||
if (it.hasCalledStart && !it.hasCalledStop) { | ||
context.report( | ||
ISSUE_MISSING_LISTENING_STOP_METHOD, | ||
it.uField, | ||
context.getLocation(it.uField), | ||
"Have called .startListening() without .stopListening()." | ||
) | ||
} else if (!it.hasCalledStart) { | ||
context.report( | ||
ISSUE_MISSING_LISTENING_START_METHOD, | ||
it.uField, | ||
context.getLocation(it.uField), | ||
"Have not called .startListening()." | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
class AdapterStartListeningMethodVisitor( | ||
private val adapterReferences: List<AdapterReference> | ||
) : AbstractUastVisitor() { | ||
private val START_LISTENING_METHOD_NAME = "startListening" | ||
|
||
override fun visitCallExpression(node: UCallExpression): Boolean = | ||
if (START_LISTENING_METHOD_NAME == node.methodName) { | ||
adapterReferences | ||
.find { it.uField.name == node.receiver?.asRenderString() } | ||
?.let { | ||
it.hasCalledStart = true | ||
} | ||
true | ||
} else { | ||
super.visitCallExpression(node) | ||
} | ||
} | ||
|
||
class AdapterStopListeningMethodVisitor( | ||
private val adapterReferences: List<AdapterReference> | ||
) : AbstractUastVisitor() { | ||
private val STOP_LISTENING_METHOD_NAME = "stopListening" | ||
|
||
override fun visitCallExpression(node: UCallExpression): Boolean = | ||
if (STOP_LISTENING_METHOD_NAME == node.methodName) { | ||
adapterReferences | ||
.find { it.uField.name == node.receiver?.asRenderString() } | ||
?.let { | ||
it.hasCalledStop = true | ||
} | ||
true | ||
} else { | ||
super.visitCallExpression(node) | ||
} | ||
} | ||
|
||
companion object { | ||
val ISSUE_MISSING_LISTENING_START_METHOD = Issue.create( | ||
"FirestoreAdapterMissingStartListeningMethod", | ||
"Checks if FirestoreAdapter has called .startListening() method.", | ||
"If a class is using a FirestoreAdapter and does not call startListening it won't be " + | ||
"notified on changes.", | ||
CORRECTNESS, 10, WARNING, | ||
Implementation( | ||
FirestoreRecyclerAdapterLifecycleDetector::class.java, | ||
EnumSet.of(Scope.JAVA_FILE) | ||
) | ||
) | ||
|
||
val ISSUE_MISSING_LISTENING_STOP_METHOD = Issue.create( | ||
"FirestoreAdapterMissingStopListeningMethod", | ||
"Checks if FirestoreAdapter has called .stopListening() method.", | ||
"If a class is using a FirestoreAdapter and has called .startListening() but missing " + | ||
" .stopListening() might cause issues with RecyclerView data changes.", | ||
CORRECTNESS, 10, WARNING, | ||
Implementation( | ||
FirestoreRecyclerAdapterLifecycleDetector::class.java, | ||
EnumSet.of(Scope.JAVA_FILE) | ||
) | ||
) | ||
} | ||
} | ||
|
||
data class AdapterReference( | ||
val uField: UField, | ||
var hasCalledStart: Boolean = false, | ||
var hasCalledStop: Boolean = false | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...l/lint/src/test/java/com/firebaseui/lint/FirestoreRecyclerAdapterLifecycleDetectorTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.firebaseui.lint | ||
|
||
import com.android.tools.lint.checks.infrastructure.TestFiles.java | ||
import com.firebaseui.lint.FirestoreRecyclerAdapterLifecycleDetector.Companion.ISSUE_MISSING_LISTENING_START_METHOD | ||
import com.firebaseui.lint.FirestoreRecyclerAdapterLifecycleDetector.Companion.ISSUE_MISSING_LISTENING_STOP_METHOD | ||
import com.firebaseui.lint.LintTestHelper.configuredLint | ||
import org.junit.Test | ||
|
||
class FirestoreRecyclerAdapterLifecycleDetectorTest { | ||
|
||
@Test | ||
fun `Checks missing startListening() method call`() { | ||
configuredLint() | ||
.files(java(""" | ||
|public class MissingStartListeningMethodCall { | ||
| private FirestoreRecyclerAdapter adapter; | ||
|} | ||
""".trimMargin())) | ||
.issues(ISSUE_MISSING_LISTENING_START_METHOD) | ||
.run() | ||
.expect(""" | ||
|src/MissingStartListeningMethodCall.java:2: Warning: Have not called .startListening(). [FirestoreAdapterMissingStartListeningMethod] | ||
| private FirestoreRecyclerAdapter adapter; | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|0 errors, 1 warnings | ||
""".trimMargin()) | ||
} | ||
|
||
@Test | ||
fun `Checks missing stopListening() method call`() { | ||
configuredLint() | ||
.files(java(""" | ||
|public class MissingStopListeningMethodCall { | ||
| private FirestoreRecyclerAdapter adapter; | ||
| | ||
| public void onStart() { | ||
| adapter.startListening(); | ||
| } | ||
|} | ||
""".trimMargin())) | ||
.issues(ISSUE_MISSING_LISTENING_STOP_METHOD) | ||
.run() | ||
.expect(""" | ||
|src/MissingStopListeningMethodCall.java:2: Warning: Have called .startListening() without .stopListening(). [FirestoreAdapterMissingStopListeningMethod] | ||
| private FirestoreRecyclerAdapter adapter; | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|0 errors, 1 warnings | ||
""".trimMargin()) | ||
} | ||
|
||
@Test | ||
fun `Checks no warnings when startListening & stopListening methods called`() { | ||
configuredLint() | ||
.files(java(""" | ||
|public class HasCalledStartStopListeningMethods { | ||
| private FirestoreRecyclerAdapter adapter; | ||
| | ||
| public void onStart() { | ||
| adapter.startListening(); | ||
| } | ||
| | ||
| public void onStop() { | ||
| adapter.stopListening(); | ||
| } | ||
|} | ||
""".trimMargin())) | ||
.issues(ISSUE_MISSING_LISTENING_START_METHOD, ISSUE_MISSING_LISTENING_STOP_METHOD) | ||
.run() | ||
.expectClean() | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
internal/lint/src/test/java/com/firebaseui/lint/LintTestHelper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.firebaseui.lint | ||
|
||
import com.android.tools.lint.checks.infrastructure.TestLintTask | ||
import java.io.File | ||
|
||
object LintTestHelper { | ||
// Nasty hack to make lint tests pass on Windows. For some reason, lint doesn't | ||
// automatically find the Android SDK in its standard path on Windows. This hack looks | ||
// through the system properties to find the path defined in `local.properties` and then | ||
// sets lint's SDK home to that path if it's found. | ||
private val sdkPath = System.getProperty("java.library.path").split(';').find { | ||
it.contains("SDK", true) | ||
} | ||
|
||
fun configuredLint(): TestLintTask = TestLintTask.lint().apply { | ||
sdkHome(File(sdkPath ?: return@apply)) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this trigger if
startListening
is not called butsetLifecycleOwner
is called?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope have not taken that into account, but I was thinking of it as well. I am not quite sure on the proper approach to solve this.
Currently
setLifecycleOwner
is applied onFirestoreRecyclerOptions
which should be applied onFirestoreRecyclerAdapter
, but there is nothing stopping the user of using both that method, as well as, thestart/stopListening
ones. So I am not quite sure how to prioritize those two together and chose to just add warnings based on the general usage and existence of lifecycle method invocations. That lets the user decide which warning to suppress, effectively acknowledging that it is the desired behavior and not just a method call that they forgot to invoke.@samtstern I am open to suggestions on the matter if you have anything to suggest :D
Edit 1: I would probably turn the invocation of
startListening
aftersetLifecycleOwner
has been called, into aRuntimeException
so as to give a clear option on the user. Not sure if it should just be a Lint check since in the future those two method calls might conflict in an unpredictable way!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think (as @SUPERCILEX mentioned) understanding lifecycle in a lint could be very tricky. Would you be ok with reducing this PR down to just the
start
/stop
calls and not the lifecycle bits?We'd also have to do the work to ship our custom lint checks, which may be kind of slow, but we don't have to do that in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure I can do that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samtstern I removed
setLifecyleOwner
related Lint checks and code :)