• 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.XElement;
20 import androidx.room.compiler.processing.XProcessingEnv;
21 import androidx.room.compiler.processing.XTypeElement;
22 import com.google.auto.value.AutoValue;
23 import com.squareup.javapoet.ClassName;
24 import dagger.hilt.processor.internal.ClassNames;
25 import dagger.internal.codegen.xprocessing.XElements;
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(XProcessingEnv env)43   static Root createDefaultRoot(XProcessingEnv env) {
44     XTypeElement rootElement = env.requireTypeElement(ClassNames.DEFAULT_ROOT.canonicalName());
45     return new AutoValue_Root(rootElement, rootElement, /*isTestRoot=*/ true);
46   }
47 
48   /** Creates a {@plainlink Root root} for the given {@plainlink Element element}. */
create(XElement element, XProcessingEnv env)49   static Root create(XElement element, XProcessingEnv env) {
50     XTypeElement rootElement = XElements.asTypeElement(element);
51     if (ClassNames.DEFAULT_ROOT.equals(rootElement.getClassName())) {
52       return createDefaultRoot(env);
53     }
54     return new AutoValue_Root(rootElement, rootElement, RootType.of(rootElement).isTestRoot());
55   }
56 
57   /** Returns the root element that should be used with processing. */
element()58   abstract XTypeElement element();
59 
60   /**
61    * Returns the originating root element. In most cases this will be the same as {@link
62    * #element()}.
63    */
originatingRootElement()64   abstract XTypeElement originatingRootElement();
65 
66   /** Returns {@code true} if this is a test root. */
isTestRoot()67   abstract boolean isTestRoot();
68 
69   /** Returns the class name of the root element. */
classname()70   ClassName classname() {
71     return element().getClassName();
72   }
73 
74   /** Returns the class name of the originating root element. */
originatingRootClassname()75   ClassName originatingRootClassname() {
76     return originatingRootElement().getClassName();
77   }
78 
79   @Override
toString()80   public final String toString() {
81     return originatingRootElement().toString();
82   }
83 
84   /** Returns {@code true} if this uses the default root. */
isDefaultRoot()85   boolean isDefaultRoot() {
86     return classname().equals(ClassNames.DEFAULT_ROOT);
87   }
88 }
89