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
getSupportedOptions()47   public Set<String> getSupportedOptions() {
48     return HiltCompilerOptions.getProcessorOptions();
49   }
50 
51   @Override
delayErrors()52   public boolean delayErrors() {
53     return true;
54   }
55 
56   @Override
processEach(TypeElement annotation, Element element)57   public void processEach(TypeElement annotation, Element element) throws Exception {
58     AndroidEntryPointMetadata metadata = AndroidEntryPointMetadata.of(getProcessingEnv(), element);
59     new InjectorEntryPointGenerator(getProcessingEnv(), metadata).generate();
60     switch (metadata.androidType()) {
61       case APPLICATION:
62         new ApplicationGenerator(getProcessingEnv(), metadata).generate();
63         break;
64       case ACTIVITY:
65         new ActivityGenerator(getProcessingEnv(), metadata).generate();
66         break;
67       case BROADCAST_RECEIVER:
68         new BroadcastReceiverGenerator(getProcessingEnv(), metadata).generate();
69         break;
70       case FRAGMENT:
71         new FragmentGenerator(
72             getProcessingEnv(), metadata )
73             .generate();
74         break;
75       case SERVICE:
76         new ServiceGenerator(getProcessingEnv(), metadata).generate();
77         break;
78       case VIEW:
79         new ViewGenerator(getProcessingEnv(), metadata).generate();
80         break;
81       default:
82         throw new IllegalStateException("Unknown Hilt type: " + metadata.androidType());
83     }
84   }
85 }
86