1 /* 2 * Copyright (C) 2014 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.XElement; 20 import androidx.room.compiler.processing.XFiler; 21 import androidx.room.compiler.processing.XProcessingEnv; 22 import com.google.common.collect.ImmutableList; 23 import com.squareup.javapoet.TypeSpec; 24 import dagger.Component; 25 import dagger.internal.codegen.base.SourceFileGenerator; 26 import dagger.internal.codegen.binding.BindingGraph; 27 import dagger.internal.codegen.writing.ComponentImplementation; 28 import java.util.Optional; 29 import javax.inject.Inject; 30 31 /** Generates the implementation of the abstract types annotated with {@link Component}. */ 32 final class ComponentGenerator extends SourceFileGenerator<BindingGraph> { 33 private final TopLevelImplementationComponent.Factory topLevelImplementationComponentFactory; 34 35 @Inject ComponentGenerator( XFiler filer, XProcessingEnv processingEnv, TopLevelImplementationComponent.Factory topLevelImplementationComponentFactory)36 ComponentGenerator( 37 XFiler filer, 38 XProcessingEnv processingEnv, 39 TopLevelImplementationComponent.Factory topLevelImplementationComponentFactory) { 40 super(filer, processingEnv); 41 this.topLevelImplementationComponentFactory = topLevelImplementationComponentFactory; 42 } 43 44 @Override originatingElement(BindingGraph input)45 public XElement originatingElement(BindingGraph input) { 46 return input.componentTypeElement(); 47 } 48 49 @Override topLevelTypes(BindingGraph bindingGraph)50 public ImmutableList<TypeSpec.Builder> topLevelTypes(BindingGraph bindingGraph) { 51 ComponentImplementation componentImplementation = 52 topLevelImplementationComponentFactory 53 .create(bindingGraph) 54 .currentImplementationSubcomponentBuilder() 55 .bindingGraph(bindingGraph) 56 .parentImplementation(Optional.empty()) 57 .parentRequestRepresentations(Optional.empty()) 58 .parentRequirementExpressions(Optional.empty()) 59 .build() 60 .componentImplementation(); 61 return ImmutableList.of(componentImplementation.generate().toBuilder()); 62 } 63 } 64