| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 1 | /* |
| ronshapiro | 5dde42d | 2016-06-17 09:03:35 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Dagger Authors. |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 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 | */ |
| dpb | 1b65b6a | 2016-07-11 12:11:24 -0700 | [diff] [blame] | 16 | |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 17 | package dagger.internal.codegen; |
| 18 | |
| dpb | ffd98f6 | 2016-12-20 10:05:16 -0800 | [diff] [blame] | 19 | import static dagger.internal.codegen.DaggerElements.getAnnotationMirror; |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame^] | 20 | import static javax.tools.Diagnostic.Kind.ERROR; |
| gak | e55f074 | 2016-07-12 15:36:42 -0700 | [diff] [blame] | 21 | |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame^] | 22 | import com.google.auto.common.MoreElements; |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSet; |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 24 | import dagger.multibindings.ElementsIntoSet; |
| 25 | import dagger.multibindings.IntoMap; |
| 26 | import dagger.multibindings.IntoSet; |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 27 | import java.lang.annotation.Annotation; |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 28 | import java.util.Set; |
| 29 | import javax.annotation.processing.Messager; |
| ronshapiro | ed0d852 | 2018-01-04 12:01:06 -0800 | [diff] [blame] | 30 | import javax.inject.Inject; |
| dpb | 4df5adf | 2018-10-25 14:09:30 -0700 | [diff] [blame] | 31 | import javax.lang.model.element.ExecutableElement; |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 32 | |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 33 | /** |
| dpb | 5420346 | 2016-10-19 13:46:43 -0700 | [diff] [blame] | 34 | * Processing step that verifies that {@link IntoSet}, {@link ElementsIntoSet} and {@link IntoMap} |
| dpb | 4df5adf | 2018-10-25 14:09:30 -0700 | [diff] [blame] | 35 | * are not present on non-binding methods. |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 36 | */ |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame^] | 37 | final class MultibindingAnnotationsProcessingStep |
| 38 | extends TypeCheckingProcessingStep<ExecutableElement> { |
| dpb | 4df5adf | 2018-10-25 14:09:30 -0700 | [diff] [blame] | 39 | private final AnyBindingMethodValidator anyBindingMethodValidator; |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 40 | private final Messager messager; |
| 41 | |
| ronshapiro | ed0d852 | 2018-01-04 12:01:06 -0800 | [diff] [blame] | 42 | @Inject |
| dpb | 4df5adf | 2018-10-25 14:09:30 -0700 | [diff] [blame] | 43 | MultibindingAnnotationsProcessingStep( |
| 44 | AnyBindingMethodValidator anyBindingMethodValidator, Messager messager) { |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame^] | 45 | super(MoreElements::asExecutable); |
| dpb | 4df5adf | 2018-10-25 14:09:30 -0700 | [diff] [blame] | 46 | this.anyBindingMethodValidator = anyBindingMethodValidator; |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 47 | 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 |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame^] | 56 | 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 | } |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 67 | } |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 68 | } |