![JAR search and dependency download from the Maven repository](/logo.png)
schwarz.it.lightsaber.checkers.EmptyComponents.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lightsaber Show documentation
Show all versions of lightsaber Show documentation
A Dagger 2 plugin to find unused dependencies declared in your Modules and Components.
package schwarz.it.lightsaber.checkers
import dagger.spi.model.BindingGraph
import dagger.spi.model.DaggerProcessingEnv
import schwarz.it.lightsaber.CodePosition
import schwarz.it.lightsaber.Finding
import schwarz.it.lightsaber.domain.hasSuppress
import schwarz.it.lightsaber.getCodePosition
import schwarz.it.lightsaber.toCodePosition
import schwarz.it.lightsaber.utils.fold
import schwarz.it.lightsaber.utils.getElements
import schwarz.it.lightsaber.utils.getFullQualifiedName
import schwarz.it.lightsaber.utils.getMethods
import schwarz.it.lightsaber.utils.getTypes
import javax.lang.model.element.ExecutableElement
import javax.lang.model.element.TypeElement
import javax.lang.model.util.Types
internal fun checkEmptyComponents(
bindingGraph: BindingGraph,
daggerProcessingEnv: DaggerProcessingEnv,
): List {
return bindingGraph.componentNodes()
.filter { componentNode ->
componentNode.componentPath().currentComponent().fold(
{ it.getAllFunctions(daggerProcessingEnv.getTypes()).none() },
{ classDeclaration ->
val hasNoDeclaredFunctions = classDeclaration
.getAllFunctions()
.none { it.simpleName.asString() !in kspDefaultDeclaredFunctions }
val hasNoDeclaredProperties = classDeclaration
.getAllProperties()
.none()
hasNoDeclaredFunctions && hasNoDeclaredProperties
},
)
}
.map {
val annotation = if (it.isSubcomponent) {
"@Subcomponent"
} else {
"@Component"
}
Finding(
"The $annotation `${it.getFullQualifiedName()}` is empty and could be removed.",
it.getCodePosition(daggerProcessingEnv),
it.componentPath().currentComponent()::hasSuppress,
)
}
}
private fun TypeElement.getAllFunctions(types: Types): Sequence {
return sequence {
yieldAll(getMethods())
yieldAll(interfaces.asSequence().flatMap { (types.asElement(it) as TypeElement).getAllFunctions(types) })
yieldAll((types.asElement(superclass) as TypeElement?)?.getAllFunctions(types).orEmpty())
}
}
private val kspDefaultDeclaredFunctions = listOf(
"equals",
"hashCode",
"toString",
)
private fun BindingGraph.ComponentNode.getCodePosition(daggerProcessingEnv: DaggerProcessingEnv): CodePosition {
return componentPath().currentComponent().fold(
{
daggerProcessingEnv.getElements().getCodePosition(it)
},
{
it.location.toCodePosition()
},
)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy