• 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.google.auto.common.MoreElements;
20 import com.google.auto.value.AutoValue;
21 import com.squareup.javapoet.ClassName;
22 import dagger.hilt.processor.internal.ClassNames;
23 import javax.annotation.processing.ProcessingEnvironment;
24 import javax.lang.model.element.Element;
25 import javax.lang.model.element.TypeElement;
26 
27 /** Metadata for a root element that can trigger the {@link RootProcessor}. */
28 @AutoValue
29 abstract class Root {
30   /**
31    * Creates the default root for this (test) build compilation.
32    *
33    * <p>A default root installs only the global {@code InstallIn} and {@code TestInstallIn}
34    * dependencies. Test-specific dependencies are not installed in the default root.
35    *
36    * <p>The default root is used for two purposes:
37    *
38    * <ul>
39    *   <li>To inject {@code EarlyEntryPoint} annotated interfaces.
40    *   <li>To inject tests that only depend on global dependencies
41    * </ul>
42    */
createDefaultRoot(ProcessingEnvironment env)43   static Root createDefaultRoot(ProcessingEnvironment env) {
44     TypeElement rootElement =
45         env.getElementUtils().getTypeElement(ClassNames.DEFAULT_ROOT.canonicalName());
46     return new AutoValue_Root(rootElement, /*isTestRoot=*/ true);
47   }
48 
49   /** Creates a {@plainlink Root root} for the given {@plainlink Element element}. */
create(Element element, ProcessingEnvironment env)50   static Root create(Element element, ProcessingEnvironment env) {
51     TypeElement rootElement = MoreElements.asType(element);
52     return new AutoValue_Root(rootElement, RootType.of(rootElement).isTestRoot());
53   }
54 
55   /** Returns the root element that should be used with processing. */
element()56   abstract TypeElement element();
57 
58   /** Returns {@code true} if this is a test root. */
isTestRoot()59   abstract boolean isTestRoot();
60 
61   /** Returns the class name of the root element. */
classname()62   ClassName classname() {
63     return ClassName.get(element());
64   }
65 
66   @Override
toString()67   public final String toString() {
68     return element().toString();
69   }
70 
71   /** Returns {@code true} if this uses the default root. */
isDefaultRoot()72   boolean isDefaultRoot() {
73     return classname().equals(ClassNames.DEFAULT_ROOT);
74   }
75 }
76