diff --git a/java/dagger/hilt/android/processor/internal/viewmodel/ViewModelValidationPlugin.kt b/java/dagger/hilt/android/processor/internal/viewmodel/ViewModelValidationPlugin.kt index d5c3666b37d..0c472d708cf 100644 --- a/java/dagger/hilt/android/processor/internal/viewmodel/ViewModelValidationPlugin.kt +++ b/java/dagger/hilt/android/processor/internal/viewmodel/ViewModelValidationPlugin.kt @@ -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 @@ -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, @@ -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 { diff --git a/java/dagger/hilt/processor/internal/DaggerModels.kt b/java/dagger/hilt/processor/internal/DaggerModels.kt index 7d64d23969f..66b076a8cb8 100644 --- a/java/dagger/hilt/processor/internal/DaggerModels.kt +++ b/java/dagger/hilt/processor/internal/DaggerModels.kt @@ -18,6 +18,7 @@ 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 @@ -25,18 +26,23 @@ 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 -> { diff --git a/java/dagger/internal/codegen/validation/BUILD b/java/dagger/internal/codegen/validation/BUILD index cc36708433f..a24b7e0bfed 100644 --- a/java/dagger/internal/codegen/validation/BUILD +++ b/java/dagger/internal/codegen/validation/BUILD @@ -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", diff --git a/java/dagger/internal/codegen/validation/SpiModelBindingGraphConverter.java b/java/dagger/internal/codegen/validation/SpiModelBindingGraphConverter.java index 75d0f7f99a9..396afc20131 100644 --- a/java/dagger/internal/codegen/validation/SpiModelBindingGraphConverter.java +++ b/java/dagger/internal/codegen/validation/SpiModelBindingGraphConverter.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.validation; +import static androidx.room.compiler.processing.compat.XConverters.getProcessingEnv; import static androidx.room.compiler.processing.compat.XConverters.toJavac; import static androidx.room.compiler.processing.compat.XConverters.toKS; import static androidx.room.compiler.processing.compat.XConverters.toKSResolver; @@ -31,6 +32,7 @@ import androidx.room.compiler.processing.XTypeElement; import com.google.auto.value.AutoValue; import com.google.auto.value.extension.memoized.Memoized; +import com.google.common.base.Equivalence; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSetMultimap; import com.google.common.graph.EndpointPair; @@ -38,7 +40,16 @@ import com.google.common.graph.MutableNetwork; import com.google.common.graph.Network; import com.google.common.graph.NetworkBuilder; +import com.google.devtools.ksp.processing.Resolver; +import com.google.devtools.ksp.processing.SymbolProcessorEnvironment; +import com.google.devtools.ksp.symbol.KSAnnotated; +import com.google.devtools.ksp.symbol.KSAnnotation; +import com.google.devtools.ksp.symbol.KSClassDeclaration; +import com.google.devtools.ksp.symbol.KSFunctionDeclaration; +import com.google.devtools.ksp.symbol.KSType; +import dagger.internal.codegen.xprocessing.XAnnotations; import dagger.internal.codegen.xprocessing.XElements; +import dagger.internal.codegen.xprocessing.XTypes; import dagger.spi.model.Binding; import dagger.spi.model.BindingGraph; import dagger.spi.model.BindingGraph.ChildFactoryMethodEdge; @@ -64,6 +75,12 @@ import dagger.spi.model.RequestKind; import dagger.spi.model.Scope; import java.util.Optional; +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.Element; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; +import javax.lang.model.type.TypeMirror; import javax.tools.Diagnostic; /** A Utility class for converting to the {@link BindingGraph} used by external plugins. */ @@ -141,19 +158,20 @@ private static Edge toSpiModel( } } - private static Key toSpiModel(dagger.internal.codegen.model.Key key, XProcessingEnv env) { + private static Key toSpiModel(dagger.internal.codegen.model.Key key) { Key.Builder builder = - Key.builder(toSpiModel(key.type().xprocessing(), env)) - .qualifier(key.qualifier().map(qualifier -> toSpiModel(qualifier.xprocessing(), env))); + Key.builder(toSpiModel(key.type().xprocessing())) + .qualifier(key.qualifier().map(qualifier -> toSpiModel(qualifier.xprocessing()))); if (key.multibindingContributionIdentifier().isPresent()) { return builder .multibindingContributionIdentifier( toSpiModel( - key.multibindingContributionIdentifier().get().contributingModule().xprocessing(), - env), + key.multibindingContributionIdentifier() + .get() + .contributingModule() + .xprocessing()), toSpiModel( - key.multibindingContributionIdentifier().get().bindingMethod().xprocessing(), - env)) + key.multibindingContributionIdentifier().get().bindingMethod().xprocessing())) .build(); } return builder.build().withoutMultibindingContributionIdentifier(); @@ -169,98 +187,50 @@ private static RequestKind toSpiModel(dagger.internal.codegen.model.RequestKind @SuppressWarnings("CheckReturnValue") private static DependencyRequest toSpiModel( - dagger.internal.codegen.model.DependencyRequest request, XProcessingEnv env) { + dagger.internal.codegen.model.DependencyRequest request) { DependencyRequest.Builder builder = DependencyRequest.builder() .kind(toSpiModel(request.kind())) - .key(toSpiModel(request.key(), env)) + .key(toSpiModel(request.key())) .isNullable(request.isNullable()); - request - .requestElement() - .ifPresent(e -> builder.requestElement(toSpiModel(e.xprocessing(), env))); + request.requestElement().ifPresent(e -> builder.requestElement(toSpiModel(e.xprocessing()))); return builder.build(); } - private static Scope toSpiModel(dagger.internal.codegen.model.Scope scope, XProcessingEnv env) { - return Scope.scope(toSpiModel(scope.scopeAnnotation().xprocessing(), env)); + private static Scope toSpiModel(dagger.internal.codegen.model.Scope scope) { + return Scope.scope(toSpiModel(scope.scopeAnnotation().xprocessing())); } - private static ComponentPath toSpiModel( - dagger.internal.codegen.model.ComponentPath path, XProcessingEnv env) { + private static ComponentPath toSpiModel(dagger.internal.codegen.model.ComponentPath path) { return ComponentPath.create( path.components().stream() - .map(component -> toSpiModel(component.xprocessing(), env)) + .map(component -> toSpiModel(component.xprocessing())) .collect(toImmutableList())); } - private static DaggerTypeElement toSpiModel(XTypeElement typeElement, XProcessingEnv env) { - switch (env.getBackend()) { - case JAVAC: - return DaggerTypeElement.fromJavac(toJavac(typeElement)); - case KSP: - return DaggerTypeElement.fromKsp(toKS(typeElement)); - } - throw new IllegalStateException( - String.format("Backend %s is not supported yet.", env.getBackend())); + private static DaggerTypeElement toSpiModel(XTypeElement typeElement) { + return DaggerTypeElementImpl.from(typeElement); } - private static DaggerType toSpiModel(XType type, XProcessingEnv env) { - switch (env.getBackend()) { - case JAVAC: - return DaggerType.fromJavac(toJavac(type)); - case KSP: - return DaggerType.fromKsp(toKS(type)); - } - throw new IllegalStateException( - String.format("Backend %s is not supported yet.", env.getBackend())); + private static DaggerType toSpiModel(XType type) { + return DaggerTypeImpl.from(type); } - static DaggerAnnotation toSpiModel(XAnnotation annotation, XProcessingEnv env) { - DaggerTypeElement typeElement = toSpiModel(annotation.getTypeElement(), env); - - switch (env.getBackend()) { - case JAVAC: - return DaggerAnnotation.fromJavac(typeElement, toJavac(annotation)); - case KSP: - return DaggerAnnotation.fromKsp(typeElement, toKS(annotation)); - } - throw new IllegalStateException( - String.format("Backend %s is not supported yet.", env.getBackend())); + static DaggerAnnotation toSpiModel(XAnnotation annotation) { + return DaggerAnnotationImpl.from(annotation); } - private static DaggerElement toSpiModel(XElement element, XProcessingEnv env) { - switch (env.getBackend()) { - case JAVAC: - return DaggerElement.fromJavac(toJavac(element)); - case KSP: - return DaggerElement.fromKsp(XElements.toKSAnnotated(element)); - } - throw new IllegalStateException( - String.format("Backend %s is not supported yet.", env.getBackend())); + private static DaggerElement toSpiModel(XElement element) { + return DaggerElementImpl.from(element); } - private static DaggerExecutableElement toSpiModel( - XExecutableElement executableElement, XProcessingEnv env) { - switch (env.getBackend()) { - case JAVAC: - return DaggerExecutableElement.fromJava(toJavac(executableElement)); - case KSP: - return DaggerExecutableElement.fromKsp(toKS(executableElement)); - } - throw new IllegalStateException( - String.format("Backend %s is not supported yet.", env.getBackend())); + private static DaggerExecutableElement toSpiModel(XExecutableElement executableElement) { + return DaggerExecutableElementImpl.from(executableElement); } static DaggerProcessingEnv toSpiModel(XProcessingEnv env) { - switch (env.getBackend()) { - case JAVAC: - return DaggerProcessingEnv.fromJavac(toJavac(env)); - case KSP: - return DaggerProcessingEnv.fromKsp(toKS(env), toKSResolver(env)); - } - throw new IllegalStateException( - String.format("Backend %s is not supported yet.", env.getBackend())); + return DaggerProcessingEnvImpl.from(env); } private static dagger.internal.codegen.model.BindingGraph.ComponentNode toInternal( @@ -295,14 +265,14 @@ static ComponentNode create( dagger.internal.codegen.model.BindingGraph.ComponentNode componentNode, XProcessingEnv env) { return new AutoValue_SpiModelBindingGraphConverter_ComponentNodeImpl( - toSpiModel(componentNode.componentPath(), env), + toSpiModel(componentNode.componentPath()), componentNode.isSubcomponent(), componentNode.isRealComponent(), componentNode.entryPoints().stream() - .map(request -> SpiModelBindingGraphConverter.toSpiModel(request, env)) + .map(request -> SpiModelBindingGraphConverter.toSpiModel(request)) .collect(toImmutableSet()), componentNode.scopes().stream() - .map(request -> SpiModelBindingGraphConverter.toSpiModel(request, env)) + .map(SpiModelBindingGraphConverter::toSpiModel) .collect(toImmutableSet()), componentNode); } @@ -319,15 +289,15 @@ public final String toString() { abstract static class BindingNodeImpl implements Binding { static Binding create(dagger.internal.codegen.model.Binding binding, XProcessingEnv env) { return new AutoValue_SpiModelBindingGraphConverter_BindingNodeImpl( - toSpiModel(binding.key(), env), - toSpiModel(binding.componentPath(), env), + toSpiModel(binding.key()), + toSpiModel(binding.componentPath()), binding.dependencies().stream() - .map(request -> SpiModelBindingGraphConverter.toSpiModel(request, env)) + .map(request -> SpiModelBindingGraphConverter.toSpiModel(request)) .collect(toImmutableSet()), - binding.bindingElement().map(element -> toSpiModel(element.xprocessing(), env)), - binding.contributingModule().map(module -> toSpiModel(module.xprocessing(), env)), + binding.bindingElement().map(element -> toSpiModel(element.xprocessing())), + binding.contributingModule().map(module -> toSpiModel(module.xprocessing())), binding.requiresModuleInstance(), - binding.scope().map(scope -> SpiModelBindingGraphConverter.toSpiModel(scope, env)), + binding.scope().map(SpiModelBindingGraphConverter::toSpiModel), binding.isNullable(), binding.isProduction(), toSpiModel(binding.kind()), @@ -348,8 +318,8 @@ static MissingBinding create( dagger.internal.codegen.model.BindingGraph.MissingBinding missingBinding, XProcessingEnv env) { return new AutoValue_SpiModelBindingGraphConverter_MissingBindingImpl( - toSpiModel(missingBinding.componentPath(), env), - toSpiModel(missingBinding.key(), env), + toSpiModel(missingBinding.componentPath()), + toSpiModel(missingBinding.key()), missingBinding); } @@ -369,7 +339,7 @@ static DependencyEdge create( dagger.internal.codegen.model.BindingGraph.DependencyEdge dependencyEdge, XProcessingEnv env) { return new AutoValue_SpiModelBindingGraphConverter_DependencyEdgeImpl( - toSpiModel(dependencyEdge.dependencyRequest(), env), + toSpiModel(dependencyEdge.dependencyRequest()), dependencyEdge.isEntryPoint(), dependencyEdge); } @@ -388,8 +358,7 @@ static ChildFactoryMethodEdge create( dagger.internal.codegen.model.BindingGraph.ChildFactoryMethodEdge childFactoryMethodEdge, XProcessingEnv env) { return new AutoValue_SpiModelBindingGraphConverter_ChildFactoryMethodEdgeImpl( - toSpiModel(childFactoryMethodEdge.factoryMethod().xprocessing(), env), - childFactoryMethodEdge); + toSpiModel(childFactoryMethodEdge.factoryMethod().xprocessing()), childFactoryMethodEdge); } abstract dagger.internal.codegen.model.BindingGraph.ChildFactoryMethodEdge internalDelegate(); @@ -409,7 +378,7 @@ static SubcomponentCreatorBindingEdge create( XProcessingEnv env) { return new AutoValue_SpiModelBindingGraphConverter_SubcomponentCreatorBindingEdgeImpl( subcomponentCreatorBindingEdge.declaringModules().stream() - .map(module -> toSpiModel(module.xprocessing(), env)) + .map(module -> toSpiModel(module.xprocessing())) .collect(toImmutableSet()), subcomponentCreatorBindingEdge); } @@ -458,6 +427,201 @@ public Optional componentNode(ComponentPath componentPath) { } } + @AutoValue + abstract static class DaggerElementImpl extends DaggerElement { + public static DaggerElement from(XElement element) { + return new AutoValue_SpiModelBindingGraphConverter_DaggerElementImpl(element); + } + + abstract XElement element(); + + @Override + public Element java() { + return toJavac(element()); + } + + @Override + public KSAnnotated ksp() { + return toKS(element()); + } + + @Override + public DaggerProcessingEnv.Backend backend() { + return getBackend(getProcessingEnv(element())); + } + + @Override + public final String toString() { + return XElements.toStableString(element()); + } + } + + @AutoValue + abstract static class DaggerTypeElementImpl extends DaggerTypeElement { + public static DaggerTypeElement from(XTypeElement element) { + return new AutoValue_SpiModelBindingGraphConverter_DaggerTypeElementImpl(element); + } + + abstract XTypeElement element(); + + @Override + public TypeElement java() { + return toJavac(element()); + } + + @Override + public KSClassDeclaration ksp() { + return toKS(element()); + } + + @Override + public DaggerProcessingEnv.Backend backend() { + return getBackend(getProcessingEnv(element())); + } + + @Override + public final String toString() { + return XElements.toStableString(element()); + } + } + + @AutoValue + abstract static class DaggerTypeImpl extends DaggerType { + public static DaggerType from(XType type) { + return new AutoValue_SpiModelBindingGraphConverter_DaggerTypeImpl( + XTypes.equivalence().wrap(type)); + } + + abstract Equivalence.Wrapper type(); + + @Override + public TypeMirror java() { + return toJavac(type().get()); + } + + @Override + public KSType ksp() { + return toKS(type().get()); + } + + @Override + public DaggerProcessingEnv.Backend backend() { + return getBackend(getProcessingEnv(type().get())); + } + + @Override + public final String toString() { + return XTypes.toStableString(type().get()); + } + } + + @AutoValue + abstract static class DaggerAnnotationImpl extends DaggerAnnotation { + public static DaggerAnnotation from(XAnnotation annotation) { + return new AutoValue_SpiModelBindingGraphConverter_DaggerAnnotationImpl( + XAnnotations.equivalence().wrap(annotation)); + } + + abstract Equivalence.Wrapper annotation(); + + @Override + public DaggerTypeElement annotationTypeElement() { + return DaggerTypeElementImpl.from(annotation().get().getTypeElement()); + } + + @Override + public AnnotationMirror java() { + return toJavac(annotation().get()); + } + + @Override + public KSAnnotation ksp() { + return toKS(annotation().get()); + } + + @Override + public DaggerProcessingEnv.Backend backend() { + return getBackend(getProcessingEnv(annotation().get())); + } + + @Override + public final String toString() { + return XAnnotations.toStableString(annotation().get()); + } + } + + @AutoValue + abstract static class DaggerExecutableElementImpl extends DaggerExecutableElement { + public static DaggerExecutableElement from(XExecutableElement executableElement) { + return new AutoValue_SpiModelBindingGraphConverter_DaggerExecutableElementImpl( + executableElement); + } + + abstract XExecutableElement executableElement(); + + @Override + public ExecutableElement java() { + return toJavac(executableElement()); + } + + @Override + public KSFunctionDeclaration ksp() { + return toKS(executableElement()); + } + + @Override + public DaggerProcessingEnv.Backend backend() { + return getBackend(getProcessingEnv(executableElement())); + } + + @Override + public final String toString() { + return XElements.toStableString(executableElement()); + } + } + + private static class DaggerProcessingEnvImpl extends DaggerProcessingEnv { + private final XProcessingEnv env; + + public static DaggerProcessingEnv from(XProcessingEnv env) { + return new DaggerProcessingEnvImpl(env); + } + + DaggerProcessingEnvImpl(XProcessingEnv env) { + this.env = env; + } + + @Override + public ProcessingEnvironment java() { + return toJavac(env); + } + + @Override + public SymbolProcessorEnvironment ksp() { + return toKS(env); + } + + @Override + public Resolver resolver() { + return toKSResolver(env); + } + + @Override + public DaggerProcessingEnv.Backend backend() { + return getBackend(env); + } + } + + private static DaggerProcessingEnv.Backend getBackend(XProcessingEnv env) { + switch (env.getBackend()) { + case JAVAC: + return DaggerProcessingEnv.Backend.JAVAC; + case KSP: + return DaggerProcessingEnv.Backend.KSP; + } + throw new AssertionError(String.format("Unexpected backend %s", env.getBackend())); + } + private static final class DiagnosticReporterImpl extends DiagnosticReporter { static DiagnosticReporterImpl create( dagger.internal.codegen.model.DiagnosticReporter reporter) { diff --git a/java/dagger/spi/model/ComponentPath.java b/java/dagger/spi/model/ComponentPath.java index 63a5f6b90e9..57dcfe6617b 100644 --- a/java/dagger/spi/model/ComponentPath.java +++ b/java/dagger/spi/model/ComponentPath.java @@ -89,7 +89,7 @@ public final boolean atRoot() { @Override public final String toString() { - return components().stream().map(DaggerTypeElement::qualifiedName).collect(joining(" → ")); + return components().stream().map(Key::qualifiedName).collect(joining(" → ")); } @Memoized diff --git a/java/dagger/spi/model/DaggerAnnotation.java b/java/dagger/spi/model/DaggerAnnotation.java index aa44e3827ee..15fb2d7e2d8 100644 --- a/java/dagger/spi/model/DaggerAnnotation.java +++ b/java/dagger/spi/model/DaggerAnnotation.java @@ -16,25 +16,14 @@ package dagger.spi.model; -import com.google.auto.common.AnnotationMirrors; -import com.google.auto.value.AutoValue; import com.google.devtools.ksp.symbol.KSAnnotation; +import com.google.errorprone.annotations.DoNotMock; import javax.annotation.Nullable; import javax.lang.model.element.AnnotationMirror; /** Wrapper type for an annotation. */ -@AutoValue +@DoNotMock("Only use real implementations created by Dagger") public abstract class DaggerAnnotation { - public static DaggerAnnotation fromJavac( - DaggerTypeElement annotationTypeElement, AnnotationMirror annotation) { - return new AutoValue_DaggerAnnotation(annotationTypeElement, annotation, null); - } - - public static DaggerAnnotation fromKsp( - DaggerTypeElement annotationTypeElement, KSAnnotation ksp) { - return new AutoValue_DaggerAnnotation(annotationTypeElement, null, ksp); - } - public abstract DaggerTypeElement annotationTypeElement(); /** @@ -56,15 +45,4 @@ public DaggerProcessingEnv.Backend backend() { } throw new AssertionError("Unexpected backend"); } - - @Override - public String toString() { - switch (backend()) { - case JAVAC: - return AnnotationMirrors.toString(java()); - case KSP: - return ksp().toString(); - } - throw new IllegalStateException(String.format("Backend %s not supported yet.", backend())); - } } diff --git a/java/dagger/spi/model/DaggerElement.java b/java/dagger/spi/model/DaggerElement.java index e4796b4d340..6b11812ba3b 100644 --- a/java/dagger/spi/model/DaggerElement.java +++ b/java/dagger/spi/model/DaggerElement.java @@ -16,22 +16,14 @@ package dagger.spi.model; -import com.google.auto.value.AutoValue; import com.google.devtools.ksp.symbol.KSAnnotated; +import com.google.errorprone.annotations.DoNotMock; import javax.annotation.Nullable; import javax.lang.model.element.Element; /** Wrapper type for an element. */ -@AutoValue +@DoNotMock("Only use real implementations created by Dagger") public abstract class DaggerElement { - public static DaggerElement fromJavac(Element element) { - return new AutoValue_DaggerElement(element, null); - } - - public static DaggerElement fromKsp(KSAnnotated ksp) { - return new AutoValue_DaggerElement(null, ksp); - } - /** * Java representation for the element, returns {@code null} not using java annotation processor. */ @@ -42,23 +34,6 @@ public static DaggerElement fromKsp(KSAnnotated ksp) { @Nullable public abstract KSAnnotated ksp(); - public DaggerProcessingEnv.Backend backend() { - if (java() != null) { - return DaggerProcessingEnv.Backend.JAVAC; - } else if (ksp() != null) { - return DaggerProcessingEnv.Backend.KSP; - } - throw new AssertionError("Unexpected backend"); - } - - @Override - public String toString() { - switch (backend()) { - case JAVAC: - return java().toString(); - case KSP: - return ksp().toString(); - } - throw new IllegalStateException(String.format("Backend %s not supported yet.", backend())); - } + /** Returns the backend used in this compilation. */ + public abstract DaggerProcessingEnv.Backend backend(); } diff --git a/java/dagger/spi/model/DaggerExecutableElement.java b/java/dagger/spi/model/DaggerExecutableElement.java index 5f1ea6c5f1f..5ddf4a94618 100644 --- a/java/dagger/spi/model/DaggerExecutableElement.java +++ b/java/dagger/spi/model/DaggerExecutableElement.java @@ -16,22 +16,14 @@ package dagger.spi.model; -import com.google.auto.value.AutoValue; import com.google.devtools.ksp.symbol.KSFunctionDeclaration; +import com.google.errorprone.annotations.DoNotMock; import javax.annotation.Nullable; import javax.lang.model.element.ExecutableElement; /** Wrapper type for an executable element. */ -@AutoValue +@DoNotMock("Only use real implementations created by Dagger") public abstract class DaggerExecutableElement { - public static DaggerExecutableElement fromJava(ExecutableElement executableElement) { - return new AutoValue_DaggerExecutableElement(executableElement, null); - } - - public static DaggerExecutableElement fromKsp(KSFunctionDeclaration declaration) { - return new AutoValue_DaggerExecutableElement(null, declaration); - } - /** * Java representation for the element, returns {@code null} not using java annotation processor. */ @@ -42,33 +34,6 @@ public static DaggerExecutableElement fromKsp(KSFunctionDeclaration declaration) @Nullable public abstract KSFunctionDeclaration ksp(); - public DaggerProcessingEnv.Backend backend() { - if (java() != null) { - return DaggerProcessingEnv.Backend.JAVAC; - } else if (ksp() != null) { - return DaggerProcessingEnv.Backend.KSP; - } - throw new AssertionError("Unexpected backend"); - } - - @Override - public String toString() { - switch (backend()) { - case JAVAC: - return java().toString(); - case KSP: - return ksp().toString(); - } - throw new IllegalStateException(String.format("Backend %s not supported yet.", backend())); - } - - String simpleName() { - switch (backend()) { - case JAVAC: - return java().getSimpleName().toString(); - case KSP: - return ksp().getSimpleName().toString(); - } - throw new IllegalStateException(String.format("Backend %s not supported yet.", backend())); - } + /** Returns the backend used in this compilation. */ + public abstract DaggerProcessingEnv.Backend backend(); } diff --git a/java/dagger/spi/model/DaggerProcessingEnv.java b/java/dagger/spi/model/DaggerProcessingEnv.java index 8c652c89d2c..11b274f0ff4 100644 --- a/java/dagger/spi/model/DaggerProcessingEnv.java +++ b/java/dagger/spi/model/DaggerProcessingEnv.java @@ -16,28 +16,19 @@ package dagger.spi.model; -import com.google.auto.value.AutoValue; import com.google.devtools.ksp.processing.Resolver; import com.google.devtools.ksp.processing.SymbolProcessorEnvironment; +import com.google.errorprone.annotations.DoNotMock; import javax.annotation.Nullable; import javax.annotation.processing.ProcessingEnvironment; /** Wrapper type for an element. */ -@AutoValue +@DoNotMock("Only use real implementations created by Dagger") public abstract class DaggerProcessingEnv { /** Represents a type of backend used for compilation. */ - public enum Backend { JAVAC, KSP } - - public static boolean isJavac(Backend backend) { - return backend.equals(Backend.JAVAC); - } - - public static DaggerProcessingEnv fromJavac(ProcessingEnvironment env) { - return new AutoValue_DaggerProcessingEnv(env, null, null); - } - - public static DaggerProcessingEnv fromKsp(SymbolProcessorEnvironment env, Resolver resolver) { - return new AutoValue_DaggerProcessingEnv(null, env, resolver); + public enum Backend { + JAVAC, + KSP } /** @@ -47,19 +38,14 @@ public static DaggerProcessingEnv fromKsp(SymbolProcessorEnvironment env, Resolv @Nullable public abstract ProcessingEnvironment java(); + /** Ksp symbol processing environment hosting symbol processors. */ @Nullable public abstract SymbolProcessorEnvironment ksp(); + /** Ksp resolver provides [SymbolProcessor] with access to compiler details such as Symbols. */ @Nullable public abstract Resolver resolver(); /** Returns the backend used in this compilation. */ - public DaggerProcessingEnv.Backend backend() { - if (java() != null) { - return DaggerProcessingEnv.Backend.JAVAC; - } else if (ksp() != null) { - return DaggerProcessingEnv.Backend.KSP; - } - throw new AssertionError("Unexpected backend"); - } + public abstract DaggerProcessingEnv.Backend backend(); } diff --git a/java/dagger/spi/model/DaggerType.java b/java/dagger/spi/model/DaggerType.java index 03f5a8cc4bb..9942ef9dd49 100644 --- a/java/dagger/spi/model/DaggerType.java +++ b/java/dagger/spi/model/DaggerType.java @@ -16,21 +16,14 @@ package dagger.spi.model; -import com.google.auto.value.AutoValue; import com.google.devtools.ksp.symbol.KSType; +import com.google.errorprone.annotations.DoNotMock; import javax.annotation.Nullable; import javax.lang.model.type.TypeMirror; /** Wrapper type for a type. */ -@AutoValue +@DoNotMock("Only use real implementations created by Dagger") public abstract class DaggerType { - public static DaggerType fromJavac(TypeMirror type) { - return new AutoValue_DaggerType(type, null); - } - - public static DaggerType fromKsp(KSType type) { - return new AutoValue_DaggerType(null, type); - } /** Java representation for the type, returns {@code null} not using java annotation processor. */ @Nullable @@ -40,23 +33,6 @@ public static DaggerType fromKsp(KSType type) { @Nullable public abstract KSType ksp(); - public DaggerProcessingEnv.Backend backend() { - if (java() != null) { - return DaggerProcessingEnv.Backend.JAVAC; - } else if (ksp() != null) { - return DaggerProcessingEnv.Backend.KSP; - } - throw new AssertionError("Unexpected backend"); - } - - @Override - public String toString() { - switch (backend()) { - case JAVAC: - return java().toString(); - case KSP: - return ksp().toString(); - } - throw new IllegalStateException(String.format("Backend %s not supported yet.", backend())); - } + /** Returns the backend used in this compilation. */ + public abstract DaggerProcessingEnv.Backend backend(); } diff --git a/java/dagger/spi/model/DaggerTypeElement.java b/java/dagger/spi/model/DaggerTypeElement.java index a08d94c6ede..db1e2987ad1 100644 --- a/java/dagger/spi/model/DaggerTypeElement.java +++ b/java/dagger/spi/model/DaggerTypeElement.java @@ -16,23 +16,14 @@ package dagger.spi.model; -import com.google.auto.common.MoreElements; -import com.google.auto.value.AutoValue; import com.google.devtools.ksp.symbol.KSClassDeclaration; +import com.google.errorprone.annotations.DoNotMock; import javax.annotation.Nullable; import javax.lang.model.element.TypeElement; /** Wrapper type for a type element. */ -@AutoValue +@DoNotMock("Only use real implementations created by Dagger") public abstract class DaggerTypeElement { - public static DaggerTypeElement fromJavac(@Nullable TypeElement element) { - return new AutoValue_DaggerTypeElement(element, null); - } - - public static DaggerTypeElement fromKsp(@Nullable KSClassDeclaration declaration) { - return new AutoValue_DaggerTypeElement(null, declaration); - } - /** Java representation for the type, returns {@code null} not using java annotation processor. */ @Nullable public abstract TypeElement java(); @@ -41,53 +32,5 @@ public static DaggerTypeElement fromKsp(@Nullable KSClassDeclaration declaration @Nullable public abstract KSClassDeclaration ksp(); - public final boolean hasAnnotation(String annotationName) { - switch (backend()) { - case JAVAC: - return MoreElements.isAnnotationPresent(java(), annotationName); - case KSP: - return KspUtilsKt.hasAnnotation(ksp(), annotationName); - } - throw new IllegalStateException(String.format("Backend %s not supported yet.", backend())); - } - - public String packageName() { - switch (backend()) { - case JAVAC: - return MoreElements.getPackage(java()).getQualifiedName().toString(); - case KSP: - return KspUtilsKt.getNormalizedPackageName(ksp()); - } - throw new IllegalStateException(String.format("Backend %s not supported yet.", backend())); - } - - public String qualifiedName() { - switch (backend()) { - case JAVAC: - return java().getQualifiedName().toString(); - case KSP: - return ksp().getQualifiedName().asString(); - } - throw new IllegalStateException(String.format("Backend %s not supported yet.", backend())); - } - - public DaggerProcessingEnv.Backend backend() { - if (java() != null) { - return DaggerProcessingEnv.Backend.JAVAC; - } else if (ksp() != null) { - return DaggerProcessingEnv.Backend.KSP; - } - throw new AssertionError("Unexpected backend"); - } - - @Override - public String toString() { - switch (backend()) { - case JAVAC: - return java().toString(); - case KSP: - return ksp().toString(); - } - throw new IllegalStateException(String.format("Backend %s not supported yet.", backend())); - } + public abstract DaggerProcessingEnv.Backend backend(); } diff --git a/java/dagger/spi/model/Key.java b/java/dagger/spi/model/Key.java index d871e6d12a6..ee5ea1a4b16 100644 --- a/java/dagger/spi/model/Key.java +++ b/java/dagger/spi/model/Key.java @@ -130,7 +130,7 @@ public abstract static class MultibindingContributionIdentifier { private static MultibindingContributionIdentifier create( DaggerTypeElement contributingModule, DaggerExecutableElement bindingMethod) { return new AutoValue_Key_MultibindingContributionIdentifier( - contributingModule.qualifiedName(), bindingMethod.simpleName()); + qualifiedName(contributingModule), simpleName(bindingMethod)); } /** Returns the module containing the multibinding method. */ @@ -150,4 +150,24 @@ public final String toString() { return String.format("%s#%s", contributingModule(), bindingMethod()); } } + + static String qualifiedName(DaggerTypeElement element) { + switch (element.backend()) { + case JAVAC: + return element.java().getQualifiedName().toString(); + case KSP: + return element.ksp().getQualifiedName().asString(); + } + throw new IllegalStateException("Unknown backend: " + element.backend()); + } + + private static String simpleName(DaggerExecutableElement element) { + switch (element.backend()) { + case JAVAC: + return element.java().getSimpleName().toString(); + case KSP: + return element.ksp().getSimpleName().asString(); + } + throw new IllegalStateException("Unknown backend: " + element.backend()); + } } diff --git a/java/dagger/spi/model/Scope.java b/java/dagger/spi/model/Scope.java index d1505b96992..ccb85d2df6e 100644 --- a/java/dagger/spi/model/Scope.java +++ b/java/dagger/spi/model/Scope.java @@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument; +import com.google.auto.common.MoreElements; import com.google.auto.value.AutoValue; /** A representation of a {@link javax.inject.Scope}. */ @@ -42,8 +43,16 @@ public static boolean isScope(DaggerAnnotation scopeAnnotation) { * Returns {@code true} if {@code scopeAnnotationType} is a {@link javax.inject.Scope} annotation. */ public static boolean isScope(DaggerTypeElement scopeAnnotationType) { - return scopeAnnotationType.hasAnnotation(SCOPE) - || scopeAnnotationType.hasAnnotation(SCOPE_JAVAX); + switch (scopeAnnotationType.backend()) { + case JAVAC: + return MoreElements.isAnnotationPresent(scopeAnnotationType.java(), SCOPE) + || MoreElements.isAnnotationPresent(scopeAnnotationType.java(), SCOPE_JAVAX); + case KSP: + return KspUtilsKt.hasAnnotation(scopeAnnotationType.ksp(), SCOPE) + || KspUtilsKt.hasAnnotation(scopeAnnotationType.ksp(), SCOPE_JAVAX); + } + throw new IllegalStateException( + String.format("Backend %s not supported yet.", scopeAnnotationType.backend())); } private boolean isScope(String annotationName) {