• 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.aggregateddeps;
18 
19 import static androidx.room.compiler.processing.compat.XConverters.getProcessingEnv;
20 
21 import androidx.room.compiler.processing.XAnnotation;
22 import androidx.room.compiler.processing.XTypeElement;
23 import com.google.auto.value.AutoValue;
24 import com.squareup.javapoet.ClassName;
25 import com.squareup.javapoet.TypeName;
26 import dagger.hilt.processor.internal.ClassNames;
27 import dagger.hilt.processor.internal.Processors;
28 import dagger.internal.codegen.xprocessing.XTypeElements;
29 import java.util.Optional;
30 
31 /** PkgPrivateModuleMetadata contains a set of utilities for processing package private modules. */
32 @AutoValue
33 public abstract class PkgPrivateMetadata {
34   /** Returns the public Hilt wrapped type or the type itself if it is already public. */
publicModule(XTypeElement element)35   public static XTypeElement publicModule(XTypeElement element) {
36     return publicDep(element, ClassNames.MODULE);
37   }
38 
39   /** Returns the public Hilt wrapped type or the type itself if it is already public. */
publicEntryPoint(XTypeElement element)40   public static XTypeElement publicEntryPoint(XTypeElement element) {
41     return publicDep(element, ClassNames.ENTRY_POINT);
42   }
43 
44   /** Returns the public Hilt wrapped type or the type itself if it is already public. */
publicEarlyEntryPoint(XTypeElement element)45   public static XTypeElement publicEarlyEntryPoint(XTypeElement element) {
46     return publicDep(element, ClassNames.EARLY_ENTRY_POINT);
47   }
48 
publicDep(XTypeElement element, ClassName annotation)49   private static XTypeElement publicDep(XTypeElement element, ClassName annotation) {
50     return of(element, annotation)
51         .map(PkgPrivateMetadata::generatedClassName)
52         .map(ClassName::canonicalName)
53         .map(getProcessingEnv(element)::requireTypeElement)
54         .orElse(element);
55   }
56 
57   private static final String PREFIX = "HiltWrapper_";
58 
59   /** Returns the base class name of the elemenet. */
baseClassName()60   TypeName baseClassName() {
61     return getTypeElement().getClassName();
62   }
63 
64   /** Returns TypeElement for the module element the metadata object represents */
getTypeElement()65   abstract XTypeElement getTypeElement();
66 
67   /**
68    * Returns an optional @InstallIn AnnotationMirror for the module element the metadata object
69    * represents
70    */
getOptionalInstallInAnnotation()71   abstract Optional<XAnnotation> getOptionalInstallInAnnotation();
72 
73   /** Return the Type of this package private element. */
getAnnotation()74   abstract ClassName getAnnotation();
75 
76   /** Returns the expected generated classname for the element the metadata object represents */
generatedClassName()77   final ClassName generatedClassName() {
78     return Processors.prepend(
79         Processors.getEnclosedClassName(getTypeElement().getClassName()), PREFIX);
80   }
81 
82   /**
83    * Returns an Optional PkgPrivateMetadata requiring Hilt processing, otherwise returns an empty
84    * Optional.
85    */
of(XTypeElement element, ClassName annotation)86   static Optional<PkgPrivateMetadata> of(XTypeElement element, ClassName annotation) {
87     // If this is a public element no wrapping is needed
88     if (XTypeElements.isEffectivelyPublic(element) && !element.isInternal()) {
89       return Optional.empty();
90     }
91 
92     Optional<XAnnotation> installIn;
93     if (element.hasAnnotation(ClassNames.INSTALL_IN)) {
94       installIn = Optional.of(element.getAnnotation(ClassNames.INSTALL_IN));
95     } else if (element.hasAnnotation(ClassNames.TEST_INSTALL_IN)) {
96       installIn = Optional.of(element.getAnnotation(ClassNames.TEST_INSTALL_IN));
97     } else {
98       throw new IllegalStateException(
99           "Expected element to be annotated with @InstallIn: " + element);
100     }
101 
102     if (annotation.equals(ClassNames.MODULE)
103         ) {
104       // Skip modules that require a module instance. Otherwise Dagger validation will (correctly)
105       // fail on the wrapper saying a public module can't include a private one, which makes the
106       // error more confusing for users since they probably aren't aware of the wrapper. When
107       // skipped, if the root is in a different package, the error will instead just be on the
108       // generated Hilt component.
109       if (Processors.requiresModuleInstance(element)) {
110         return Optional.empty();
111       }
112     }
113     return Optional.of(new AutoValue_PkgPrivateMetadata(element, installIn, annotation));
114   }
115 }
116