blob: 91839d6debff7b1af3629b205bedd2a31569c34b [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
ronshapiro5e96ced2018-12-27 12:16:26 -080072 static void checkIsModule(TypeElement moduleElement) {
73 checkArgument(forAnnotatedElement(moduleElement).isPresent());
74 }
75
cgdecker5177a062018-12-03 14:39:10 -080076 private final Class<? extends Annotation> moduleAnnotation;
77 private final Class<? extends Annotation> methodAnnotation;
78
79 ModuleKind(
80 Class<? extends Annotation> moduleAnnotation, Class<? extends Annotation> methodAnnotation) {
81 this.moduleAnnotation = moduleAnnotation;
82 this.methodAnnotation = methodAnnotation;
83 }
84
85 /**
86 * Returns the annotation mirror for this module kind on the given type.
87 *
88 * @throws IllegalArgumentException if the annotation is not present on the type
89 */
90 AnnotationMirror getModuleAnnotation(TypeElement element) {
91 Optional<AnnotationMirror> result = getAnnotationMirror(element, moduleAnnotation);
92 checkArgument(
93 result.isPresent(), "annotation %s is not present on type %s", moduleAnnotation, element);
94 return result.get();
95 }
96
97 /** Returns the annotation that marks a module of this kind. */
98 Class<? extends Annotation> annotation() {
99 return moduleAnnotation;
100 }
101
102 /** Returns the annotation for binding methods on this type of module. */
103 // TODO(cgdecker): Validate how this is used... is it really correct? Producer modules can also
104 // have @Provides methods.
105 Class<? extends Annotation> methodAnnotation() {
106 return methodAnnotation;
107 }
108
109 /** Returns the kinds of modules that a module of this kind is allowed to include. */
110 ImmutableSet<ModuleKind> legalIncludedModuleKinds() {
111 switch (this) {
112 case MODULE:
113 return Sets.immutableEnumSet(MODULE);
114 case PRODUCER_MODULE:
115 return Sets.immutableEnumSet(MODULE, PRODUCER_MODULE);
116 }
117 throw new AssertionError(this);
118 }
119}