diff --git a/java/dagger/internal/codegen/base/ComponentAnnotation.java b/java/dagger/internal/codegen/base/ComponentAnnotation.java index 732f3e888d0..e0871eb7f85 100644 --- a/java/dagger/internal/codegen/base/ComponentAnnotation.java +++ b/java/dagger/internal/codegen/base/ComponentAnnotation.java @@ -49,19 +49,6 @@ public abstract class ComponentAnnotation { private static final ImmutableSet SUBCOMPONENT_ANNOTATIONS = ImmutableSet.of(TypeNames.SUBCOMPONENT, TypeNames.PRODUCTION_SUBCOMPONENT); - // TODO(erichang): Move ComponentCreatorAnnotation into /base and use that here? - /** The component/subcomponent creator annotation types. */ - private static final ImmutableSet CREATOR_ANNOTATIONS = - ImmutableSet.of( - TypeNames.COMPONENT_BUILDER, - TypeNames.COMPONENT_FACTORY, - TypeNames.PRODUCTION_COMPONENT_BUILDER, - TypeNames.PRODUCTION_COMPONENT_FACTORY, - TypeNames.SUBCOMPONENT_BUILDER, - TypeNames.SUBCOMPONENT_FACTORY, - TypeNames.PRODUCTION_SUBCOMPONENT_BUILDER, - TypeNames.PRODUCTION_SUBCOMPONENT_FACTORY); - /** All component annotation types. */ private static final ImmutableSet ALL_COMPONENT_ANNOTATIONS = ImmutableSet.builder() @@ -73,7 +60,7 @@ public abstract class ComponentAnnotation { private static final ImmutableSet ALL_COMPONENT_AND_CREATOR_ANNOTATIONS = ImmutableSet.builder() .addAll(ALL_COMPONENT_ANNOTATIONS) - .addAll(CREATOR_ANNOTATIONS) + .addAll(ComponentCreatorAnnotation.allCreatorAnnotations()) .build(); /** All production annotation types. */ diff --git a/java/dagger/internal/codegen/base/ComponentCreatorAnnotation.java b/java/dagger/internal/codegen/base/ComponentCreatorAnnotation.java index 7a9e21250ca..621d66f6492 100644 --- a/java/dagger/internal/codegen/base/ComponentCreatorAnnotation.java +++ b/java/dagger/internal/codegen/base/ComponentCreatorAnnotation.java @@ -16,11 +16,9 @@ package dagger.internal.codegen.base; -import static androidx.room.compiler.processing.compat.XConverters.toJavac; import static com.google.common.base.Ascii.toUpperCase; import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; import static dagger.internal.codegen.extension.DaggerStreams.valuesOf; -import static dagger.internal.codegen.langmodel.DaggerElements.isAnnotationPresent; import static java.util.stream.Collectors.mapping; import androidx.room.compiler.processing.XTypeElement; @@ -29,7 +27,6 @@ import dagger.internal.codegen.javapoet.TypeNames; import java.util.stream.Collector; import java.util.stream.Stream; -import javax.lang.model.element.TypeElement; /** Simple representation of a component creator annotation type. */ public enum ComponentCreatorAnnotation { @@ -133,14 +130,7 @@ public static ImmutableSet creatorAnnotationsFor( /** Returns all creator annotations present on the given {@code type}. */ public static ImmutableSet getCreatorAnnotations(XTypeElement type) { - return getCreatorAnnotations(toJavac(type)); - } - - /** Returns all creator annotations present on the given {@code type}. */ - public static ImmutableSet getCreatorAnnotations(TypeElement type) { - return stream() - .filter(cca -> isAnnotationPresent(type, cca.annotation())) - .collect(toImmutableSet()); + return stream().filter(cca -> type.hasAnnotation(cca.annotation())).collect(toImmutableSet()); } private static Stream stream() { diff --git a/java/dagger/internal/codegen/base/ComponentKind.java b/java/dagger/internal/codegen/base/ComponentKind.java index 6770888eb2b..9d087636a67 100644 --- a/java/dagger/internal/codegen/base/ComponentKind.java +++ b/java/dagger/internal/codegen/base/ComponentKind.java @@ -30,52 +30,15 @@ /** Enumeration of the different kinds of components. */ public enum ComponentKind { - /** {@code @Component} */ - COMPONENT(TypeNames.COMPONENT, true, false), + COMPONENT(TypeNames.COMPONENT), + SUBCOMPONENT(TypeNames.SUBCOMPONENT), + PRODUCTION_COMPONENT(TypeNames.PRODUCTION_COMPONENT), + PRODUCTION_SUBCOMPONENT(TypeNames.PRODUCTION_SUBCOMPONENT), + MODULE(TypeNames.MODULE), + PRODUCER_MODULE(TypeNames.PRODUCER_MODULE); - /** {@code @Subcomponent} */ - SUBCOMPONENT(TypeNames.SUBCOMPONENT, false, false), - - /** {@code @ProductionComponent} */ - PRODUCTION_COMPONENT(TypeNames.PRODUCTION_COMPONENT, true, true), - - /** {@code @ProductionSubcomponent} */ - PRODUCTION_SUBCOMPONENT(TypeNames.PRODUCTION_SUBCOMPONENT, false, true), - - /** - * Kind for a descriptor that was generated from a {@link dagger.Module} instead of a component - * type in order to validate the module's bindings. - */ - MODULE(TypeNames.MODULE, true, false), - - /** - * Kind for a descriptor was generated from a {@link dagger.producers.ProducerModule} instead of a - * component type in order to validate the module's bindings. - */ - PRODUCER_MODULE(TypeNames.PRODUCER_MODULE, true, true), - ; - - private static final ImmutableSet ROOT_COMPONENT_KINDS = - valuesOf(ComponentKind.class) - .filter(kind -> !kind.isForModuleValidation()) - .filter(kind -> kind.isRoot()) - .collect(toImmutableSet()); - - private static final ImmutableSet SUBCOMPONENT_KINDS = - valuesOf(ComponentKind.class) - .filter(kind -> !kind.isForModuleValidation()) - .filter(kind -> !kind.isRoot()) - .collect(toImmutableSet()); - - /** Returns the set of kinds for root components. */ - public static ImmutableSet rootComponentKinds() { - return ROOT_COMPONENT_KINDS; - } - - /** Returns the set of kinds for subcomponents. */ - public static ImmutableSet subcomponentKinds() { - return SUBCOMPONENT_KINDS; - } + private static final ImmutableSet PRODUCER_KINDS = + ImmutableSet.of(PRODUCTION_COMPONENT, PRODUCTION_SUBCOMPONENT, PRODUCER_MODULE); /** Returns the annotations for components of the given kinds. */ public static ImmutableSet annotationsFor(Iterable kinds) { @@ -106,13 +69,9 @@ public static Optional forAnnotatedElement(XTypeElement element) } private final ClassName annotation; - private final boolean isRoot; - private final boolean production; - ComponentKind(ClassName annotation, boolean isRoot, boolean production) { + ComponentKind(ClassName annotation) { this.annotation = annotation; - this.isRoot = isRoot; - this.production = production; } /** Returns the annotation that marks a component of this kind. */ @@ -134,28 +93,8 @@ public ImmutableSet legalSubcomponentKinds() { : immutableEnumSet(SUBCOMPONENT, PRODUCTION_SUBCOMPONENT); } - /** - * Returns {@code true} if the descriptor is for a root component (not a subcomponent) or is for - * {@linkplain #isForModuleValidation() module-validation}. - */ - public boolean isRoot() { - return isRoot; - } - /** Returns true if this is a production component. */ public boolean isProducer() { - return production; - } - - /** Returns {@code true} if the descriptor is for a module in order to validate its bindings. */ - public boolean isForModuleValidation() { - switch (this) { - case MODULE: - case PRODUCER_MODULE: - return true; - default: - // fall through - } - return false; + return PRODUCER_KINDS.contains(this); } } diff --git a/java/dagger/internal/codegen/base/ModuleKind.java b/java/dagger/internal/codegen/base/ModuleKind.java index 6e8ddeab2ac..7d08aee92be 100644 --- a/java/dagger/internal/codegen/base/ModuleKind.java +++ b/java/dagger/internal/codegen/base/ModuleKind.java @@ -16,10 +16,8 @@ package dagger.internal.codegen.base; -import static androidx.room.compiler.processing.compat.XConverters.toJavac; import static com.google.common.base.Preconditions.checkArgument; import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; -import static dagger.internal.codegen.langmodel.DaggerElements.isAnnotationPresent; import androidx.room.compiler.processing.XAnnotation; import androidx.room.compiler.processing.XTypeElement; @@ -30,7 +28,6 @@ import java.util.EnumSet; import java.util.Optional; import java.util.Set; -import javax.lang.model.element.TypeElement; /** Enumeration of the kinds of modules. */ public enum ModuleKind { @@ -41,7 +38,7 @@ public enum ModuleKind { PRODUCER_MODULE(TypeNames.PRODUCER_MODULE); /** Returns the annotations for modules of the given kinds. */ - public static ImmutableSet annotationsFor(Set kinds) { + private static ImmutableSet annotationsFor(Set kinds) { return kinds.stream().map(ModuleKind::annotation).collect(toImmutableSet()); } @@ -53,20 +50,9 @@ public static ImmutableSet annotationsFor(Set kinds) { * annotations */ public static Optional forAnnotatedElement(XTypeElement element) { - return forAnnotatedElement(toJavac(element)); - } - - /** - * Returns the kind of an annotated element if it is annotated with one of the module {@linkplain - * #annotation() annotations}. - * - * @throws IllegalArgumentException if the element is annotated with more than one of the module - * annotations - */ - public static Optional forAnnotatedElement(TypeElement element) { Set kinds = EnumSet.noneOf(ModuleKind.class); for (ModuleKind kind : values()) { - if (isAnnotationPresent(element, kind.annotation())) { + if (element.hasAnnotation(kind.annotation())) { kinds.add(kind); } }