1 /* 2 * Copyright (C) 2023 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.hilt.android.testing.compile; 18 19 import androidx.room.compiler.processing.XProcessingEnv; 20 import com.google.devtools.ksp.processing.SymbolProcessor; 21 import com.google.devtools.ksp.processing.SymbolProcessorEnvironment; 22 import com.google.devtools.ksp.processing.SymbolProcessorProvider; 23 import dagger.hilt.processor.internal.BaseProcessingStep; 24 import dagger.hilt.processor.internal.JavacBaseProcessingStepProcessor; 25 import dagger.hilt.processor.internal.KspBaseProcessingStepProcessor; 26 import java.util.function.Function; 27 28 /** 29 * A Javac and KSP processor to be used with the {@link HiltCompilerTests.hiltCompiler} to allow 30 * running custom processing steps during compilation tests. 31 */ 32 final class HiltCompilerProcessors { 33 /** A JavacBasicAnnotationProcessor that contains a single BaseProcessingStep. */ 34 static final class JavacProcessor extends JavacBaseProcessingStepProcessor { 35 private final Function<XProcessingEnv, BaseProcessingStep> processingStep; 36 JavacProcessor(Function<XProcessingEnv, BaseProcessingStep> processingStep)37 JavacProcessor(Function<XProcessingEnv, BaseProcessingStep> processingStep) { 38 this.processingStep = processingStep; 39 } 40 41 @Override processingStep()42 public BaseProcessingStep processingStep() { 43 return processingStep.apply(getXProcessingEnv()); 44 } 45 } 46 47 /** A KSP processor that runs the given processing steps. */ 48 static final class KspProcessor extends KspBaseProcessingStepProcessor { 49 private final Function<XProcessingEnv, BaseProcessingStep> processingStep; 50 KspProcessor( SymbolProcessorEnvironment symbolProcessorEnvironment, Function<XProcessingEnv, BaseProcessingStep> processingStep)51 private KspProcessor( 52 SymbolProcessorEnvironment symbolProcessorEnvironment, 53 Function<XProcessingEnv, BaseProcessingStep> processingStep) { 54 super(symbolProcessorEnvironment); 55 this.processingStep = processingStep; 56 } 57 58 @Override processingStep()59 public BaseProcessingStep processingStep() { 60 return processingStep.apply(getXProcessingEnv()); 61 } 62 63 /** Provides the {@link KspComponentProcessor}. */ 64 static final class Provider implements SymbolProcessorProvider { 65 private final Function<XProcessingEnv, BaseProcessingStep> processingStep; 66 Provider(Function<XProcessingEnv, BaseProcessingStep> processingStep)67 Provider(Function<XProcessingEnv, BaseProcessingStep> processingStep) { 68 this.processingStep = processingStep; 69 } 70 71 @Override create(SymbolProcessorEnvironment symbolProcessorEnvironment)72 public SymbolProcessor create(SymbolProcessorEnvironment symbolProcessorEnvironment) { 73 return new KspProcessor(symbolProcessorEnvironment, processingStep); 74 } 75 } 76 } 77 HiltCompilerProcessors()78 private HiltCompilerProcessors() {} 79 } 80