• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.internal.codegen;
18 
19 import com.squareup.javapoet.AnnotationSpec;
20 import dagger.internal.GenerationOptions;
21 import javax.lang.model.element.TypeElement;
22 import javax.tools.Diagnostic;
23 
24 /** A collection of options that dictate how the compiler will run. */
25 abstract class CompilerOptions {
usesProducers()26   abstract boolean usesProducers();
27 
28   /**
29    * Returns true if the fast initialization flag, {@code fastInit}, is enabled.
30    *
31    * <p>If enabled, the generated code will attempt to optimize for fast component initialization.
32    * This is done by reducing the number of factory classes loaded during initialization and the
33    * number of eagerly initialized fields at the cost of potential memory leaks and higher
34    * per-provision instantiation time.
35    */
fastInit()36   abstract boolean fastInit();
37 
formatGeneratedSource()38   abstract boolean formatGeneratedSource();
39 
writeProducerNameInToken()40   abstract boolean writeProducerNameInToken();
41 
nullableValidationKind()42   abstract Diagnostic.Kind nullableValidationKind();
43 
doCheckForNulls()44   final boolean doCheckForNulls() {
45     return nullableValidationKind().equals(Diagnostic.Kind.ERROR);
46   }
47 
privateMemberValidationKind()48   abstract Diagnostic.Kind privateMemberValidationKind();
49 
staticMemberValidationKind()50   abstract Diagnostic.Kind staticMemberValidationKind();
51 
52   /**
53    * If {@code true}, Dagger will generate factories and components even if some members-injected
54    * types have {@code private} or {@code static} {@code @Inject}-annotated members.
55    *
56    * <p>This should only ever be enabled by the TCK tests. Disabling this validation could lead to
57    * generating code that does not compile.
58    */
ignorePrivateAndStaticInjectionForComponent()59   abstract boolean ignorePrivateAndStaticInjectionForComponent();
60 
scopeCycleValidationType()61   abstract ValidationType scopeCycleValidationType();
62 
warnIfInjectionFactoryNotGeneratedUpstream()63   abstract boolean warnIfInjectionFactoryNotGeneratedUpstream();
64 
headerCompilation()65   abstract boolean headerCompilation();
66 
aheadOfTimeSubcomponents()67   abstract boolean aheadOfTimeSubcomponents();
68 
69   /**
70    * Enables a testing configuration where all superclass {@link ComponentImplementation}s are
71    * derived from their serialized forms.
72    */
forceUseSerializedComponentImplementations()73   abstract boolean forceUseSerializedComponentImplementations();
74 
75   /**
76    * If {@code true}, in {@link #aheadOfTimeSubcomponents()} mode, Dagger will emit metadata
77    * annotations to deserialize aspects of the {@link ComponentImplementation}.
78    *
79    * This should only be disabled in compile-testing tests that want to ignore the annotations when
80    * asserting on generated source.
81    */
emitModifiableMetadataAnnotations()82   abstract boolean emitModifiableMetadataAnnotations();
83 
useGradleIncrementalProcessing()84   abstract boolean useGradleIncrementalProcessing();
85 
86   /**
87    * Returns the validation that should be done for the full binding graph for the element.
88    *
89    * @throws IllegalArgumentException if {@code element} is not a module or (sub)component
90    */
fullBindingGraphValidationType(TypeElement element)91   abstract ValidationType fullBindingGraphValidationType(TypeElement element);
92 
moduleHasDifferentScopesDiagnosticKind()93   abstract Diagnostic.Kind moduleHasDifferentScopesDiagnosticKind();
94 
explicitBindingConflictsWithInjectValidationType()95   abstract ValidationType explicitBindingConflictsWithInjectValidationType();
96 
97   /**
98    * Creates a new {@link CompilerOptions} from the serialized {@link GenerationOptions} of a base
99    * component implementation.
100    */
withGenerationOptions(GenerationOptions generationOptions)101   final CompilerOptions withGenerationOptions(GenerationOptions generationOptions) {
102     return new ForwardingCompilerOptions(this) {
103       @Override
104       public boolean fastInit() {
105         return generationOptions.fastInit();
106       }
107     };
108   }
109 
110   /**
111    * Returns a {@link GenerationOptions} annotation that serializes any options for this compilation
112    * that should be reused in future compilations.
113    */
114   final AnnotationSpec toGenerationOptionsAnnotation() {
115     return AnnotationSpec.builder(GenerationOptions.class)
116         .addMember("fastInit", "$L", fastInit())
117         .build();
118   }
119 }
120