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