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.testing.compile; 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.javac.JavacBasicAnnotationProcessor; 23 import androidx.room.compiler.processing.ksp.KspBasicAnnotationProcessor; 24 import com.google.common.collect.ImmutableCollection; 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 javax.lang.model.SourceVersion; 30 31 /** 32 * A Javac and KSP processor to be used with the {@link CompilerTests.DaggerCompiler} to allow 33 * running custom processing steps during compilation tests. 34 */ 35 final class CompilerProcessors { 36 /** A Javac processor that runs the given processing steps. */ 37 static final class JavacProcessor extends JavacBasicAnnotationProcessor { 38 private final ImmutableCollection<XProcessingStep> processingSteps; 39 JavacProcessor(ImmutableCollection<XProcessingStep> processingSteps)40 JavacProcessor(ImmutableCollection<XProcessingStep> processingSteps) { 41 super(options -> CompilerTests.PROCESSING_ENV_CONFIG); 42 this.processingSteps = processingSteps; 43 } 44 45 @Override initialize(XProcessingEnv env)46 public void initialize(XProcessingEnv env) {} 47 48 @Override getSupportedSourceVersion()49 public SourceVersion getSupportedSourceVersion() { 50 return SourceVersion.latestSupported(); 51 } 52 53 @Override getSupportedOptions()54 public ImmutableSet<String> getSupportedOptions() { 55 return ImmutableSet.of(); 56 } 57 58 @Override processingSteps()59 public ImmutableCollection<XProcessingStep> processingSteps() { 60 return processingSteps; 61 } 62 63 @Override postRound(XProcessingEnv env, XRoundEnv roundEnv)64 public void postRound(XProcessingEnv env, XRoundEnv roundEnv) {} 65 } 66 67 /** A KSP processor that runs the given processing steps. */ 68 static final class KspProcessor extends KspBasicAnnotationProcessor { 69 private final ImmutableCollection<XProcessingStep> processingSteps; 70 KspProcessor( SymbolProcessorEnvironment symbolProcessorEnvironment, ImmutableCollection<XProcessingStep> processingSteps)71 private KspProcessor( 72 SymbolProcessorEnvironment symbolProcessorEnvironment, 73 ImmutableCollection<XProcessingStep> processingSteps) { 74 super(symbolProcessorEnvironment, CompilerTests.PROCESSING_ENV_CONFIG); 75 this.processingSteps = processingSteps; 76 } 77 78 @Override initialize(XProcessingEnv env)79 public void initialize(XProcessingEnv env) {} 80 81 @Override processingSteps()82 public ImmutableCollection<XProcessingStep> processingSteps() { 83 return processingSteps; 84 } 85 86 @Override postRound(XProcessingEnv env, XRoundEnv roundEnv)87 public void postRound(XProcessingEnv env, XRoundEnv roundEnv) {} 88 89 /** Provides the {@link KspComponentProcessor}. */ 90 static final class Provider implements SymbolProcessorProvider { 91 private final ImmutableCollection<XProcessingStep> processingSteps; 92 Provider(ImmutableCollection<XProcessingStep> processingSteps)93 Provider(ImmutableCollection<XProcessingStep> processingSteps) { 94 this.processingSteps = processingSteps; 95 } 96 97 @Override create(SymbolProcessorEnvironment symbolProcessorEnvironment)98 public SymbolProcessor create(SymbolProcessorEnvironment symbolProcessorEnvironment) { 99 return new KspProcessor(symbolProcessorEnvironment, processingSteps); 100 } 101 } 102 } 103 CompilerProcessors()104 private CompilerProcessors() {} 105 } 106