blob: 7087f882ad0aa1ba0087fb6202d04bd0f624f7fe [file] [log] [blame]
cgdecker5177a062018-12-03 14:39:10 -08001/*
2 * Copyright (C) 2015 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
17package dagger.internal.codegen;
18
19import static com.google.common.base.Preconditions.checkArgument;
20import static dagger.internal.codegen.DaggerElements.getAnnotationMirror;
21import static dagger.internal.codegen.DaggerStreams.toImmutableSet;
22
23import com.google.auto.common.MoreElements;
24import com.google.common.collect.ImmutableSet;
25import com.google.common.collect.Sets;
26import dagger.Module;
27import dagger.Provides;
28import dagger.producers.ProducerModule;
29import dagger.producers.Produces;
30import java.lang.annotation.Annotation;
31import java.util.EnumSet;
32import java.util.Optional;
33import java.util.Set;
34import javax.lang.model.element.AnnotationMirror;
35import javax.lang.model.element.TypeElement;
36
37/** Enumeration of the kinds of modules. */
38enum ModuleKind {
39 /** {@code @Module} */
40 MODULE(Module.class, Provides.class),
41
42 /** {@code @ProducerModule} */
43 PRODUCER_MODULE(ProducerModule.class, Produces.class);
44
45 /** Returns the annotations for modules of the given kinds. */
46 static ImmutableSet<Class<? extends Annotation>> annotationsFor(Set<ModuleKind> kinds) {
47 return kinds.stream().map(ModuleKind::annotation).collect(toImmutableSet());
48 }
49
50 /**
51 * Returns the kind of an annotated element if it is annotated with one of the module {@linkplain
52 * #annotation() annotations}.
53 *
54 * @throws IllegalArgumentException if the element is annotated with more than one of the module
55 * annotations
56 */
57 static Optional<ModuleKind> forAnnotatedElement(TypeElement element) {
58 Set<ModuleKind> kinds = EnumSet.noneOf(ModuleKind.class);
59 for (ModuleKind kind : values()) {
60 if (MoreElements.isAnnotationPresent(element, kind.annotation())) {
61 kinds.add(kind);
62 }
63 }
64
65 if (kinds.size() > 1) {
66 throw new IllegalArgumentException(
67 element + " cannot be annotated with more than one of " + annotationsFor(kinds));
68 }
69 return kinds.stream().findAny();
70 }
71
72 private final Class<? extends Annotation> moduleAnnotation;
73 private final Class<? extends Annotation> methodAnnotation;
74
75 ModuleKind(
76 Class<? extends Annotation> moduleAnnotation, Class<? extends Annotation> methodAnnotation) {
77 this.moduleAnnotation = moduleAnnotation;
78 this.methodAnnotation = methodAnnotation;
79 }
80
81 /**
82 * Returns the annotation mirror for this module kind on the given type.
83 *
84 * @throws IllegalArgumentException if the annotation is not present on the type
85 */
86 AnnotationMirror getModuleAnnotation(TypeElement element) {
87 Optional<AnnotationMirror> result = getAnnotationMirror(element, moduleAnnotation);
88 checkArgument(
89 result.isPresent(), "annotation %s is not present on type %s", moduleAnnotation, element);
90 return result.get();
91 }
92
93 /** Returns the annotation that marks a module of this kind. */
94 Class<? extends Annotation> annotation() {
95 return moduleAnnotation;
96 }
97
98 /** Returns the annotation for binding methods on this type of module. */
99 // TODO(cgdecker): Validate how this is used... is it really correct? Producer modules can also
100 // have @Provides methods.
101 Class<? extends Annotation> methodAnnotation() {
102 return methodAnnotation;
103 }
104
105 /** Returns the kinds of modules that a module of this kind is allowed to include. */
106 ImmutableSet<ModuleKind> legalIncludedModuleKinds() {
107 switch (this) {
108 case MODULE:
109 return Sets.immutableEnumSet(MODULE);
110 case PRODUCER_MODULE:
111 return Sets.immutableEnumSet(MODULE, PRODUCER_MODULE);
112 }
113 throw new AssertionError(this);
114 }
115}