• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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;
18 
19 import com.google.common.collect.ImmutableSet;
20 import java.util.Arrays;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import javax.annotation.processing.ProcessingEnvironment;
24 import javax.lang.model.element.TypeElement;
25 
26 /** Hilt annotation processor options. */
27 // TODO(danysantiago): Consider consolidating with Dagger compiler options logic.
28 public final class HiltCompilerOptions {
29 
30   /**
31    * Returns {@code true} if the superclass validation is disabled for
32    * {@link dagger.hilt.android.AndroidEntryPoint}-annotated classes.
33    *
34    * This flag is for internal use only! The superclass validation checks that the super class is a
35    * generated {@code Hilt_} class. This flag is disabled by the Hilt Gradle plugin to enable
36    * bytecode transformation to change the superclass.
37    */
isAndroidSuperclassValidationDisabled( TypeElement element, ProcessingEnvironment env)38   public static boolean isAndroidSuperclassValidationDisabled(
39       TypeElement element, ProcessingEnvironment env) {
40     BooleanOption option = BooleanOption.DISABLE_ANDROID_SUPERCLASS_VALIDATION;
41     return option.get(env);
42   }
43 
44   /**
45    * Returns {@code true} if cross-compilation root validation is disabled.
46    *
47    * <p>This flag should rarely be needed, but may be used for legacy/migration purposes if
48    * tests require the use of {@link dagger.hilt.android.HiltAndroidApp} rather than
49    * {@link dagger.hilt.android.testing.HiltAndroidTest}.
50    *
51    * <p>Note that Hilt still does validation within a single compilation unit. In particular,
52    * a compilation unit that contains a {@code HiltAndroidApp} usage cannot have other
53    * {@code HiltAndroidApp} or {@code HiltAndroidTest} usages in the same compilation unit.
54    */
isCrossCompilationRootValidationDisabled( ImmutableSet<TypeElement> rootElements, ProcessingEnvironment env)55   public static boolean isCrossCompilationRootValidationDisabled(
56       ImmutableSet<TypeElement> rootElements, ProcessingEnvironment env) {
57     BooleanOption option = BooleanOption.DISABLE_CROSS_COMPILATION_ROOT_VALIDATION;
58     return option.get(env);
59   }
60 
61   /** Returns {@code true} if the check for {@link dagger.hilt.InstallIn} is disabled. */
isModuleInstallInCheckDisabled(ProcessingEnvironment env)62   public static boolean isModuleInstallInCheckDisabled(ProcessingEnvironment env) {
63     return BooleanOption.DISABLE_MODULES_HAVE_INSTALL_IN_CHECK.get(env);
64   }
65 
66   /**
67    * Returns {@code true} of unit tests should try to share generated components, rather than using
68    * separate generated components per Hilt test root.
69    *
70    * <p>Tests that provide their own test bindings (e.g. using {@link
71    * dagger.hilt.android.testing.BindValue} or a test {@link dagger.Module}) cannot use the shared
72    * component. In these cases, a component will be generated for the test.
73    */
isSharedTestComponentsEnabled(ProcessingEnvironment env)74   public static boolean isSharedTestComponentsEnabled(ProcessingEnvironment env) {
75     return BooleanOption.SHARE_TEST_COMPONENTS.get(env);
76   }
77 
78   /** Processor options which can have true or false values. */
79   private enum BooleanOption {
80     DISABLE_ANDROID_SUPERCLASS_VALIDATION(
81         "android.internal.disableAndroidSuperclassValidation", false),
82 
83     DISABLE_CROSS_COMPILATION_ROOT_VALIDATION("disableCrossCompilationRootValidation", false),
84 
85     DISABLE_MODULES_HAVE_INSTALL_IN_CHECK("disableModulesHaveInstallInCheck", false),
86 
87     SHARE_TEST_COMPONENTS("shareTestComponents", false);
88 
89     private final String name;
90     private final boolean defaultValue;
91 
BooleanOption(String name, boolean defaultValue)92     BooleanOption(String name, boolean defaultValue) {
93       this.name = name;
94       this.defaultValue = defaultValue;
95     }
96 
get(ProcessingEnvironment env)97     boolean get(ProcessingEnvironment env) {
98       String value = env.getOptions().get(getQualifiedName());
99       if (value == null) {
100         return defaultValue;
101       }
102       // TODO(danysantiago): Strictly verify input, either 'true' or 'false' and nothing else.
103       return Boolean.parseBoolean(value);
104     }
105 
getQualifiedName()106     String getQualifiedName() {
107       return "dagger.hilt." + name;
108     }
109   }
110 
getProcessorOptions()111   public static Set<String> getProcessorOptions() {
112     return Arrays.stream(BooleanOption.values())
113         .map(BooleanOption::getQualifiedName)
114         .collect(Collectors.toSet());
115   }
116 }
117