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.binding; 18 19 import static com.google.auto.common.AnnotationMirrors.getAnnotationElementAndValue; 20 import static dagger.internal.codegen.binding.ConfigurationAnnotations.getSubcomponentCreator; 21 22 import com.google.auto.value.AutoValue; 23 import com.google.auto.value.extension.memoized.Memoized; 24 import com.google.common.collect.ImmutableSet; 25 import dagger.internal.codegen.base.ModuleAnnotation; 26 import dagger.model.Key; 27 import java.util.Optional; 28 import javax.inject.Inject; 29 import javax.lang.model.element.Element; 30 import javax.lang.model.element.TypeElement; 31 32 /** 33 * A declaration for a subcomponent that is included in a module via {@link 34 * dagger.Module#subcomponents()}. 35 */ 36 @AutoValue 37 public abstract class SubcomponentDeclaration extends BindingDeclaration { 38 /** 39 * Key for the {@link dagger.Subcomponent.Builder} or {@link 40 * dagger.producers.ProductionSubcomponent.Builder} of {@link #subcomponentType()}. 41 */ 42 @Override key()43 public abstract Key key(); 44 45 /** 46 * The type element that defines the {@link dagger.Subcomponent} or {@link 47 * dagger.producers.ProductionSubcomponent} for this declaration. 48 */ subcomponentType()49 abstract TypeElement subcomponentType(); 50 51 /** The module annotation. */ moduleAnnotation()52 public abstract ModuleAnnotation moduleAnnotation(); 53 54 @Memoized 55 @Override hashCode()56 public abstract int hashCode(); 57 58 @Override equals(Object obj)59 public abstract boolean equals(Object obj); 60 61 /** A {@link SubcomponentDeclaration} factory. */ 62 public static class Factory { 63 private final KeyFactory keyFactory; 64 65 @Inject Factory(KeyFactory keyFactory)66 Factory(KeyFactory keyFactory) { 67 this.keyFactory = keyFactory; 68 } 69 forModule(TypeElement module)70 ImmutableSet<SubcomponentDeclaration> forModule(TypeElement module) { 71 ImmutableSet.Builder<SubcomponentDeclaration> declarations = ImmutableSet.builder(); 72 ModuleAnnotation moduleAnnotation = ModuleAnnotation.moduleAnnotation(module).get(); 73 Element subcomponentAttribute = 74 getAnnotationElementAndValue(moduleAnnotation.annotation(), "subcomponents").getKey(); 75 for (TypeElement subcomponent : moduleAnnotation.subcomponents()) { 76 declarations.add( 77 new AutoValue_SubcomponentDeclaration( 78 Optional.of(subcomponentAttribute), 79 Optional.of(module), 80 keyFactory.forSubcomponentCreator( 81 getSubcomponentCreator(subcomponent).get().asType()), 82 subcomponent, 83 moduleAnnotation)); 84 } 85 return declarations.build(); 86 } 87 } 88 } 89