| 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; |
| 20 | import static dagger.internal.codegen.DaggerElements.isAnyAnnotationPresent; |
| gak | e55f074 | 2016-07-12 15:36:42 -0700 | [diff] [blame] | 21 | import static dagger.internal.codegen.ErrorMessages.MULTIBINDING_ANNOTATION_NOT_ON_BINDING_METHOD; |
| gak | e55f074 | 2016-07-12 15:36:42 -0700 | [diff] [blame] | 22 | |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 23 | import com.google.auto.common.BasicAnnotationProcessor.ProcessingStep; |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableSet; |
| 25 | import com.google.common.collect.SetMultimap; |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 26 | import dagger.Binds; |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 27 | import dagger.Provides; |
| 28 | import dagger.multibindings.ElementsIntoSet; |
| 29 | import dagger.multibindings.IntoMap; |
| 30 | import dagger.multibindings.IntoSet; |
| 31 | import dagger.producers.Produces; |
| 32 | import java.lang.annotation.Annotation; |
| 33 | import java.util.Map.Entry; |
| 34 | import java.util.Set; |
| 35 | import javax.annotation.processing.Messager; |
| 36 | import javax.lang.model.element.AnnotationMirror; |
| 37 | import javax.lang.model.element.Element; |
| 38 | import javax.tools.Diagnostic.Kind; |
| 39 | |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 40 | /** |
| dpb | 5420346 | 2016-10-19 13:46:43 -0700 | [diff] [blame] | 41 | * Processing step that verifies that {@link IntoSet}, {@link ElementsIntoSet} and {@link IntoMap} |
| 42 | * are not present on invalid elements. |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 43 | */ |
| 44 | final class MultibindingAnnotationsProcessingStep implements ProcessingStep { |
| 45 | |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 46 | private static final ImmutableSet<Class<? extends Annotation>> VALID_BINDING_ANNOTATIONS = |
| 47 | ImmutableSet.of(Provides.class, Produces.class, Binds.class); |
| 48 | |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 49 | 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(); |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 65 | if (!isAnyAnnotationPresent(element, VALID_BINDING_ANNOTATIONS)) { |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 66 | AnnotationMirror annotation = getAnnotationMirror(entry.getValue(), entry.getKey()).get(); |
| 67 | messager.printMessage( |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 68 | Kind.ERROR, MULTIBINDING_ANNOTATION_NOT_ON_BINDING_METHOD, element, annotation); |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | return ImmutableSet.of(); |
| 72 | } |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 73 | } |