diff --git a/java/dagger/hilt/processor/internal/DaggerModels.kt b/java/dagger/hilt/processor/internal/DaggerModels.kt index 66b076a8cb8..f875df27bb8 100644 --- a/java/dagger/hilt/processor/internal/DaggerModels.kt +++ b/java/dagger/hilt/processor/internal/DaggerModels.kt @@ -23,19 +23,18 @@ import com.squareup.javapoet.ClassName import dagger.spi.model.DaggerAnnotation import dagger.spi.model.DaggerElement import dagger.spi.model.DaggerProcessingEnv +import dagger.spi.model.DaggerProcessingEnv.Backend.JAVAC +import dagger.spi.model.DaggerProcessingEnv.Backend.KSP import dagger.spi.model.DaggerType fun DaggerType.hasAnnotation(className: ClassName): Boolean = when (checkNotNull(backend())) { - DaggerProcessingEnv.Backend.JAVAC -> { - val javaType = checkNotNull(java()) - Processors.hasAnnotation(MoreTypes.asTypeElement(javaType), className) - } - DaggerProcessingEnv.Backend.KSP -> { - val kspType = checkNotNull(ksp()) - kspType.declaration.hasAnnotation(className.canonicalName()) + JAVAC -> { + val javac = javac() + Processors.hasAnnotation(MoreTypes.asTypeElement(javac), className) } + KSP -> ksp().declaration.hasAnnotation(className.canonicalName()) } fun KSDeclaration.hasAnnotation(annotationName: String): Boolean = @@ -45,26 +44,20 @@ fun KSDeclaration.hasAnnotation(annotationName: String): Boolean = fun DaggerElement.hasAnnotation(className: ClassName) = when (checkNotNull(backend())) { - DaggerProcessingEnv.Backend.JAVAC -> { - val javaElement = checkNotNull(java()) - Processors.hasAnnotation(javaElement, className) - } - DaggerProcessingEnv.Backend.KSP -> { - val kspAnnotated = checkNotNull(ksp()) - kspAnnotated.hasAnnotation(className) + JAVAC -> { + val javac = javac() + Processors.hasAnnotation(javac, className) } + KSP -> ksp().hasAnnotation(className) } fun DaggerAnnotation.getQualifiedName() = when (checkNotNull(backend())) { - DaggerProcessingEnv.Backend.JAVAC -> { - val javaAnnotation = checkNotNull(java()) - MoreTypes.asTypeElement(javaAnnotation.annotationType).qualifiedName.toString() - } - DaggerProcessingEnv.Backend.KSP -> { - val kspAnnotation = checkNotNull(ksp()) - kspAnnotation.annotationType.resolve().declaration.qualifiedName!!.asString() + JAVAC -> { + val javac = javac() + MoreTypes.asTypeElement(javac.annotationType).qualifiedName.toString() } + KSP -> ksp().annotationType.resolve().declaration.qualifiedName!!.asString() } private fun KSAnnotated.hasAnnotation(className: ClassName) = diff --git a/java/dagger/internal/codegen/validation/SpiModelBindingGraphConverter.java b/java/dagger/internal/codegen/validation/SpiModelBindingGraphConverter.java index 396afc20131..42e1adffb34 100644 --- a/java/dagger/internal/codegen/validation/SpiModelBindingGraphConverter.java +++ b/java/dagger/internal/codegen/validation/SpiModelBindingGraphConverter.java @@ -20,6 +20,7 @@ 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; +import static com.google.common.base.Preconditions.checkState; import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList; import static dagger.internal.codegen.extension.DaggerStreams.toImmutableMap; import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; @@ -269,7 +270,7 @@ static ComponentNode create( componentNode.isSubcomponent(), componentNode.isRealComponent(), componentNode.entryPoints().stream() - .map(request -> SpiModelBindingGraphConverter.toSpiModel(request)) + .map(SpiModelBindingGraphConverter::toSpiModel) .collect(toImmutableSet()), componentNode.scopes().stream() .map(SpiModelBindingGraphConverter::toSpiModel) @@ -292,7 +293,7 @@ static Binding create(dagger.internal.codegen.model.Binding binding, XProcessing toSpiModel(binding.key()), toSpiModel(binding.componentPath()), binding.dependencies().stream() - .map(request -> SpiModelBindingGraphConverter.toSpiModel(request)) + .map(SpiModelBindingGraphConverter::toSpiModel) .collect(toImmutableSet()), binding.bindingElement().map(element -> toSpiModel(element.xprocessing())), binding.contributingModule().map(module -> toSpiModel(module.xprocessing())), @@ -436,12 +437,14 @@ public static DaggerElement from(XElement element) { abstract XElement element(); @Override - public Element java() { + public Element javac() { + checkIsJavac(backend()); return toJavac(element()); } @Override public KSAnnotated ksp() { + checkIsKsp(backend()); return toKS(element()); } @@ -465,12 +468,14 @@ public static DaggerTypeElement from(XTypeElement element) { abstract XTypeElement element(); @Override - public TypeElement java() { + public TypeElement javac() { + checkIsJavac(backend()); return toJavac(element()); } @Override public KSClassDeclaration ksp() { + checkIsKsp(backend()); return toKS(element()); } @@ -495,12 +500,14 @@ public static DaggerType from(XType type) { abstract Equivalence.Wrapper type(); @Override - public TypeMirror java() { + public TypeMirror javac() { + checkIsJavac(backend()); return toJavac(type().get()); } @Override public KSType ksp() { + checkIsKsp(backend()); return toKS(type().get()); } @@ -530,12 +537,14 @@ public DaggerTypeElement annotationTypeElement() { } @Override - public AnnotationMirror java() { + public AnnotationMirror javac() { + checkIsJavac(backend()); return toJavac(annotation().get()); } @Override public KSAnnotation ksp() { + checkIsKsp(backend()); return toKS(annotation().get()); } @@ -560,12 +569,14 @@ public static DaggerExecutableElement from(XExecutableElement executableElement) abstract XExecutableElement executableElement(); @Override - public ExecutableElement java() { + public ExecutableElement javac() { + checkIsJavac(backend()); return toJavac(executableElement()); } @Override public KSFunctionDeclaration ksp() { + checkIsKsp(backend()); return toKS(executableElement()); } @@ -592,12 +603,14 @@ public static DaggerProcessingEnv from(XProcessingEnv env) { } @Override - public ProcessingEnvironment java() { + public ProcessingEnvironment javac() { + checkIsJavac(backend()); return toJavac(env); } @Override public SymbolProcessorEnvironment ksp() { + checkIsKsp(backend()); return toKS(env); } @@ -612,6 +625,18 @@ public DaggerProcessingEnv.Backend backend() { } } + private static void checkIsJavac(DaggerProcessingEnv.Backend backend) { + checkState( + backend == DaggerProcessingEnv.Backend.JAVAC, + "Expected JAVAC backend but was: %s", backend); + } + + private static void checkIsKsp(DaggerProcessingEnv.Backend backend) { + checkState( + backend == DaggerProcessingEnv.Backend.KSP, + "Expected KSP backend but was: %s", backend); + } + private static DaggerProcessingEnv.Backend getBackend(XProcessingEnv env) { switch (env.getBackend()) { case JAVAC: diff --git a/java/dagger/spi/model/CompilerEnvironment.java b/java/dagger/spi/model/CompilerEnvironment.java deleted file mode 100644 index 28553e4c30c..00000000000 --- a/java/dagger/spi/model/CompilerEnvironment.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2021 The Dagger Authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package dagger.spi.model; - -/** Types for the compiler in use for annotation processing. */ -public enum CompilerEnvironment { - JAVA, - KSP -} diff --git a/java/dagger/spi/model/DaggerAnnotation.java b/java/dagger/spi/model/DaggerAnnotation.java index 15fb2d7e2d8..5b590033f8a 100644 --- a/java/dagger/spi/model/DaggerAnnotation.java +++ b/java/dagger/spi/model/DaggerAnnotation.java @@ -18,7 +18,6 @@ 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. */ @@ -27,22 +26,19 @@ public abstract class DaggerAnnotation { public abstract DaggerTypeElement annotationTypeElement(); /** - * java representation for the annotation, returns {@code null} if the annotation isn't a java - * element. + * Returns the Javac representation for the annotation. + * + * @throws IllegalStateException if the current backend isn't Javac. */ - @Nullable - public abstract AnnotationMirror java(); + public abstract AnnotationMirror javac(); - /** KSP declaration for the annotation, returns {@code null} not using KSP. */ - @Nullable + /** + * Returns the KSP representation for the annotation. + * + * @throws IllegalStateException if the current backend isn't KSP. + */ public abstract KSAnnotation 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"); - } + /** Returns the backend used in this compilation. */ + public abstract DaggerProcessingEnv.Backend backend(); } diff --git a/java/dagger/spi/model/DaggerElement.java b/java/dagger/spi/model/DaggerElement.java index 6b11812ba3b..0c0c53ebf0d 100644 --- a/java/dagger/spi/model/DaggerElement.java +++ b/java/dagger/spi/model/DaggerElement.java @@ -18,20 +18,23 @@ 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. */ @DoNotMock("Only use real implementations created by Dagger") public abstract class DaggerElement { /** - * Java representation for the element, returns {@code null} not using java annotation processor. + * Returns the Javac representation for the element. + * + * @throws IllegalStateException if the current backend isn't Javac. */ - @Nullable - public abstract Element java(); + public abstract Element javac(); - /** KSP declaration for the element, returns {@code null} not using KSP. */ - @Nullable + /** + * Returns the KSP representation for the element. + * + * @throws IllegalStateException if the current backend isn't KSP. + */ public abstract KSAnnotated ksp(); /** Returns the backend used in this compilation. */ diff --git a/java/dagger/spi/model/DaggerExecutableElement.java b/java/dagger/spi/model/DaggerExecutableElement.java index 5ddf4a94618..afbd8e001aa 100644 --- a/java/dagger/spi/model/DaggerExecutableElement.java +++ b/java/dagger/spi/model/DaggerExecutableElement.java @@ -18,20 +18,23 @@ 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. */ @DoNotMock("Only use real implementations created by Dagger") public abstract class DaggerExecutableElement { /** - * Java representation for the element, returns {@code null} not using java annotation processor. + * Returns the Javac representation for the executable element. + * + * @throws IllegalStateException if the current backend isn't Javac. */ - @Nullable - public abstract ExecutableElement java(); + public abstract ExecutableElement javac(); - /** KSP declaration for the element, returns {@code null} not using KSP. */ - @Nullable + /** + * Returns the KSP representation for the executable element. + * + * @throws IllegalStateException if the current backend isn't KSP. + */ public abstract KSFunctionDeclaration ksp(); /** Returns the backend used in this compilation. */ diff --git a/java/dagger/spi/model/DaggerProcessingEnv.java b/java/dagger/spi/model/DaggerProcessingEnv.java index 11b274f0ff4..ab2e33e5f35 100644 --- a/java/dagger/spi/model/DaggerProcessingEnv.java +++ b/java/dagger/spi/model/DaggerProcessingEnv.java @@ -19,7 +19,6 @@ 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. */ @@ -32,18 +31,24 @@ public enum Backend { } /** - * Java representation for the processing environment, returns {@code null} not using java - * annotation processor. + * Returns the Javac representation for the processing environment. + * + * @throws IllegalStateException if the current backend isn't Javac. */ - @Nullable - public abstract ProcessingEnvironment java(); + public abstract ProcessingEnvironment javac(); - /** Ksp symbol processing environment hosting symbol processors. */ - @Nullable + /** + * Returns the KSP representation for the processing environment. + * + * @throws IllegalStateException if the current backend isn't KSP. + */ public abstract SymbolProcessorEnvironment ksp(); - /** Ksp resolver provides [SymbolProcessor] with access to compiler details such as Symbols. */ - @Nullable + /** + * Returns the KSP representation for the resolver. + * + * @throws IllegalStateException if the current backend isn't KSP. + */ public abstract Resolver resolver(); /** Returns the backend used in this compilation. */ diff --git a/java/dagger/spi/model/DaggerType.java b/java/dagger/spi/model/DaggerType.java index 9942ef9dd49..46335060650 100644 --- a/java/dagger/spi/model/DaggerType.java +++ b/java/dagger/spi/model/DaggerType.java @@ -18,19 +18,23 @@ 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. */ @DoNotMock("Only use real implementations created by Dagger") public abstract class DaggerType { + /** + * Returns the Javac representation for the type. + * + * @throws IllegalStateException if the current backend isn't Javac. + */ + public abstract TypeMirror javac(); - /** Java representation for the type, returns {@code null} not using java annotation processor. */ - @Nullable - public abstract TypeMirror java(); - - /** KSP declaration for the type, returns {@code null} not using KSP. */ - @Nullable + /** + * Returns the KSP representation for the type. + * + * @throws IllegalStateException if the current backend isn't KSP. + */ public abstract KSType ksp(); /** Returns the backend used in this compilation. */ diff --git a/java/dagger/spi/model/DaggerTypeElement.java b/java/dagger/spi/model/DaggerTypeElement.java index db1e2987ad1..935770d11bb 100644 --- a/java/dagger/spi/model/DaggerTypeElement.java +++ b/java/dagger/spi/model/DaggerTypeElement.java @@ -18,19 +18,25 @@ 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. */ @DoNotMock("Only use real implementations created by Dagger") public abstract class DaggerTypeElement { - /** Java representation for the type, returns {@code null} not using java annotation processor. */ - @Nullable - public abstract TypeElement java(); + /** + * Returns the Javac representation for the type element. + * + * @throws IllegalStateException if the current backend isn't Javac. + */ + public abstract TypeElement javac(); - /** KSP declaration for the element, returns {@code null} not using KSP. */ - @Nullable + /** + * Returns the KSP representation for the type element. + * + * @throws IllegalStateException if the current backend isn't KSP. + */ public abstract KSClassDeclaration ksp(); + /** Returns the backend used in this compilation. */ public abstract DaggerProcessingEnv.Backend backend(); } diff --git a/java/dagger/spi/model/Key.java b/java/dagger/spi/model/Key.java index ee5ea1a4b16..d4478f6a65c 100644 --- a/java/dagger/spi/model/Key.java +++ b/java/dagger/spi/model/Key.java @@ -154,7 +154,7 @@ public final String toString() { static String qualifiedName(DaggerTypeElement element) { switch (element.backend()) { case JAVAC: - return element.java().getQualifiedName().toString(); + return element.javac().getQualifiedName().toString(); case KSP: return element.ksp().getQualifiedName().asString(); } @@ -164,7 +164,7 @@ static String qualifiedName(DaggerTypeElement element) { private static String simpleName(DaggerExecutableElement element) { switch (element.backend()) { case JAVAC: - return element.java().getSimpleName().toString(); + return element.javac().getSimpleName().toString(); case KSP: return element.ksp().getSimpleName().asString(); } diff --git a/java/dagger/spi/model/MoreAnnotationMirrors.java b/java/dagger/spi/model/MoreAnnotationMirrors.java index 44c0363c9b8..a5c04c6afb7 100644 --- a/java/dagger/spi/model/MoreAnnotationMirrors.java +++ b/java/dagger/spi/model/MoreAnnotationMirrors.java @@ -35,7 +35,7 @@ final class MoreAnnotationMirrors { * defined in the annotation type. */ public static String toStableString(DaggerAnnotation qualifier) { - return stableAnnotationMirrorToString(qualifier.java()); + return stableAnnotationMirrorToString(qualifier.javac()); } /** diff --git a/java/dagger/spi/model/Scope.java b/java/dagger/spi/model/Scope.java index ccb85d2df6e..0b058c3b24a 100644 --- a/java/dagger/spi/model/Scope.java +++ b/java/dagger/spi/model/Scope.java @@ -45,8 +45,8 @@ public static boolean isScope(DaggerAnnotation scopeAnnotation) { public static boolean isScope(DaggerTypeElement scopeAnnotationType) { switch (scopeAnnotationType.backend()) { case JAVAC: - return MoreElements.isAnnotationPresent(scopeAnnotationType.java(), SCOPE) - || MoreElements.isAnnotationPresent(scopeAnnotationType.java(), SCOPE_JAVAX); + return MoreElements.isAnnotationPresent(scopeAnnotationType.javac(), SCOPE) + || MoreElements.isAnnotationPresent(scopeAnnotationType.javac(), SCOPE_JAVAX); case KSP: return KspUtilsKt.hasAnnotation(scopeAnnotationType.ksp(), SCOPE) || KspUtilsKt.hasAnnotation(scopeAnnotationType.ksp(), SCOPE_JAVAX); diff --git a/java/dagger/spi/model/testing/BindingGraphSubject.java b/java/dagger/spi/model/testing/BindingGraphSubject.java index e2067318b06..4d53d93673a 100644 --- a/java/dagger/spi/model/testing/BindingGraphSubject.java +++ b/java/dagger/spi/model/testing/BindingGraphSubject.java @@ -112,7 +112,7 @@ private static String keyString(String qualifier, String type) { private static String formattedType(DaggerType type) { switch (type.backend()) { case JAVAC: - return type.java().toString(); + return type.javac().toString(); case KSP: return type.ksp().getDeclaration().getQualifiedName().asString(); }