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