blob: 053506237d847e8e0dbaad406a0d5e9257889702 [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;
dpb3b0fcc82018-11-29 13:26:30 -080020import static javax.tools.Diagnostic.Kind.ERROR;
gake55f0742016-07-12 15:36:42 -070021
dpb3b0fcc82018-11-29 13:26:30 -080022import com.google.auto.common.MoreElements;
ronshapiro665a4a52016-04-12 21:10:00 -070023import com.google.common.collect.ImmutableSet;
ronshapiro665a4a52016-04-12 21:10:00 -070024import dagger.multibindings.ElementsIntoSet;
25import dagger.multibindings.IntoMap;
26import dagger.multibindings.IntoSet;
ronshapiro665a4a52016-04-12 21:10:00 -070027import java.lang.annotation.Annotation;
ronshapiro665a4a52016-04-12 21:10:00 -070028import java.util.Set;
29import javax.annotation.processing.Messager;
ronshapiroed0d8522018-01-04 12:01:06 -080030import javax.inject.Inject;
dpb4df5adf2018-10-25 14:09:30 -070031import javax.lang.model.element.ExecutableElement;
ronshapiro665a4a52016-04-12 21:10:00 -070032
ronshapiro665a4a52016-04-12 21:10:00 -070033/**
dpb54203462016-10-19 13:46:43 -070034 * Processing step that verifies that {@link IntoSet}, {@link ElementsIntoSet} and {@link IntoMap}
dpb4df5adf2018-10-25 14:09:30 -070035 * are not present on non-binding methods.
ronshapiro665a4a52016-04-12 21:10:00 -070036 */
dpb3b0fcc82018-11-29 13:26:30 -080037final class MultibindingAnnotationsProcessingStep
38 extends TypeCheckingProcessingStep<ExecutableElement> {
dpb4df5adf2018-10-25 14:09:30 -070039 private final AnyBindingMethodValidator anyBindingMethodValidator;
ronshapiro665a4a52016-04-12 21:10:00 -070040 private final Messager messager;
41
ronshapiroed0d8522018-01-04 12:01:06 -080042 @Inject
dpb4df5adf2018-10-25 14:09:30 -070043 MultibindingAnnotationsProcessingStep(
44 AnyBindingMethodValidator anyBindingMethodValidator, Messager messager) {
dpb3b0fcc82018-11-29 13:26:30 -080045 super(MoreElements::asExecutable);
dpb4df5adf2018-10-25 14:09:30 -070046 this.anyBindingMethodValidator = anyBindingMethodValidator;
ronshapiro665a4a52016-04-12 21:10:00 -070047 this.messager = messager;
48 }
49
50 @Override
51 public Set<? extends Class<? extends Annotation>> annotations() {
52 return ImmutableSet.of(IntoSet.class, ElementsIntoSet.class, IntoMap.class);
53 }
54
55 @Override
dpb3b0fcc82018-11-29 13:26:30 -080056 protected void process(
57 ExecutableElement method, ImmutableSet<Class<? extends Annotation>> annotations) {
58 if (!anyBindingMethodValidator.isBindingMethod(method)) {
59 annotations.forEach(
60 annotation ->
61 messager.printMessage(
62 ERROR,
63 "Multibinding annotations may only be on @Provides, @Produces, or @Binds methods",
64 method,
65 getAnnotationMirror(method, annotation).get()));
66 }
ronshapiro665a4a52016-04-12 21:10:00 -070067 }
ronshapiro665a4a52016-04-12 21:10:00 -070068}