Make ComponentProcessor implement JavacBasicAnnotationProcessor.
This CL makes ComponentProcessor extend JavacBasicAnnotationProcessor, which can handle XProcessingStep and XProcessingEnv directly rather than having to convert it ourselves.
This CL also patches in the fixes to JavacBasicAnnotationProcessor, which is required for this change:
https://github.com/androidx/androidx/commit/8f65ad670a8c0650dca3567611ff8d8f13600117
At some point we will have a KspComponentProcessor that extends KspBasicAnnotationProcessor. At that point we'll likely want to refactor a lot of the code in ComponentProcessor into a separate class that can be shared by both KspComponentProcessor and ComponentProcessor, but for now I've kept it all in ComponentProcessor.
RELNOTES=N/A
PiperOrigin-RevId: 384769549
diff --git a/java/dagger/internal/codegen/ComponentProcessor.java b/java/dagger/internal/codegen/ComponentProcessor.java
index 41bf5d8..0684dde 100644
--- a/java/dagger/internal/codegen/ComponentProcessor.java
+++ b/java/dagger/internal/codegen/ComponentProcessor.java
@@ -16,23 +16,18 @@
package dagger.internal.codegen;
-import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
-import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.XProcessingStep;
+import androidx.room.compiler.processing.XRoundEnv;
import androidx.room.compiler.processing.compat.XConverters;
-import com.google.auto.common.BasicAnnotationProcessor;
-import com.google.auto.common.BasicAnnotationProcessor.Step;
+import androidx.room.compiler.processing.javac.JavacBasicAnnotationProcessor;
import com.google.auto.service.AutoService;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.ImmutableSetMultimap;
-import com.google.common.collect.Maps;
import com.google.errorprone.annotations.CheckReturnValue;
-import dagger.Binds;
import dagger.BindsInstance;
import dagger.Component;
import dagger.Module;
@@ -55,19 +50,15 @@
import dagger.internal.codegen.validation.MonitoringModuleProcessingStep;
import dagger.internal.codegen.validation.MultibindingAnnotationsProcessingStep;
import dagger.internal.codegen.validation.ValidationBindingGraphPlugins;
-import dagger.multibindings.IntoSet;
import dagger.spi.BindingGraphPlugin;
import java.util.Arrays;
import java.util.Optional;
import java.util.Set;
-import java.util.stream.Stream;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
-import javax.annotation.processing.RoundEnvironment;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.lang.model.SourceVersion;
-import javax.lang.model.element.Element;
import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;
/**
@@ -78,13 +69,13 @@
*/
@IncrementalAnnotationProcessor(ISOLATING)
@AutoService(Processor.class)
-public class ComponentProcessor extends BasicAnnotationProcessor {
+public class ComponentProcessor extends JavacBasicAnnotationProcessor {
private final Optional<ImmutableSet<BindingGraphPlugin>> testingPlugins;
@Inject InjectBindingRegistry injectBindingRegistry;
@Inject SourceFileGenerator<ProvisionBinding> factoryGenerator;
@Inject SourceFileGenerator<MembersInjectionBinding> membersInjectorGenerator;
- @Inject ImmutableList<Step> processingSteps;
+ @Inject ImmutableList<XProcessingStep> processingSteps;
@Inject ValidationBindingGraphPlugins validationBindingGraphPlugins;
@Inject ExternalBindingGraphPlugins externalBindingGraphPlugins;
@Inject Set<ClearableCache> clearableCaches;
@@ -130,7 +121,7 @@
}
@Override
- protected Iterable<? extends Step> steps() {
+ public Iterable<XProcessingStep> processingSteps() {
ProcessorComponent.factory()
.create(processingEnv, testingPlugins.orElseGet(this::loadExternalPlugins))
.inject(this);
@@ -175,13 +166,8 @@
@Module
interface ProcessingStepsModule {
- @Binds
- @IntoSet
- ClearableCache bindXProcessingEnvCache(XProcessingEnvCache cache);
-
@Provides
- static ImmutableList<Step> processingSteps(
- XProcessingEnvCache xProcessingEnvCache,
+ static ImmutableList<XProcessingStep> processingSteps(
MapKeyProcessingStep mapKeyProcessingStep,
InjectProcessingStep injectProcessingStep,
AssistedInjectProcessingStep assistedInjectProcessingStep,
@@ -195,29 +181,27 @@
ComponentHjarProcessingStep componentHjarProcessingStep,
BindingMethodProcessingStep bindingMethodProcessingStep,
CompilerOptions compilerOptions) {
- return Stream.of(
- mapKeyProcessingStep,
- injectProcessingStep,
- assistedInjectProcessingStep,
- assistedFactoryProcessingStep,
- assistedProcessingStep,
- monitoringModuleProcessingStep,
- multibindingAnnotationsProcessingStep,
- bindsInstanceProcessingStep,
- moduleProcessingStep,
- compilerOptions.headerCompilation()
- ? componentHjarProcessingStep
- : componentProcessingStep,
- bindingMethodProcessingStep)
- // TODO(bcorso): Remove DelegatingStep once we've migrated to XBasicAnnotationProcessor.
- .map(step -> DelegatingStep.create(xProcessingEnvCache, step))
- .collect(toImmutableList());
+ return ImmutableList.of(
+ mapKeyProcessingStep,
+ injectProcessingStep,
+ assistedInjectProcessingStep,
+ assistedFactoryProcessingStep,
+ assistedProcessingStep,
+ monitoringModuleProcessingStep,
+ multibindingAnnotationsProcessingStep,
+ bindsInstanceProcessingStep,
+ moduleProcessingStep,
+ compilerOptions.headerCompilation()
+ ? componentHjarProcessingStep
+ : componentProcessingStep,
+ bindingMethodProcessingStep);
}
}
@Override
- protected void postRound(RoundEnvironment roundEnv) {
- if (!roundEnv.processingOver()) {
+ public void postRound(XProcessingEnv env, XRoundEnv roundEnv) {
+ // TODO(bcorso): Add a way to determine if processing is over without converting to Javac here.
+ if (!XConverters.toJavac(roundEnv).processingOver()) {
try {
injectBindingRegistry.generateSourcesForRequiredBindings(
factoryGenerator, membersInjectorGenerator);
@@ -227,65 +211,4 @@
}
clearableCaches.forEach(ClearableCache::clearCache);
}
-
- /** An {@link XProcessingEnv} cache that clears tha cache on each processing round. */
- @Singleton
- static final class XProcessingEnvCache implements ClearableCache {
- private final ProcessingEnvironment processingEnv;
- private XProcessingEnv xProcessingEnv;
-
- @Inject
- XProcessingEnvCache(ProcessingEnvironment processingEnv) {
- this.processingEnv = processingEnv;
- }
-
- @Override
- public void clearCache() {
- xProcessingEnv = null;
- }
-
- public XProcessingEnv get() {
- if (xProcessingEnv == null) {
- xProcessingEnv = XProcessingEnv.create(processingEnv);
- }
- return xProcessingEnv;
- }
- }
-
- /** A {@link Step} that delegates to a {@link XProcessingStep}. */
- private static final class DelegatingStep implements Step {
- static Step create(XProcessingEnvCache xProcessingEnvCache, XProcessingStep xProcessingStep) {
- return new DelegatingStep(xProcessingEnvCache, xProcessingStep);
- }
-
- private final XProcessingEnvCache xProcessingEnvCache;
- private final XProcessingStep delegate;
-
- DelegatingStep(XProcessingEnvCache xProcessingEnvCache, XProcessingStep delegate) {
- this.xProcessingEnvCache = xProcessingEnvCache;
- this.delegate = delegate;
- }
-
- @Override
- public Set<String> annotations() {
- return delegate.annotations();
- }
-
- @Override
- public Set<? extends Element> process(
- ImmutableSetMultimap<String, Element> elementsByAnnotation) {
- XProcessingEnv xProcessingEnv = xProcessingEnvCache.get();
- return delegate.process(
- xProcessingEnv,
- Maps.transformValues(
- elementsByAnnotation.asMap(),
- javacElements ->
- javacElements.stream()
- .map(element -> XConverters.toXProcessing(element, xProcessingEnv))
- .collect(toImmutableSet())))
- .stream()
- .map(XConverters::toJavac)
- .collect(toImmutableSet());
- }
- }
}
diff --git a/java/dagger/internal/codegen/validation/TypeCheckingProcessingStep.java b/java/dagger/internal/codegen/validation/TypeCheckingProcessingStep.java
deleted file mode 100644
index 5c875ea..0000000
--- a/java/dagger/internal/codegen/validation/TypeCheckingProcessingStep.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Copyright (C) 2018 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.internal.codegen.validation;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Preconditions.checkState;
-import static com.google.common.collect.Sets.difference;
-import static dagger.internal.codegen.extension.DaggerStreams.toImmutableMap;
-import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
-
-import com.google.auto.common.BasicAnnotationProcessor.Step;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.ImmutableSetMultimap;
-import com.squareup.javapoet.ClassName;
-import java.util.Set;
-import java.util.function.Function;
-import javax.lang.model.element.Element;
-
-/**
- * A {@link ProcessingStep} that processes one element at a time and defers any for which {@link
- * TypeNotPresentException} is thrown.
- */
-// TODO(dpb): Contribute to auto-common.
-public abstract class TypeCheckingProcessingStep<E extends Element> implements Step {
- private final Function<Element, E> downcaster;
-
- protected TypeCheckingProcessingStep(Function<Element, E> downcaster) {
- this.downcaster = checkNotNull(downcaster);
- }
-
- @Override
- public final ImmutableSet<String> annotations() {
- return annotationClassNames().stream().map(ClassName::canonicalName).collect(toImmutableSet());
- }
-
- @Override
- public ImmutableSet<Element> process(ImmutableSetMultimap<String, Element> elementsByAnnotation) {
- ImmutableMap<String, ClassName> annotationClassNames =
- annotationClassNames().stream()
- .collect(toImmutableMap(ClassName::canonicalName, className -> className));
- checkState(
- annotationClassNames.keySet().containsAll(elementsByAnnotation.keySet()),
- "Unexpected annotations for %s: %s",
- this.getClass().getName(),
- difference(elementsByAnnotation.keySet(), annotationClassNames.keySet()));
-
- ImmutableSet.Builder<Element> deferredElements = ImmutableSet.builder();
- ImmutableSetMultimap.copyOf(elementsByAnnotation)
- .inverse()
- .asMap()
- .forEach(
- (element, annotations) -> {
- try {
- process(
- downcaster.apply(element),
- annotations.stream().map(annotationClassNames::get).collect(toImmutableSet()));
- } catch (TypeNotPresentException e) {
- deferredElements.add(element);
- }
- });
- return deferredElements.build();
- }
-
- /**
- * Processes one element. If this method throws {@link TypeNotPresentException}, the element will
- * be deferred until the next round of processing.
- *
- * @param annotations the subset of {@link Step#annotations()} that annotate {@code element}
- */
- protected abstract void process(E element, ImmutableSet<ClassName> annotations);
-
- /** Returns the set of annotations processed by this {@link Step}. */
- protected abstract Set<ClassName> annotationClassNames();
-}