diff --git a/java/dagger/internal/codegen/ComponentProcessingStep.java b/java/dagger/internal/codegen/ComponentProcessingStep.java index 8f8e8763bc6..66b325233f7 100644 --- a/java/dagger/internal/codegen/ComponentProcessingStep.java +++ b/java/dagger/internal/codegen/ComponentProcessingStep.java @@ -24,9 +24,7 @@ import static java.util.Collections.disjoint; import androidx.room.compiler.processing.XMessager; -import androidx.room.compiler.processing.XProcessingEnv; import androidx.room.compiler.processing.XTypeElement; -import androidx.room.compiler.processing.compat.XConverters; import com.google.auto.common.BasicAnnotationProcessor.ProcessingStep; import com.google.common.collect.ImmutableSet; import com.google.errorprone.annotations.CanIgnoreReturnValue; @@ -44,7 +42,6 @@ import dagger.internal.codegen.validation.ValidationReport; import java.util.Set; import javax.inject.Inject; -import javax.lang.model.element.TypeElement; /** * A {@link ProcessingStep} that is responsible for dealing with a component or production component @@ -59,7 +56,6 @@ final class ComponentProcessingStep extends TypeCheckingProcessingStep componentGenerator; private final BindingGraphValidator bindingGraphValidator; - private final XProcessingEnv processingEnv; @Inject ComponentProcessingStep( @@ -70,8 +66,7 @@ final class ComponentProcessingStep extends TypeCheckingProcessingStep componentGenerator, - BindingGraphValidator bindingGraphValidator, - XProcessingEnv processingEnv) { + BindingGraphValidator bindingGraphValidator) { this.messager = messager; this.componentValidator = componentValidator; this.creatorValidator = creatorValidator; @@ -80,7 +75,6 @@ final class ComponentProcessingStep extends TypeCheckingProcessingStep componentDependencies, ImmutableSet transitiveModules, - ImmutableMap dependenciesByDependencyMethod, + ImmutableMap dependenciesByDependencyMethod, ImmutableSet scopes, ImmutableSet subcomponentsFromModules, ImmutableBiMap subcomponentsByFactoryMethod, ImmutableBiMap subcomponentsByBuilderMethod, ImmutableSet componentMethods, Optional creator) { - return new AutoValue_ComponentDescriptor( - componentAnnotation, - component, - componentDependencies, - transitiveModules, - dependenciesByDependencyMethod, - scopes, - subcomponentsFromModules, - subcomponentsByFactoryMethod, - subcomponentsByBuilderMethod, - componentMethods, - creator); + ComponentDescriptor descriptor = + new AutoValue_ComponentDescriptor( + componentAnnotation, + component, + componentDependencies, + transitiveModules, + dependenciesByDependencyMethod, + scopes, + subcomponentsFromModules, + subcomponentsByFactoryMethod, + subcomponentsByBuilderMethod, + componentMethods, + creator); + descriptor.processingEnv = processingEnv; + return descriptor; } + // This is required temporarily during the XProcessing migration to use toXProcessing(). + private XProcessingEnv processingEnv; + /** The annotation that specifies that {@link #typeElement()} is a component. */ public abstract ComponentAnnotation annotation(); @@ -124,7 +136,7 @@ public final boolean isRealComponent() { * The element that defines the component. This is the element to which the {@link #annotation()} * was applied. */ - public abstract TypeElement typeElement(); + public abstract XTypeElement typeElement(); /** * The set of component dependencies listed in {@link Component#dependencies} or {@link @@ -187,15 +199,18 @@ ImmutableSet requirements() { * the enclosing type of the method; a method may be declared by a supertype of the actual * dependency. */ - public abstract ImmutableMap + public abstract ImmutableMap dependenciesByDependencyMethod(); /** The {@linkplain #dependencies() component dependency} that defines a method. */ - public final ComponentRequirement getDependencyThatDefinesMethod(Element method) { - checkArgument( - method instanceof ExecutableElement, "method must be an executable element: %s", method); - return checkNotNull( - dependenciesByDependencyMethod().get(method), "no dependency implements %s", method); + public final ComponentRequirement getDependencyThatDefinesMethod(Element javaMethod) { + XElement method = toXProcessing(javaMethod, processingEnv); + checkArgument(isMethod(method), "method must be an executable element: %s", method); + checkState( + dependenciesByDependencyMethod().containsKey(method), + "no dependency implements %s", + method); + return dependenciesByDependencyMethod().get(method); } /** The scopes of the component. */ @@ -230,7 +245,7 @@ public final ImmutableSet childComponents() { /** Returns a map of {@link #childComponents()} indexed by {@link #typeElement()}. */ @Memoized - public ImmutableMap childComponentsByElement() { + public ImmutableMap childComponentsByElement() { return Maps.uniqueIndex(childComponents(), ComponentDescriptor::typeElement); } @@ -312,7 +327,8 @@ public final boolean hasCreator() { */ public final Optional cancellationPolicy() { return isProduction() - ? Optional.ofNullable(typeElement().getAnnotation(CancellationPolicy.class)) + // TODO(bcorso): Get values from XAnnotation instead of using CancellationPolicy directly. + ? Optional.ofNullable(toJavac(typeElement()).getAnnotation(CancellationPolicy.class)) : Optional.empty(); } @@ -387,6 +403,14 @@ public interface Builder { private static final ImmutableSet NON_CONTRIBUTING_OBJECT_METHOD_NAMES = ImmutableSet.of("toString", "hashCode", "clone", "getClass"); + /** + * Returns {@code true} if a method could be a component entry point but not a members-injection + * method. + */ + static boolean isComponentContributionMethod(XMethodElement method) { + return isComponentContributionMethod(toJavac(method)); + } + /** * Returns {@code true} if a method could be a component entry point but not a members-injection * method. diff --git a/java/dagger/internal/codegen/binding/ComponentDescriptorFactory.java b/java/dagger/internal/codegen/binding/ComponentDescriptorFactory.java index c1a89ca83ec..ac0170ed06c 100644 --- a/java/dagger/internal/codegen/binding/ComponentDescriptorFactory.java +++ b/java/dagger/internal/codegen/binding/ComponentDescriptorFactory.java @@ -111,14 +111,14 @@ private ComponentDescriptor create( .map(ComponentRequirement::forDependency) .collect(toImmutableSet()); - ImmutableMap.Builder dependenciesByDependencyMethod = + ImmutableMap.Builder dependenciesByDependencyMethod = ImmutableMap.builder(); - for (ComponentRequirement componentDependency : componentDependencies) { for (ExecutableElement dependencyMethod : methodsIn(elements.getAllMembers(componentDependency.typeElement()))) { if (isComponentContributionMethod(dependencyMethod)) { - dependenciesByDependencyMethod.put(dependencyMethod, componentDependency); + dependenciesByDependencyMethod.put( + (XMethodElement) toXProcessing(dependencyMethod, processingEnv), componentDependency); } } } @@ -185,8 +185,9 @@ private ComponentDescriptor create( } return ComponentDescriptor.create( + processingEnv, componentAnnotation, - toJavac(typeElement), + typeElement, componentDependencies, transitiveModules, dependenciesByDependencyMethod.build(), diff --git a/java/dagger/internal/codegen/binding/LegacyBindingGraph.java b/java/dagger/internal/codegen/binding/LegacyBindingGraph.java index ddc96975823..2f8d39a4620 100644 --- a/java/dagger/internal/codegen/binding/LegacyBindingGraph.java +++ b/java/dagger/internal/codegen/binding/LegacyBindingGraph.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.binding; +import androidx.room.compiler.processing.XTypeElement; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; @@ -25,7 +26,6 @@ import dagger.spi.model.RequestKind; import java.util.Collection; import java.util.Map; -import javax.lang.model.element.TypeElement; // TODO(bcorso): Remove the LegacyBindingGraph after we've migrated to the new BindingGraph. /** The canonical representation of a full-resolved graph. */ @@ -69,7 +69,7 @@ ImmutableList subgraphs() { private static ImmutableList checkForDuplicates( ImmutableList graphs) { - Map> duplicateGraphs = + Map> duplicateGraphs = Maps.filterValues( Multimaps.index(graphs, graph -> graph.componentDescriptor().typeElement()).asMap(), overlapping -> overlapping.size() > 1); diff --git a/java/dagger/internal/codegen/binding/ResolvedBindings.java b/java/dagger/internal/codegen/binding/ResolvedBindings.java index a991d067623..2ef9bd63f78 100644 --- a/java/dagger/internal/codegen/binding/ResolvedBindings.java +++ b/java/dagger/internal/codegen/binding/ResolvedBindings.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.binding; +import static androidx.room.compiler.processing.compat.XConverters.toJavac; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.Iterables.getOnlyElement; @@ -99,7 +100,7 @@ && optionalBindingDeclarations().isEmpty() /** All bindings for {@link #key()} that are owned by a component. */ ImmutableSet bindingsOwnedBy(ComponentDescriptor component) { - return allBindings().get(component.typeElement()); + return allBindings().get(toJavac(component.typeElement())); } /** @@ -151,7 +152,7 @@ static ResolvedBindings forMembersInjectionBinding( return new AutoValue_ResolvedBindings( key, ImmutableSetMultimap.of(), - ImmutableMap.of(owningComponent.typeElement(), ownedMembersInjectionBinding), + ImmutableMap.of(toJavac(owningComponent.typeElement()), ownedMembersInjectionBinding), ImmutableSet.of(), ImmutableSet.of(), ImmutableSet.of()); diff --git a/java/dagger/internal/codegen/binding/SourceFiles.java b/java/dagger/internal/codegen/binding/SourceFiles.java index fbd216d0cef..7f54482ca5e 100644 --- a/java/dagger/internal/codegen/binding/SourceFiles.java +++ b/java/dagger/internal/codegen/binding/SourceFiles.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.binding; +import static androidx.room.compiler.processing.compat.XConverters.toJavac; import static com.google.common.base.CaseFormat.LOWER_CAMEL; import static com.google.common.base.CaseFormat.UPPER_CAMEL; import static com.google.common.base.Preconditions.checkArgument; @@ -41,7 +42,6 @@ import static javax.lang.model.SourceVersion.isName; import androidx.room.compiler.processing.XTypeElement; -import androidx.room.compiler.processing.compat.XConverters; import com.google.auto.common.MoreElements; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; @@ -201,11 +201,7 @@ public static String classFileName(ClassName className) { } public static ClassName generatedMonitoringModuleName(XTypeElement componentElement) { - return generatedMonitoringModuleName(XConverters.toJavac(componentElement)); - } - - public static ClassName generatedMonitoringModuleName(TypeElement componentElement) { - return siblingClassName(componentElement, "_MonitoringModule"); + return siblingClassName(toJavac(componentElement), "_MonitoringModule"); } // TODO(ronshapiro): when JavaPoet migration is complete, replace the duplicated code diff --git a/java/dagger/internal/codegen/compileroption/CompilerOptions.java b/java/dagger/internal/codegen/compileroption/CompilerOptions.java index a0d1cda347f..13dc8618170 100644 --- a/java/dagger/internal/codegen/compileroption/CompilerOptions.java +++ b/java/dagger/internal/codegen/compileroption/CompilerOptions.java @@ -16,6 +16,9 @@ package dagger.internal.codegen.compileroption; +import static androidx.room.compiler.processing.compat.XConverters.toJavac; + +import androidx.room.compiler.processing.XTypeElement; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic; @@ -23,6 +26,18 @@ public abstract class CompilerOptions { public abstract boolean usesProducers(); + /** + * Returns true if the fast initialization flag, {@code fastInit}, is enabled. + * + *

If enabled, the generated code will attempt to optimize for fast component initialization. + * This is done by reducing the number of factory classes loaded during initialization and the + * number of eagerly initialized fields at the cost of potential memory leaks and higher + * per-provision instantiation time. + */ + public final boolean fastInit(XTypeElement element) { + return fastInit(toJavac(element)); + } + /** * Returns true if the fast initialization flag, {@code fastInit}, is enabled. * diff --git a/java/dagger/internal/codegen/componentgenerator/ComponentHjarGenerator.java b/java/dagger/internal/codegen/componentgenerator/ComponentHjarGenerator.java index fdeeb3244f7..c1f6cfac266 100644 --- a/java/dagger/internal/codegen/componentgenerator/ComponentHjarGenerator.java +++ b/java/dagger/internal/codegen/componentgenerator/ComponentHjarGenerator.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.componentgenerator; +import static androidx.room.compiler.processing.compat.XConverters.toJavac; import static com.google.common.base.CaseFormat.LOWER_CAMEL; import static com.google.common.base.CaseFormat.UPPER_CAMEL; import static com.google.common.base.Preconditions.checkArgument; @@ -34,6 +35,7 @@ import androidx.room.compiler.processing.XFiler; import androidx.room.compiler.processing.XMethodElement; +import androidx.room.compiler.processing.XTypeElement; import com.google.auto.common.MoreTypes; import com.google.common.base.Ascii; import com.google.common.collect.ImmutableList; @@ -94,7 +96,7 @@ final class ComponentHjarGenerator extends SourceFileGenerator topLevelTypes(ComponentDescriptor compone TypeSpec.classBuilder(generatedTypeName) .addModifiers(FINAL) .addMethod(privateConstructor()); - if (componentDescriptor.typeElement().getModifiers().contains(PUBLIC)) { + if (componentDescriptor.typeElement().isPublic()) { generatedComponent.addModifiers(PUBLIC); } - TypeElement componentElement = componentDescriptor.typeElement(); + XTypeElement componentElement = componentDescriptor.typeElement(); addSupertype(generatedComponent, componentElement); TypeName builderMethodReturnType; @@ -124,7 +126,7 @@ public ImmutableList topLevelTypes(ComponentDescriptor compone TypeSpec.classBuilder("Builder") .addModifiers(STATIC, FINAL) .addMethod(privateConstructor()); - if (componentDescriptor.typeElement().getModifiers().contains(PUBLIC)) { + if (componentDescriptor.typeElement().isPublic()) { builder.addModifiers(PUBLIC); } @@ -149,7 +151,7 @@ && componentRequirements(componentDescriptor) generatedComponent.addMethod(createMethod(componentDescriptor)); } - DeclaredType componentType = MoreTypes.asDeclared(componentElement.asType()); + DeclaredType componentType = MoreTypes.asDeclared(toJavac(componentElement).asType()); // TODO(ronshapiro): unify with ComponentImplementationBuilder Set methodSignatures = Sets.newHashSetWithExpectedSize(componentDescriptor.componentMethods().size()); @@ -162,7 +164,7 @@ && componentRequirements(componentDescriptor) .forEach( method -> generatedComponent.addMethod( - emptyComponentMethod(componentElement, method.methodElement()))); + emptyComponentMethod(toJavac(componentElement), method.methodElement()))); if (componentDescriptor.isProduction()) { generatedComponent @@ -199,7 +201,7 @@ private static Stream componentRequirements(ComponentDescr !module.moduleElement().getModifiers().contains(ABSTRACT) && isElementAccessibleFrom( module.moduleElement(), - ClassName.get(component.typeElement()).packageName())) + component.typeElement().getClassName().packageName())) .map(module -> ComponentRequirement.forModule(module.moduleElement().asType()))); } @@ -230,7 +232,7 @@ private static MethodSpec builderSetterMethod( private static MethodSpec builderBuildMethod(ComponentDescriptor component) { return MethodSpec.methodBuilder("build") .addModifiers(PUBLIC) - .returns(ClassName.get(component.typeElement())) + .returns(component.typeElement().getClassName()) .build(); } @@ -245,7 +247,7 @@ private static MethodSpec staticCreatorMethod( private static MethodSpec createMethod(ComponentDescriptor componentDescriptor) { return MethodSpec.methodBuilder("create") .addModifiers(PUBLIC, STATIC) - .returns(ClassName.get(componentDescriptor.typeElement())) + .returns(componentDescriptor.typeElement().getClassName()) .build(); } diff --git a/java/dagger/internal/codegen/validation/ComponentDescriptorValidator.java b/java/dagger/internal/codegen/validation/ComponentDescriptorValidator.java index d4a3a0d675e..ad6b789fd92 100644 --- a/java/dagger/internal/codegen/validation/ComponentDescriptorValidator.java +++ b/java/dagger/internal/codegen/validation/ComponentDescriptorValidator.java @@ -18,6 +18,7 @@ import static androidx.room.compiler.processing.XElementKt.isMethod; import static androidx.room.compiler.processing.XElementKt.isMethodParameter; +import static androidx.room.compiler.processing.compat.XConverters.toJavac; import static androidx.room.compiler.processing.compat.XConverters.toXProcessing; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -147,7 +148,8 @@ private ValidationReport.Builder report(ComponentDescriptor component) { private void reportComponentItem( Diagnostic.Kind kind, ComponentDescriptor component, String message) { report(component) - .addItem(message, kind, component.typeElement(), component.annotation().annotation()); + .addItem( + message, kind, toJavac(component.typeElement()), component.annotation().annotation()); } private void reportComponentError(ComponentDescriptor component, String error) { @@ -164,7 +166,8 @@ void visitComponent(ComponentDescriptor component) { /** Validates that component dependencies do not form a cycle. */ private void validateComponentDependencyHierarchy(ComponentDescriptor component) { - validateComponentDependencyHierarchy(component, component.typeElement(), new ArrayDeque<>()); + validateComponentDependencyHierarchy( + component, toJavac(component.typeElement()), new ArrayDeque<>()); } /** Recursive method to validate that component dependencies do not form a cycle. */ @@ -235,7 +238,10 @@ private void validateDependencyScopes(ComponentDescriptor component) { // Dagger 1.x scope compatibility requires this be suppress-able. if (!compilerOptions.scopeCycleValidationType().equals(ValidationType.NONE)) { validateDependencyScopeHierarchy( - component, component.typeElement(), new ArrayDeque<>(), new ArrayDeque<>()); + component, + toJavac(component.typeElement()), + new ArrayDeque<>(), + new ArrayDeque<>()); } } } else { diff --git a/java/dagger/internal/codegen/validation/ComponentHierarchyValidator.java b/java/dagger/internal/codegen/validation/ComponentHierarchyValidator.java index e4a6334e1a1..be55da0a7d7 100644 --- a/java/dagger/internal/codegen/validation/ComponentHierarchyValidator.java +++ b/java/dagger/internal/codegen/validation/ComponentHierarchyValidator.java @@ -16,6 +16,7 @@ package dagger.internal.codegen.validation; +import static androidx.room.compiler.processing.compat.XConverters.toJavac; import static com.google.common.base.Functions.constant; import static com.google.common.base.Predicates.and; import static com.google.common.base.Predicates.in; @@ -65,7 +66,9 @@ ValidationReport validate(ComponentDescriptor componentDescriptor) { validateSubcomponentMethods( report, componentDescriptor, - Maps.toMap(componentDescriptor.moduleTypes(), constant(componentDescriptor.typeElement()))); + Maps.toMap( + componentDescriptor.moduleTypes(), + constant(toJavac(componentDescriptor.typeElement())))); validateRepeatedScopedDeclarations(report, componentDescriptor, LinkedHashMultimap.create()); if (compilerOptions.scopeCycleValidationType().diagnosticKind().isPresent()) { @@ -102,7 +105,7 @@ private void validateSubcomponentMethods( Maps.toMap( Sets.difference( childComponent.moduleTypes(), existingModuleToOwners.keySet()), - constant(childComponent.typeElement()))) + constant(toJavac(childComponent.typeElement())))) .build()); }); } diff --git a/java/dagger/internal/codegen/writing/ComponentCreatorImplementationFactory.java b/java/dagger/internal/codegen/writing/ComponentCreatorImplementationFactory.java index 05fbdff817b..4c7ad46546b 100644 --- a/java/dagger/internal/codegen/writing/ComponentCreatorImplementationFactory.java +++ b/java/dagger/internal/codegen/writing/ComponentCreatorImplementationFactory.java @@ -38,7 +38,6 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; import com.google.common.collect.Sets; -import com.squareup.javapoet.ClassName; import com.squareup.javapoet.CodeBlock; import com.squareup.javapoet.FieldSpec; import com.squareup.javapoet.MethodSpec; @@ -281,7 +280,7 @@ private void addFactoryMethod() { MethodSpec factoryMethod() { MethodSpec.Builder factoryMethod = factoryMethodBuilder(); factoryMethod - .returns(ClassName.get(componentDescriptor().typeElement())) + .returns(componentDescriptor().typeElement().getClassName()) .addModifiers(PUBLIC); ImmutableMap factoryMethodParameters = @@ -473,11 +472,9 @@ protected ImmutableMap userSettableRequ @Override protected Optional visibility() { - return componentImplementation - .componentDescriptor() - .typeElement() - .getModifiers() - .contains(PUBLIC) ? Optional.of(PUBLIC) : Optional.empty(); + return componentImplementation.componentDescriptor().typeElement().isPublic() + ? Optional.of(PUBLIC) + : Optional.empty(); } @Override diff --git a/java/dagger/internal/codegen/writing/ComponentNames.java b/java/dagger/internal/codegen/writing/ComponentNames.java index d7922ff6204..30e4e41eff3 100644 --- a/java/dagger/internal/codegen/writing/ComponentNames.java +++ b/java/dagger/internal/codegen/writing/ComponentNames.java @@ -51,7 +51,7 @@ public final class ComponentNames { /** Returns the class name for the root component. */ public static ClassName getRootComponentClassName(ComponentDescriptor componentDescriptor) { checkState(!componentDescriptor.isSubcomponent()); - ClassName componentName = ClassName.get(componentDescriptor.typeElement()); + ClassName componentName = componentDescriptor.typeElement().getClassName(); return ClassName.get(componentName.packageName(), "Dagger" + classFileName(componentName)); } diff --git a/java/dagger/internal/codegen/writing/ProvisionBindingRepresentation.java b/java/dagger/internal/codegen/writing/ProvisionBindingRepresentation.java index 5639a32367a..2d9e5776c6f 100644 --- a/java/dagger/internal/codegen/writing/ProvisionBindingRepresentation.java +++ b/java/dagger/internal/codegen/writing/ProvisionBindingRepresentation.java @@ -20,6 +20,7 @@ import static dagger.internal.codegen.writing.StaticFactoryInstanceSupplier.usesStaticFactoryCreation; import static dagger.spi.model.BindingKind.DELEGATE; +import androidx.room.compiler.processing.XTypeElement; import dagger.assisted.Assisted; import dagger.assisted.AssistedFactory; import dagger.assisted.AssistedInject; @@ -29,7 +30,6 @@ import dagger.internal.codegen.compileroption.CompilerOptions; import dagger.internal.codegen.langmodel.DaggerTypes; import dagger.spi.model.RequestKind; -import javax.lang.model.element.TypeElement; /** * A binding representation that wraps code generation methods that satisfy all kinds of request for @@ -56,7 +56,7 @@ final class ProvisionBindingRepresentation implements BindingRepresentation { DaggerTypes types) { this.binding = binding; this.graph = graph; - TypeElement rootComponent = + XTypeElement rootComponent = componentImplementation.rootComponentImplementation().componentDescriptor().typeElement(); this.isFastInit = compilerOptions.fastInit(rootComponent); this.directInstanceBindingRepresentation =