blob: df977c235eaf30d874ac6064a377b98e79f9317d [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;
36import javax.lang.model.element.AnnotationMirror;
37import javax.lang.model.element.Element;
38import javax.tools.Diagnostic.Kind;
39
ronshapiro665a4a52016-04-12 21:10:00 -070040/**
dpb54203462016-10-19 13:46:43 -070041 * Processing step that verifies that {@link IntoSet}, {@link ElementsIntoSet} and {@link IntoMap}
42 * are not present on invalid elements.
ronshapiro665a4a52016-04-12 21:10:00 -070043 */
44final class MultibindingAnnotationsProcessingStep implements ProcessingStep {
45
ronshapiro846fd942016-06-01 11:50:01 -070046 private static final ImmutableSet<Class<? extends Annotation>> VALID_BINDING_ANNOTATIONS =
47 ImmutableSet.of(Provides.class, Produces.class, Binds.class);
48
ronshapiro665a4a52016-04-12 21:10:00 -070049 private final Messager messager;
50
51 MultibindingAnnotationsProcessingStep(Messager messager) {
52 this.messager = messager;
53 }
54
55 @Override
56 public Set<? extends Class<? extends Annotation>> annotations() {
57 return ImmutableSet.of(IntoSet.class, ElementsIntoSet.class, IntoMap.class);
58 }
59
60 @Override
61 public Set<Element> process(
62 SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) {
63 for (Entry<Class<? extends Annotation>, Element> entry : elementsByAnnotation.entries()) {
64 Element element = entry.getValue();
ronshapiro846fd942016-06-01 11:50:01 -070065 if (!isAnyAnnotationPresent(element, VALID_BINDING_ANNOTATIONS)) {
ronshapiro665a4a52016-04-12 21:10:00 -070066 AnnotationMirror annotation = getAnnotationMirror(entry.getValue(), entry.getKey()).get();
67 messager.printMessage(
ronshapiro846fd942016-06-01 11:50:01 -070068 Kind.ERROR, MULTIBINDING_ANNOTATION_NOT_ON_BINDING_METHOD, element, annotation);
ronshapiro665a4a52016-04-12 21:10:00 -070069 }
70 }
71 return ImmutableSet.of();
72 }
ronshapiro665a4a52016-04-12 21:10:00 -070073}