Skip to content

Commit

Permalink
Make ComponentProcessor implement JavacBasicAnnotationProcessor.
Browse files Browse the repository at this point in the history
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:
androidx/androidx@8f65ad6

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
  • Loading branch information
bcorso authored and Dagger Team committed Jul 14, 2021
1 parent feac1eb commit e152ea0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 189 deletions.
123 changes: 23 additions & 100 deletions java/dagger/internal/codegen/ComponentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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;
Expand Down Expand Up @@ -130,7 +121,7 @@ public ImmutableSet<String> getSupportedOptions() {
}

@Override
protected Iterable<? extends Step> steps() {
public Iterable<XProcessingStep> processingSteps() {
ProcessorComponent.factory()
.create(processingEnv, testingPlugins.orElseGet(this::loadExternalPlugins))
.inject(this);
Expand Down Expand Up @@ -175,13 +166,8 @@ ProcessorComponent create(

@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,
Expand All @@ -195,29 +181,27 @@ static ImmutableList<Step> processingSteps(
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);
Expand All @@ -227,65 +211,4 @@ protected void postRound(RoundEnvironment roundEnv) {
}
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());
}
}
}

This file was deleted.

0 comments on commit e152ea0

Please sign in to comment.