• 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.internal.codegen.componentgenerator;
18 
19 import androidx.room.compiler.processing.XProcessingEnv;
20 import dagger.Binds;
21 import dagger.Module;
22 import dagger.Provides;
23 import dagger.internal.codegen.base.SourceFileGenerator;
24 import dagger.internal.codegen.base.SourceFileHjarGenerator;
25 import dagger.internal.codegen.binding.BindingGraph;
26 import dagger.internal.codegen.binding.ComponentDescriptor;
27 
28 /** Provides bindings needed to generated the component. */
29 @Module(subcomponents = TopLevelImplementationComponent.class)
30 public interface ComponentGeneratorModule {
31 
32   @Binds
componentGenerator(ComponentGenerator generator)33   abstract SourceFileGenerator<BindingGraph> componentGenerator(ComponentGenerator generator);
34 
35   // The SourceFileHjarGenerator wrapper first generates the entire TypeSpec before stripping out
36   // things that aren't needed for the hjar. However, this can be really expensive for the component
37   // because it is usually the most expensive file to generate, and most of its content is not
38   // needed in the hjar. Thus, we provide a completely separate processing step,
39   // ComponentHjarProcessingStep and ComponentHjarGenerator, for when generating hjars for
40   // components, which can avoid generating the parts of the component that would have been stripped
41   // out by the HjarSourceFileGenerator anyway. Note that we still wrap ComponentHjarGenerator in
42   // SourceFileHjarGenerator because it adds in constructor and method bodies that are needed for
43   // Javac to compile correctly, e.g. super(...) calls in the constructor and return statements in
44   // methods.
45   @Provides
componentHjarGenerator( XProcessingEnv processingEnv, ComponentHjarGenerator hjarGenerator)46   static SourceFileGenerator<ComponentDescriptor> componentHjarGenerator(
47       XProcessingEnv processingEnv,
48       ComponentHjarGenerator hjarGenerator) {
49     // Note: technically the ComponentHjarGenerator is already in hjar form, but the
50     // SourceFileHjarGenerator wrapper adds in proper method bodies, e.g. constructors that require
51     // super() calls or methods that require return statements.
52     return SourceFileHjarGenerator.wrap(hjarGenerator, processingEnv);
53   }
54 }
55