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 net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING; 20 21 import com.google.auto.service.AutoService; 22 import com.google.common.collect.ImmutableSet; 23 import dagger.hilt.android.processor.internal.AndroidClassNames; 24 import dagger.hilt.processor.internal.BaseProcessor; 25 import java.util.Set; 26 import javax.annotation.processing.Processor; 27 import javax.lang.model.element.Element; 28 import javax.lang.model.element.TypeElement; 29 import net.ltgt.gradle.incap.IncrementalAnnotationProcessor; 30 31 /** 32 * Processor that creates a module for classes marked with {@link 33 * dagger.hilt.android.AndroidEntryPoint}. 34 */ 35 @IncrementalAnnotationProcessor(ISOLATING) 36 @AutoService(Processor.class) 37 public final class AndroidEntryPointProcessor extends BaseProcessor { 38 39 @Override getSupportedAnnotationTypes()40 public Set<String> getSupportedAnnotationTypes() { 41 return ImmutableSet.of( 42 AndroidClassNames.ANDROID_ENTRY_POINT.toString(), 43 AndroidClassNames.HILT_ANDROID_APP.toString()); 44 } 45 46 @Override delayErrors()47 public boolean delayErrors() { 48 return true; 49 } 50 51 @Override processEach(TypeElement annotation, Element element)52 public void processEach(TypeElement annotation, Element element) throws Exception { 53 AndroidEntryPointMetadata metadata = AndroidEntryPointMetadata.of(getProcessingEnv(), element); 54 new InjectorEntryPointGenerator(getProcessingEnv(), metadata).generate(); 55 switch (metadata.androidType()) { 56 case APPLICATION: 57 new ApplicationGenerator(getProcessingEnv(), metadata).generate(); 58 break; 59 case ACTIVITY: 60 new ActivityGenerator(getProcessingEnv(), metadata).generate(); 61 break; 62 case BROADCAST_RECEIVER: 63 new BroadcastReceiverGenerator(getProcessingEnv(), metadata).generate(); 64 break; 65 case FRAGMENT: 66 new FragmentGenerator( 67 getProcessingEnv(), metadata ) 68 .generate(); 69 break; 70 case SERVICE: 71 new ServiceGenerator(getProcessingEnv(), metadata).generate(); 72 break; 73 case VIEW: 74 new ViewGenerator(getProcessingEnv(), metadata).generate(); 75 break; 76 default: 77 throw new IllegalStateException("Unknown Hilt type: " + metadata.androidType()); 78 } 79 } 80 } 81