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.processor.internal; 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 com.google.common.collect.ImmutableList; 24 import com.google.common.collect.ImmutableSet; 25 import java.util.Set; 26 import javax.lang.model.SourceVersion; 27 28 /** A JavacBasicAnnotationProcessor that contains a single BaseProcessingStep. */ 29 public abstract class JavacBaseProcessingStepProcessor extends JavacBasicAnnotationProcessor { 30 private BaseProcessingStep processingStep; 31 JavacBaseProcessingStepProcessor()32 public JavacBaseProcessingStepProcessor() { 33 super(HiltProcessingEnvConfigs.CONFIGS); 34 } 35 36 @Override initialize(XProcessingEnv env)37 public void initialize(XProcessingEnv env) { 38 HiltCompilerOptions.checkWrongAndDeprecatedOptions(env); 39 processingStep = processingStep(); 40 } 41 42 @Override getSupportedSourceVersion()43 public final SourceVersion getSupportedSourceVersion() { 44 return SourceVersion.latestSupported(); 45 } 46 47 @Override getSupportedOptions()48 public final ImmutableSet<String> getSupportedOptions() { 49 // This is declared here rather than in the actual processors because KAPT will issue a 50 // warning if any used option is not unsupported. This can happen when there is a module 51 // which uses Hilt but lacks any @AndroidEntryPoint annotations. 52 // See: https://github.com/google/dagger/issues/2040 53 return ImmutableSet.<String>builder() 54 .addAll(HiltCompilerOptions.getProcessorOptions()) 55 .addAll(additionalProcessingOptions()) 56 .build(); 57 } 58 59 @Override processingSteps()60 public final ImmutableList<XProcessingStep> processingSteps() { 61 return ImmutableList.of(processingStep); 62 } 63 64 @Override preRound(XProcessingEnv env, XRoundEnv round)65 public void preRound(XProcessingEnv env, XRoundEnv round) { 66 processingStep.preRoundProcess(env, round); 67 } 68 processingStep()69 protected abstract BaseProcessingStep processingStep(); 70 71 @Override postRound(XProcessingEnv env, XRoundEnv round)72 public void postRound(XProcessingEnv env, XRoundEnv round) { 73 processingStep.postRoundProcess(env, round); 74 } 75 76 /** Returns additional processing options that should only be applied for a single processor. */ additionalProcessingOptions()77 protected Set<String> additionalProcessingOptions() { 78 return ImmutableSet.of(); 79 } 80 } 81