1 /* 2 * Copyright (C) 2014 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.internal.codegen; 18 19 import static net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING; 20 21 import androidx.room.compiler.processing.XProcessingEnv; 22 import androidx.room.compiler.processing.XProcessingStep; 23 import androidx.room.compiler.processing.XRoundEnv; 24 import androidx.room.compiler.processing.javac.JavacBasicAnnotationProcessor; 25 import com.google.auto.service.AutoService; 26 import com.google.common.annotations.VisibleForTesting; 27 import com.google.common.collect.ImmutableSet; 28 import dagger.internal.codegen.compileroption.ProcessingEnvironmentCompilerOptions; 29 import dagger.spi.model.BindingGraphPlugin; 30 import java.util.Arrays; 31 import java.util.Optional; 32 import javax.annotation.processing.Processor; 33 import javax.lang.model.SourceVersion; 34 import net.ltgt.gradle.incap.IncrementalAnnotationProcessor; 35 36 /** 37 * The Javac annotation processor responsible for generating the classes that drive the Dagger 38 * implementation. 39 */ 40 @IncrementalAnnotationProcessor(ISOLATING) 41 @AutoService(Processor.class) 42 public final class ComponentProcessor extends JavacBasicAnnotationProcessor { 43 /** 44 * Creates a component processor that uses given {@link BindingGraphPlugin}s instead of loading 45 * them from a {@link java.util.ServiceLoader}. 46 */ 47 @VisibleForTesting withTestPlugins(BindingGraphPlugin... testingPlugins)48 public static ComponentProcessor withTestPlugins(BindingGraphPlugin... testingPlugins) { 49 return withTestPlugins(Arrays.asList(testingPlugins)); 50 } 51 52 /** 53 * Creates a component processor that uses given {@link BindingGraphPlugin}s instead of loading 54 * them from a {@link java.util.ServiceLoader}. 55 */ 56 @VisibleForTesting withTestPlugins(Iterable<BindingGraphPlugin> testingPlugins)57 public static ComponentProcessor withTestPlugins(Iterable<BindingGraphPlugin> testingPlugins) { 58 return new ComponentProcessor( 59 Optional.of(ImmutableSet.copyOf(testingPlugins)), Optional.empty()); 60 } 61 62 /** 63 * Creates a component processor that uses given {@link BindingGraphPlugin}s instead of loading 64 * them from a {@link java.util.ServiceLoader}. 65 */ 66 @VisibleForTesting forTesting(dagger.spi.BindingGraphPlugin... testingPlugins)67 public static ComponentProcessor forTesting(dagger.spi.BindingGraphPlugin... testingPlugins) { 68 return forTesting(Arrays.asList(testingPlugins)); 69 } 70 71 /** 72 * Creates a component processor that uses given {@link BindingGraphPlugin}s instead of loading 73 * them from a {@link java.util.ServiceLoader}. 74 */ 75 @VisibleForTesting forTesting( Iterable<dagger.spi.BindingGraphPlugin> testingPlugins)76 public static ComponentProcessor forTesting( 77 Iterable<dagger.spi.BindingGraphPlugin> testingPlugins) { 78 return new ComponentProcessor( 79 Optional.empty(), Optional.of(ImmutableSet.copyOf(testingPlugins))); 80 } 81 82 private final DelegateComponentProcessor delegate = new DelegateComponentProcessor(); 83 private final Optional<ImmutableSet<BindingGraphPlugin>> testingPlugins; 84 private final Optional<ImmutableSet<dagger.spi.BindingGraphPlugin>> legacyTestingPlugins; 85 ComponentProcessor()86 public ComponentProcessor() { 87 this(Optional.empty(), Optional.empty()); 88 } 89 ComponentProcessor( Optional<ImmutableSet<BindingGraphPlugin>> testingPlugins, Optional<ImmutableSet<dagger.spi.BindingGraphPlugin>> legacyTestingPlugins)90 private ComponentProcessor( 91 Optional<ImmutableSet<BindingGraphPlugin>> testingPlugins, 92 Optional<ImmutableSet<dagger.spi.BindingGraphPlugin>> legacyTestingPlugins) { 93 super(options -> DelegateComponentProcessor.PROCESSING_ENV_CONFIG); 94 this.testingPlugins = testingPlugins; 95 this.legacyTestingPlugins = legacyTestingPlugins; 96 } 97 98 @Override initialize(XProcessingEnv env)99 public void initialize(XProcessingEnv env) { 100 delegate.initialize(env, testingPlugins, legacyTestingPlugins); 101 } 102 103 @Override getSupportedSourceVersion()104 public SourceVersion getSupportedSourceVersion() { 105 return SourceVersion.latestSupported(); 106 } 107 108 @Override getSupportedOptions()109 public ImmutableSet<String> getSupportedOptions() { 110 return ImmutableSet.<String>builder() 111 .addAll(ProcessingEnvironmentCompilerOptions.supportedOptions()) 112 .addAll(delegate.validationBindingGraphPlugins.allSupportedOptions()) 113 .addAll(delegate.externalBindingGraphPlugins.allSupportedOptions()) 114 .build(); 115 } 116 117 @Override processingSteps()118 public Iterable<XProcessingStep> processingSteps() { 119 return delegate.processingSteps(); 120 } 121 122 @Override preRound(XProcessingEnv env, XRoundEnv roundEnv)123 public void preRound(XProcessingEnv env, XRoundEnv roundEnv) { 124 delegate.onProcessingRoundBegin(); 125 } 126 127 @Override postRound(XProcessingEnv env, XRoundEnv roundEnv)128 public void postRound(XProcessingEnv env, XRoundEnv roundEnv) { 129 delegate.postRound(env, roundEnv); 130 } 131 } 132