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