• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 dagger.Module;
20 import dagger.Provides;
21 import dagger.internal.GenerationOptions;
22 import dagger.internal.codegen.langmodel.DaggerElements;
23 import java.util.Optional;
24 
25 /** Adds bindings for serializing and rereading {@link GenerationOptions}. */
26 @Module
27 interface GenerationOptionsModule {
28   @Provides
29   @PerComponentImplementation
30   @GenerationCompilerOptions
generationOptions( CompilerOptions defaultOptions, ComponentImplementation componentImplementation, DaggerElements elements)31   static CompilerOptions generationOptions(
32       CompilerOptions defaultOptions,
33       ComponentImplementation componentImplementation,
34       DaggerElements elements) {
35     // Avoid looking up types that don't exist. Performance improves for large components.
36     if (!defaultOptions.aheadOfTimeSubcomponents()) {
37       return defaultOptions;
38     }
39     // Inspect the base implementation for the @GenerationOptions annotation. Even if
40     // componentImplementation is the base implementation, inspect it for the case where we are
41     // recomputing the ComponentImplementation from a previous compilation.
42     // TODO(b/117833324): consider adding a method that returns baseImplementation.orElse(this).
43     // The current state of the world is a little confusing and maybe not intuitive: the base
44     // implementation has no base implementation, but it _is_ a base implementation.
45     return Optional.of(componentImplementation.baseImplementation().orElse(componentImplementation))
46         .map(baseImplementation -> elements.getTypeElement(baseImplementation.name()))
47         // If this returns null, the type has not been generated yet and Optional will switch to an
48         // empty state. This means that we're currently generating componentImplementation, or that
49         // the base implementation is being generated in this round, and thus the options passed to
50         // this compilation are applicable
51         .map(typeElement -> typeElement.getAnnotation(GenerationOptions.class))
52         .map(defaultOptions::withGenerationOptions)
53         .orElse(defaultOptions);
54   }
55 }
56