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