• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.base;
18 
19 import static com.google.common.collect.Sets.immutableEnumSet;
20 import static dagger.internal.codegen.extension.DaggerStreams.stream;
21 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
22 import static dagger.internal.codegen.extension.DaggerStreams.valuesOf;
23 import static java.util.EnumSet.allOf;
24 
25 import androidx.room.compiler.processing.XTypeElement;
26 import com.google.common.collect.ImmutableSet;
27 import com.squareup.javapoet.ClassName;
28 import dagger.internal.codegen.javapoet.TypeNames;
29 import java.util.Optional;
30 
31 /** Enumeration of the different kinds of components. */
32 public enum ComponentKind {
33   COMPONENT(TypeNames.COMPONENT),
34   SUBCOMPONENT(TypeNames.SUBCOMPONENT),
35   PRODUCTION_COMPONENT(TypeNames.PRODUCTION_COMPONENT),
36   PRODUCTION_SUBCOMPONENT(TypeNames.PRODUCTION_SUBCOMPONENT),
37   MODULE(TypeNames.MODULE),
38   PRODUCER_MODULE(TypeNames.PRODUCER_MODULE);
39 
40   private static final ImmutableSet<ComponentKind> PRODUCER_KINDS =
41       ImmutableSet.of(PRODUCTION_COMPONENT, PRODUCTION_SUBCOMPONENT, PRODUCER_MODULE);
42 
43   /** Returns the annotations for components of the given kinds. */
annotationsFor(Iterable<ComponentKind> kinds)44   public static ImmutableSet<ClassName> annotationsFor(Iterable<ComponentKind> kinds) {
45     return stream(kinds).map(ComponentKind::annotation).collect(toImmutableSet());
46   }
47 
48   /** Returns the set of component kinds the given {@code element} has annotations for. */
getComponentKinds(XTypeElement element)49   public static ImmutableSet<ComponentKind> getComponentKinds(XTypeElement element) {
50     return valuesOf(ComponentKind.class)
51         .filter(kind -> element.hasAnnotation(kind.annotation()))
52         .collect(toImmutableSet());
53   }
54 
55   /**
56    * Returns the kind of an annotated element if it is annotated with one of the {@linkplain
57    * #annotation() annotations}.
58    *
59    * @throws IllegalArgumentException if the element is annotated with more than one of the
60    *     annotations
61    */
forAnnotatedElement(XTypeElement element)62   public static Optional<ComponentKind> forAnnotatedElement(XTypeElement element) {
63     ImmutableSet<ComponentKind> kinds = getComponentKinds(element);
64     if (kinds.size() > 1) {
65       throw new IllegalArgumentException(
66           element + " cannot be annotated with more than one of " + annotationsFor(kinds));
67     }
68     return kinds.stream().findAny();
69   }
70 
71   private final ClassName annotation;
72 
ComponentKind(ClassName annotation)73   ComponentKind(ClassName annotation) {
74     this.annotation = annotation;
75   }
76 
77   /** Returns the annotation that marks a component of this kind. */
annotation()78   public ClassName annotation() {
79     return annotation;
80   }
81 
82   /** Returns the kinds of modules that can be used with a component of this kind. */
legalModuleKinds()83   public ImmutableSet<ModuleKind> legalModuleKinds() {
84     return isProducer()
85         ? immutableEnumSet(allOf(ModuleKind.class))
86         : immutableEnumSet(ModuleKind.MODULE);
87   }
88 
89   /** Returns the kinds of subcomponents a component of this kind can have. */
legalSubcomponentKinds()90   public ImmutableSet<ComponentKind> legalSubcomponentKinds() {
91     return isProducer()
92         ? immutableEnumSet(PRODUCTION_SUBCOMPONENT)
93         : immutableEnumSet(SUBCOMPONENT, PRODUCTION_SUBCOMPONENT);
94   }
95 
96   /** Returns true if this is a production component. */
isProducer()97   public boolean isProducer() {
98     return PRODUCER_KINDS.contains(this);
99   }
100 }
101