1 /* 2 * Copyright (C) 2022 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 androidx.room.compiler.processing.XProcessingEnv; 20 import androidx.room.compiler.processing.XProcessingStep; 21 import androidx.room.compiler.processing.XRoundEnv; 22 import androidx.room.compiler.processing.ksp.KspBasicAnnotationProcessor; 23 import com.google.auto.service.AutoService; 24 import com.google.common.annotations.VisibleForTesting; 25 import com.google.common.collect.ImmutableSet; 26 import com.google.devtools.ksp.processing.SymbolProcessor; 27 import com.google.devtools.ksp.processing.SymbolProcessorEnvironment; 28 import com.google.devtools.ksp.processing.SymbolProcessorProvider; 29 import dagger.spi.model.BindingGraphPlugin; 30 import java.util.Arrays; 31 import java.util.Optional; 32 33 /** 34 * The KSP processor responsible for generating the classes that drive the Dagger implementation. 35 */ 36 public final class KspComponentProcessor extends KspBasicAnnotationProcessor { 37 private final DelegateComponentProcessor delegate = new DelegateComponentProcessor(); 38 private final Optional<ImmutableSet<BindingGraphPlugin>> testingPlugins; 39 KspComponentProcessor( SymbolProcessorEnvironment symbolProcessorEnvironment, Optional<ImmutableSet<BindingGraphPlugin>> testingPlugins)40 private KspComponentProcessor( 41 SymbolProcessorEnvironment symbolProcessorEnvironment, 42 Optional<ImmutableSet<BindingGraphPlugin>> testingPlugins) { 43 super(symbolProcessorEnvironment, DelegateComponentProcessor.PROCESSING_ENV_CONFIG); 44 this.testingPlugins = testingPlugins; 45 } 46 47 @Override initialize(XProcessingEnv env)48 public void initialize(XProcessingEnv env) { 49 delegate.initialize( 50 env, 51 testingPlugins, 52 // The legacy BindingGraphPlugin is only supported with Javac. 53 /* legacyTestingPlugins= */ Optional.empty()); 54 } 55 56 @Override processingSteps()57 public Iterable<XProcessingStep> processingSteps() { 58 return delegate.processingSteps(); 59 } 60 61 @Override postRound(XProcessingEnv env, XRoundEnv roundEnv)62 public void postRound(XProcessingEnv env, XRoundEnv roundEnv) { 63 delegate.postRound(env, roundEnv); 64 } 65 66 /** Provides the {@link KspComponentProcessor}. */ 67 @AutoService(SymbolProcessorProvider.class) 68 public static final class Provider implements SymbolProcessorProvider { 69 /** 70 * Creates a component processor that uses given {@link BindingGraphPlugin}s instead of loading 71 * them from a {@link java.util.ServiceLoader}. 72 */ 73 @VisibleForTesting withTestPlugins(BindingGraphPlugin... testingPlugins)74 public static Provider withTestPlugins(BindingGraphPlugin... testingPlugins) { 75 return withTestPlugins(Arrays.asList(testingPlugins)); 76 } 77 78 /** 79 * Creates a component processor that uses given {@link BindingGraphPlugin}s instead of loading 80 * them from a {@link java.util.ServiceLoader}. 81 */ 82 @VisibleForTesting withTestPlugins(Iterable<BindingGraphPlugin> testingPlugins)83 public static Provider withTestPlugins(Iterable<BindingGraphPlugin> testingPlugins) { 84 return new Provider(Optional.of(ImmutableSet.copyOf(testingPlugins))); 85 } 86 87 private final Optional<ImmutableSet<BindingGraphPlugin>> testingPlugins; 88 Provider()89 public Provider() { 90 this(Optional.empty()); 91 } 92 Provider(Optional<ImmutableSet<BindingGraphPlugin>> testingPlugins)93 private Provider(Optional<ImmutableSet<BindingGraphPlugin>> testingPlugins) { 94 this.testingPlugins = testingPlugins; 95 } 96 97 @Override create(SymbolProcessorEnvironment symbolProcessorEnvironment)98 public SymbolProcessor create(SymbolProcessorEnvironment symbolProcessorEnvironment) { 99 return new KspComponentProcessor(symbolProcessorEnvironment, testingPlugins); 100 } 101 } 102 } 103