1 /* 2 * Copyright (C) 2020 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.processor.internal.androidentrypoint; 18 19 import static dagger.hilt.processor.internal.HiltCompilerOptions.useAggregatingRootProcessor; 20 import static net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING; 21 22 import com.google.auto.service.AutoService; 23 import com.google.common.collect.ImmutableSet; 24 import dagger.hilt.android.processor.internal.AndroidClassNames; 25 import dagger.hilt.processor.internal.BaseProcessor; 26 import dagger.hilt.processor.internal.ProcessorErrors; 27 import java.util.Set; 28 import javax.annotation.processing.Processor; 29 import javax.lang.model.element.Element; 30 import javax.lang.model.element.TypeElement; 31 import net.ltgt.gradle.incap.IncrementalAnnotationProcessor; 32 33 /** 34 * Processor that creates a module for classes marked with {@link 35 * dagger.hilt.android.AndroidEntryPoint}. 36 */ 37 @IncrementalAnnotationProcessor(ISOLATING) 38 @AutoService(Processor.class) 39 public final class AndroidEntryPointProcessor extends BaseProcessor { 40 41 @Override getSupportedAnnotationTypes()42 public Set<String> getSupportedAnnotationTypes() { 43 return ImmutableSet.of( 44 AndroidClassNames.ANDROID_ENTRY_POINT.toString(), 45 AndroidClassNames.HILT_ANDROID_APP.toString()); 46 } 47 48 @Override delayErrors()49 public boolean delayErrors() { 50 return true; 51 } 52 53 @Override processEach(TypeElement annotation, Element element)54 public void processEach(TypeElement annotation, Element element) throws Exception { 55 AndroidEntryPointMetadata metadata = AndroidEntryPointMetadata.of(getProcessingEnv(), element); 56 new InjectorEntryPointGenerator(getProcessingEnv(), metadata).generate(); 57 switch (metadata.androidType()) { 58 case APPLICATION: 59 // The generated application references the generated component so they must be generated 60 // in the same build unit. Thus, we only generate the application here if we're using the 61 // aggregating root processor. If we're using the Hilt Gradle plugin's aggregating task, we 62 // need to generate the application within ComponentTreeDepsProcessor instead. 63 if (useAggregatingRootProcessor(getProcessingEnv())) { 64 // While we could always generate the application in ComponentTreeDepsProcessor, even if 65 // we're using the aggregating root processor, it can lead to extraneous errors when 66 // things fail before ComponentTreeDepsProcessor runs so we generate it here to avoid that 67 new ApplicationGenerator(getProcessingEnv(), metadata).generate(); 68 } else { 69 // If we're not using the aggregating root processor, then make sure the root application 70 // does not extend the generated application directly, and instead uses bytecode injection 71 ProcessorErrors.checkState( 72 metadata.requiresBytecodeInjection(), 73 metadata.element(), 74 "'enableAggregatingTask=true' cannot be used when the application directly " 75 + "references the generated Hilt class, %s. Either extend %s directly (relying " 76 + "on the Gradle plugin described in " 77 + "https://dagger.dev/hilt/gradle-setup#why-use-the-plugin or set " 78 + "'enableAggregatingTask=false'.", 79 metadata.generatedClassName(), 80 metadata.baseClassName()); 81 } 82 break; 83 case ACTIVITY: 84 new ActivityGenerator(getProcessingEnv(), metadata).generate(); 85 break; 86 case BROADCAST_RECEIVER: 87 new BroadcastReceiverGenerator(getProcessingEnv(), metadata).generate(); 88 break; 89 case FRAGMENT: 90 new FragmentGenerator( 91 getProcessingEnv(), metadata ) 92 .generate(); 93 break; 94 case SERVICE: 95 new ServiceGenerator(getProcessingEnv(), metadata).generate(); 96 break; 97 case VIEW: 98 new ViewGenerator(getProcessingEnv(), metadata).generate(); 99 break; 100 default: 101 throw new IllegalStateException("Unknown Hilt type: " + metadata.androidType()); 102 } 103 } 104 } 105