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; 18 19 import static com.google.auto.common.MoreElements.isAnnotationPresent; 20 import static com.google.common.collect.Sets.immutableEnumSet; 21 import static dagger.internal.codegen.DaggerStreams.stream; 22 import static dagger.internal.codegen.DaggerStreams.toImmutableSet; 23 import static dagger.internal.codegen.DaggerStreams.valuesOf; 24 import static java.util.EnumSet.allOf; 25 26 import com.google.common.collect.ImmutableSet; 27 import dagger.Component; 28 import dagger.Module; 29 import dagger.Subcomponent; 30 import dagger.producers.ProducerModule; 31 import dagger.producers.ProductionComponent; 32 import dagger.producers.ProductionSubcomponent; 33 import java.lang.annotation.Annotation; 34 import java.util.Optional; 35 import javax.lang.model.element.TypeElement; 36 37 /** Enumeration of the different kinds of components. */ 38 enum ComponentKind { 39 /** {@code @Component} */ 40 COMPONENT(Component.class, true, false), 41 42 /** {@code @Subcomponent} */ 43 SUBCOMPONENT(Subcomponent.class, false, false), 44 45 /** {@code @ProductionComponent} */ 46 PRODUCTION_COMPONENT(ProductionComponent.class, true, true), 47 48 /** {@code @ProductionSubcomponent} */ 49 PRODUCTION_SUBCOMPONENT(ProductionSubcomponent.class, false, true), 50 51 /** 52 * Kind for a descriptor that was generated from a {@link Module} instead of a component type in 53 * order to validate the module's bindings. 54 */ 55 MODULE(Module.class, true, false), 56 57 /** 58 * Kind for a descriptor was generated from a {@link ProducerModule} instead of a component type 59 * in order to validate the module's bindings. 60 */ 61 PRODUCER_MODULE(ProducerModule.class, true, true), 62 ; 63 64 private static final ImmutableSet<ComponentKind> ROOT_COMPONENT_KINDS = 65 valuesOf(ComponentKind.class) 66 .filter(kind -> !kind.isForModuleValidation()) 67 .filter(kind -> kind.isRoot()) 68 .collect(toImmutableSet()); 69 70 private static final ImmutableSet<ComponentKind> SUBCOMPONENT_KINDS = 71 valuesOf(ComponentKind.class) 72 .filter(kind -> !kind.isForModuleValidation()) 73 .filter(kind -> !kind.isRoot()) 74 .collect(toImmutableSet()); 75 76 /** Returns the set of kinds for root components. */ rootComponentKinds()77 static ImmutableSet<ComponentKind> rootComponentKinds() { 78 return ROOT_COMPONENT_KINDS; 79 } 80 81 /** Returns the set of kinds for subcomponents. */ subcomponentKinds()82 static ImmutableSet<ComponentKind> subcomponentKinds() { 83 return SUBCOMPONENT_KINDS; 84 } 85 86 /** Returns the annotations for components of the given kinds. */ annotationsFor(Iterable<ComponentKind> kinds)87 static ImmutableSet<Class<? extends Annotation>> annotationsFor(Iterable<ComponentKind> kinds) { 88 return stream(kinds).map(ComponentKind::annotation).collect(toImmutableSet()); 89 } 90 91 /** Returns the set of component kinds the given {@code element} has annotations for. */ getComponentKinds(TypeElement element)92 static ImmutableSet<ComponentKind> getComponentKinds(TypeElement element) { 93 return valuesOf(ComponentKind.class) 94 .filter(kind -> isAnnotationPresent(element, kind.annotation())) 95 .collect(toImmutableSet()); 96 } 97 98 /** 99 * Returns the kind of an annotated element if it is annotated with one of the {@linkplain 100 * #annotation() annotations}. 101 * 102 * @throws IllegalArgumentException if the element is annotated with more than one of the 103 * annotations 104 */ forAnnotatedElement(TypeElement element)105 static Optional<ComponentKind> forAnnotatedElement(TypeElement element) { 106 ImmutableSet<ComponentKind> kinds = getComponentKinds(element); 107 if (kinds.size() > 1) { 108 throw new IllegalArgumentException( 109 element + " cannot be annotated with more than one of " + annotationsFor(kinds)); 110 } 111 return kinds.stream().findAny(); 112 } 113 114 private final Class<? extends Annotation> annotation; 115 private final boolean isRoot; 116 private final boolean production; 117 ComponentKind( Class<? extends Annotation> annotation, boolean isRoot, boolean production)118 ComponentKind( 119 Class<? extends Annotation> annotation, 120 boolean isRoot, 121 boolean production) { 122 this.annotation = annotation; 123 this.isRoot = isRoot; 124 this.production = production; 125 } 126 127 /** Returns the annotation that marks a component of this kind. */ annotation()128 Class<? extends Annotation> annotation() { 129 return annotation; 130 } 131 132 /** Returns the kinds of modules that can be used with a component of this kind. */ legalModuleKinds()133 ImmutableSet<ModuleKind> legalModuleKinds() { 134 return isProducer() 135 ? immutableEnumSet(allOf(ModuleKind.class)) 136 : immutableEnumSet(ModuleKind.MODULE); 137 } 138 139 /** Returns the kinds of subcomponents a component of this kind can have. */ legalSubcomponentKinds()140 ImmutableSet<ComponentKind> legalSubcomponentKinds() { 141 return isProducer() 142 ? immutableEnumSet(PRODUCTION_SUBCOMPONENT) 143 : immutableEnumSet(SUBCOMPONENT, PRODUCTION_SUBCOMPONENT); 144 } 145 146 /** 147 * Returns {@code true} if the descriptor is for a root component (not a subcomponent) or is for 148 * {@linkplain #isForModuleValidation() module-validation}. 149 */ isRoot()150 boolean isRoot() { 151 return isRoot; 152 } 153 154 /** Returns true if this is a production component. */ isProducer()155 boolean isProducer() { 156 return production; 157 } 158 159 /** Returns {@code true} if the descriptor is for a module in order to validate its bindings. */ isForModuleValidation()160 boolean isForModuleValidation() { 161 switch (this) { 162 case MODULE: 163 case PRODUCER_MODULE: 164 return true; 165 default: 166 // fall through 167 } 168 return false; 169 } 170 } 171