blob: f646b5c9171aea063711f7348af440d7952a7e73 [file] [log] [blame]
ronshapiro665a4a52016-04-12 21:10:00 -07001/*
ronshapiro5dde42d2016-06-17 09:03:35 -07002 * Copyright (C) 2016 The Dagger Authors.
ronshapiro665a4a52016-04-12 21:10:00 -07003 *
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 */
dpb1b65b6a2016-07-11 12:11:24 -070016
ronshapiro665a4a52016-04-12 21:10:00 -070017package dagger.internal.codegen;
18
dpbffd98f62016-12-20 10:05:16 -080019import static dagger.internal.codegen.DaggerElements.getAnnotationMirror;
20import static dagger.internal.codegen.DaggerElements.isAnyAnnotationPresent;
gake55f0742016-07-12 15:36:42 -070021import static dagger.internal.codegen.ErrorMessages.MULTIBINDING_ANNOTATION_NOT_ON_BINDING_METHOD;
gake55f0742016-07-12 15:36:42 -070022
ronshapiro665a4a52016-04-12 21:10:00 -070023import com.google.auto.common.BasicAnnotationProcessor.ProcessingStep;
ronshapiro665a4a52016-04-12 21:10:00 -070024import com.google.common.collect.ImmutableSet;
25import com.google.common.collect.SetMultimap;
ronshapiro846fd942016-06-01 11:50:01 -070026import dagger.Binds;
ronshapiro665a4a52016-04-12 21:10:00 -070027import dagger.Provides;
28import dagger.multibindings.ElementsIntoSet;
29import dagger.multibindings.IntoMap;
30import dagger.multibindings.IntoSet;
31import dagger.producers.Produces;
32import java.lang.annotation.Annotation;
33import java.util.Map.Entry;
34import java.util.Set;
35import javax.annotation.processing.Messager;
ronshapiroed0d8522018-01-04 12:01:06 -080036import javax.inject.Inject;
ronshapiro665a4a52016-04-12 21:10:00 -070037import javax.lang.model.element.AnnotationMirror;
38import javax.lang.model.element.Element;
39import javax.tools.Diagnostic.Kind;
40
ronshapiro665a4a52016-04-12 21:10:00 -070041/**
dpb54203462016-10-19 13:46:43 -070042 * Processing step that verifies that {@link IntoSet}, {@link ElementsIntoSet} and {@link IntoMap}
43 * are not present on invalid elements.
ronshapiro665a4a52016-04-12 21:10:00 -070044 */
45final class MultibindingAnnotationsProcessingStep implements ProcessingStep {
46
ronshapiro846fd942016-06-01 11:50:01 -070047 private static final ImmutableSet<Class<? extends Annotation>> VALID_BINDING_ANNOTATIONS =
48 ImmutableSet.of(Provides.class, Produces.class, Binds.class);
49
ronshapiro665a4a52016-04-12 21:10:00 -070050 private final Messager messager;
51
ronshapiroed0d8522018-01-04 12:01:06 -080052 @Inject
ronshapiro665a4a52016-04-12 21:10:00 -070053 MultibindingAnnotationsProcessingStep(Messager messager) {
54 this.messager = messager;
55 }
56
57 @Override
58 public Set<? extends Class<? extends Annotation>> annotations() {
59 return ImmutableSet.of(IntoSet.class, ElementsIntoSet.class, IntoMap.class);
60 }
61
62 @Override
63 public Set<Element> process(
64 SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) {
65 for (Entry<Class<? extends Annotation>, Element> entry : elementsByAnnotation.entries()) {
66 Element element = entry.getValue();
ronshapiro846fd942016-06-01 11:50:01 -070067 if (!isAnyAnnotationPresent(element, VALID_BINDING_ANNOTATIONS)) {
ronshapiro665a4a52016-04-12 21:10:00 -070068 AnnotationMirror annotation = getAnnotationMirror(entry.getValue(), entry.getKey()).get();
69 messager.printMessage(
ronshapiro846fd942016-06-01 11:50:01 -070070 Kind.ERROR, MULTIBINDING_ANNOTATION_NOT_ON_BINDING_METHOD, element, annotation);
ronshapiro665a4a52016-04-12 21:10:00 -070071 }
72 }
73 return ImmutableSet.of();
74 }
ronshapiro665a4a52016-04-12 21:10:00 -070075}