1 /* 2 * Copyright (C) 2015 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 static dagger.internal.codegen.componentgenerator.ComponentGenerator.componentName; 20 21 import dagger.internal.codegen.binding.BindingGraph; 22 import dagger.internal.codegen.binding.KeyFactory; 23 import dagger.internal.codegen.compileroption.CompilerOptions; 24 import dagger.internal.codegen.writing.ComponentImplementation; 25 import dagger.internal.codegen.writing.SubcomponentNames; 26 import java.util.Optional; 27 import javax.inject.Inject; 28 import javax.inject.Singleton; 29 30 /** Factory for {@link ComponentImplementation}s. */ 31 @Singleton 32 final class ComponentImplementationFactory { 33 private final KeyFactory keyFactory; 34 private final CompilerOptions compilerOptions; 35 private final TopLevelImplementationComponent.Builder topLevelImplementationComponentBuilder; 36 37 @Inject ComponentImplementationFactory( KeyFactory keyFactory, CompilerOptions compilerOptions, TopLevelImplementationComponent.Builder topLevelImplementationComponentBuilder)38 ComponentImplementationFactory( 39 KeyFactory keyFactory, 40 CompilerOptions compilerOptions, 41 TopLevelImplementationComponent.Builder topLevelImplementationComponentBuilder) { 42 this.keyFactory = keyFactory; 43 this.compilerOptions = compilerOptions; 44 this.topLevelImplementationComponentBuilder = topLevelImplementationComponentBuilder; 45 } 46 47 /** 48 * Returns a top-level (non-nested) component implementation for a binding graph. 49 */ createComponentImplementation(BindingGraph bindingGraph)50 ComponentImplementation createComponentImplementation(BindingGraph bindingGraph) { 51 ComponentImplementation componentImplementation = 52 ComponentImplementation.topLevelComponentImplementation( 53 bindingGraph, 54 componentName(bindingGraph.componentTypeElement()), 55 new SubcomponentNames(bindingGraph, keyFactory), 56 compilerOptions); 57 58 // TODO(dpb): explore using optional bindings for the "parent" bindings 59 return topLevelImplementationComponentBuilder 60 .topLevelComponent(componentImplementation) 61 .build() 62 .currentImplementationSubcomponentBuilder() 63 .componentImplementation(componentImplementation) 64 .bindingGraph(bindingGraph) 65 .parentBuilder(Optional.empty()) 66 .parentBindingExpressions(Optional.empty()) 67 .parentRequirementExpressions(Optional.empty()) 68 .build() 69 .componentImplementationBuilder() 70 .build(); 71 } 72 } 73