• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.root;
18 
19 import com.squareup.javapoet.ClassName;
20 import dagger.hilt.processor.internal.ClassNames;
21 import dagger.hilt.processor.internal.Processors;
22 import javax.annotation.processing.ProcessingEnvironment;
23 import javax.lang.model.element.TypeElement;
24 
25 /** The valid root types for Hilt applications. */
26 // TODO(erichang): Fix this class so we don't have to have placeholders
27   enum RootType {
28     ROOT(ClassNames.HILT_ANDROID_APP),
29 
30     // Placeholder to make sure @HiltAndroidTest usages get processed
31     HILT_ANDROID_TEST_ROOT(ClassNames.HILT_ANDROID_TEST),
32 
33     TEST_ROOT(ClassNames.INTERNAL_TEST_ROOT);
34 
35   @SuppressWarnings("ImmutableEnumChecker")
36   private final ClassName annotation;
37 
RootType(ClassName annotation)38   RootType(ClassName annotation) {
39     this.annotation = annotation;
40   }
41 
isTestRoot()42   public boolean isTestRoot() {
43     return this == TEST_ROOT;
44   }
45 
className()46   public ClassName className() {
47     return annotation;
48   }
49 
of(ProcessingEnvironment env, TypeElement element)50   public static RootType of(ProcessingEnvironment env, TypeElement element) {
51     if (Processors.hasAnnotation(element, ClassNames.HILT_ANDROID_APP)) {
52       return ROOT;
53     } else if (Processors.hasAnnotation(element, ClassNames.HILT_ANDROID_TEST)) {
54       return TEST_ROOT;
55     } else if (Processors.hasAnnotation(element, ClassNames.INTERNAL_TEST_ROOT)) {
56       return TEST_ROOT;
57     }
58     throw new IllegalStateException("Unknown root type: " + element);
59   }
60 }
61