Skip to content

Commit

Permalink
Move Dagger spi model types implementation into Dagger internal.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 559210781
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Aug 22, 2023
1 parent f8334d8 commit 8d2385f
Show file tree
Hide file tree
Showing 13 changed files with 321 additions and 302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.google.auto.service.AutoService
import com.google.common.graph.EndpointPair
import com.google.common.graph.ImmutableNetwork
import dagger.hilt.android.processor.internal.AndroidClassNames
import dagger.hilt.processor.internal.asElement
import dagger.hilt.processor.internal.getQualifiedName
import dagger.hilt.processor.internal.hasAnnotation
import dagger.spi.model.Binding
Expand Down Expand Up @@ -48,8 +47,7 @@ class ViewModelValidationPlugin : BindingGraphPlugin {
val target: Node = pair.target()
val source: Node = pair.source()
if (
target is Binding &&
isHiltViewModelBinding(target) && !isInternalHiltViewModelUsage(source)
target is Binding && isHiltViewModelBinding(target) && !isInternalHiltViewModelUsage(source)
) {
diagnosticReporter.reportDependency(
Kind.ERROR,
Expand All @@ -67,7 +65,7 @@ class ViewModelValidationPlugin : BindingGraphPlugin {
// Make sure this is from an @Inject constructor rather than an overridden binding like an
// @Provides and that the class is annotated with @HiltViewModel.
return target.kind() == BindingKind.INJECTION &&
target.key().type().asElement().hasAnnotation(AndroidClassNames.HILT_VIEW_MODEL)
target.key().type().hasAnnotation(AndroidClassNames.HILT_VIEW_MODEL)
}

private fun isInternalHiltViewModelUsage(source: Node): Boolean {
Expand Down
12 changes: 9 additions & 3 deletions java/dagger/hilt/processor/internal/DaggerModels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,31 @@ package dagger.hilt.processor.internal

import com.google.auto.common.MoreTypes
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSDeclaration
import com.squareup.javapoet.ClassName
import dagger.spi.model.DaggerAnnotation
import dagger.spi.model.DaggerElement
import dagger.spi.model.DaggerProcessingEnv
import dagger.spi.model.DaggerType


fun DaggerType.asElement(): DaggerElement =
fun DaggerType.hasAnnotation(className: ClassName): Boolean =
when (checkNotNull(backend())) {
DaggerProcessingEnv.Backend.JAVAC -> {
val javaType = checkNotNull(java())
DaggerElement.fromJavac(MoreTypes.asElement(javaType))
Processors.hasAnnotation(MoreTypes.asTypeElement(javaType), className)
}
DaggerProcessingEnv.Backend.KSP -> {
val kspType = checkNotNull(ksp())
DaggerElement.fromKsp(kspType.declaration)
kspType.declaration.hasAnnotation(className.canonicalName())
}
}

fun KSDeclaration.hasAnnotation(annotationName: String): Boolean =
annotations.any {
it.annotationType.resolve().declaration.qualifiedName?.asString().equals(annotationName)
}

fun DaggerElement.hasAnnotation(className: ClassName) =
when (checkNotNull(backend())) {
DaggerProcessingEnv.Backend.JAVAC -> {
Expand Down
1 change: 0 additions & 1 deletion java/dagger/internal/codegen/validation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ java_library(
"//java/dagger/internal/codegen/model",
"//java/dagger/internal/codegen/xprocessing",
"//java/dagger/spi",
"//third_party/java/auto:common",
"//third_party/java/auto:value",
"//third_party/java/checker_framework_annotations",
"//third_party/java/error_prone:annotations",
Expand Down
Loading

0 comments on commit 8d2385f

Please sign in to comment.