-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a rule to suggest constructor injection over Provides method
- Loading branch information
1 parent
0522efa
commit d90f90f
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
67 changes: 67 additions & 0 deletions
67
...main/java/dev/whosnickdoglio/dagger/detectors/ConstructorInjectionOverProvidesDetector.kt
This file contains 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,67 @@ | ||
/* | ||
* Copyright (C) 2024 Nicholas Doglio | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
package dev.whosnickdoglio.dagger.detectors | ||
|
||
import com.android.tools.lint.client.api.UElementHandler | ||
import com.android.tools.lint.detector.api.Category | ||
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 | ||
import com.android.tools.lint.detector.api.SourceCodeScanner | ||
import com.android.tools.lint.detector.api.TextFormat | ||
import dev.whosnickdoglio.lint.annotations.dagger.PROVIDES | ||
import org.jetbrains.uast.UAnnotation | ||
import org.jetbrains.uast.UCallExpression | ||
import org.jetbrains.uast.UElement | ||
import org.jetbrains.uast.util.isConstructorCall | ||
|
||
internal class ConstructorInjectionOverProvidesDetector : Detector(), SourceCodeScanner { | ||
|
||
override fun getApplicableUastTypes(): List<Class<out UElement>> = | ||
listOf(UAnnotation::class.java) | ||
|
||
override fun createUastHandler(context: JavaContext): UElementHandler = | ||
object : UElementHandler() { | ||
override fun visitAnnotation(node: UAnnotation) { | ||
if (node.qualifiedName == PROVIDES) { | ||
val method = node.uastParent as? UCallExpression ?: return | ||
if (method.isConstructorCall()) { | ||
context.report( | ||
issue = ISSUE, | ||
location = context.getLocation(method), | ||
message = ISSUE.getExplanation(TextFormat.TEXT), | ||
Check warning on line 37 in lint/dagger/src/main/java/dev/whosnickdoglio/dagger/detectors/ConstructorInjectionOverProvidesDetector.kt Codecov / codecov/patchlint/dagger/src/main/java/dev/whosnickdoglio/dagger/detectors/ConstructorInjectionOverProvidesDetector.kt#L34-L37
|
||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
private val implementation = | ||
Implementation( | ||
ConstructorInjectionOverProvidesDetector::class.java, | ||
Scope.JAVA_FILE_SCOPE, | ||
) | ||
val ISSUE = | ||
Issue.create( | ||
id = "ConstructorInjectionOverProvidesMethods", | ||
briefDescription = "@Provides method used instead of constructor injection", | ||
explanation = | ||
""" | ||
`@Provides` methods are great for adding third party libraries or classes that require Builders or Factories \ | ||
to the Dagger graph but for classes with simple constructors you should just add a `@Inject` annotation to the constructor | ||
""", | ||
category = Category.CORRECTNESS, | ||
priority = 5, | ||
severity = Severity.WARNING, | ||
implementation = implementation, | ||
) | ||
.setEnabledByDefault(false) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
.../java/dev/whosnickdoglio/dagger/detectors/ConstructorInjectionOverProvidesDetectorTest.kt
This file contains 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,47 @@ | ||
/* | ||
* Copyright (C) 2024 Nicholas Doglio | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
package dev.whosnickdoglio.dagger.detectors | ||
|
||
import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin | ||
import com.android.tools.lint.checks.infrastructure.TestLintTask.lint | ||
import dev.whosnickdoglio.stubs.daggerAnnotations | ||
import org.junit.Test | ||
|
||
class ConstructorInjectionOverProvidesDetectorTest { | ||
|
||
// TODO java and companion object tests | ||
// TODO multiple @Provides | ||
|
||
@Test | ||
fun `class that could use constructor injection triggers provides warning`() { | ||
lint() | ||
.files( | ||
daggerAnnotations, | ||
kotlin( | ||
""" | ||
package com.test.android | ||
import dagger.Provides | ||
import dagger.Module | ||
class MyClass | ||
@Module | ||
object MyModule { | ||
@Provides | ||
fun provideMyClass(): MyClass { | ||
return MyClass() | ||
} | ||
} | ||
""" | ||
) | ||
.indented(), | ||
) | ||
.issues(ConstructorInjectionOverProvidesDetector.ISSUE) | ||
.run() | ||
.expectClean() | ||
} | ||
} |