| 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; |
| ronshapiro | ed0d852 | 2018-01-04 12:01:06 -0800 | [diff] [blame^] | 36 | import javax.inject.Inject; |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 37 | import javax.lang.model.element.AnnotationMirror; |
| 38 | import javax.lang.model.element.Element; |
| 39 | import javax.tools.Diagnostic.Kind; |
| 40 | |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 41 | /** |
| dpb | 5420346 | 2016-10-19 13:46:43 -0700 | [diff] [blame] | 42 | * Processing step that verifies that {@link IntoSet}, {@link ElementsIntoSet} and {@link IntoMap} |
| 43 | * are not present on invalid elements. |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 44 | */ |
| 45 | final class MultibindingAnnotationsProcessingStep implements ProcessingStep { |
| 46 | |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 47 | private static final ImmutableSet<Class<? extends Annotation>> VALID_BINDING_ANNOTATIONS = |
| 48 | ImmutableSet.of(Provides.class, Produces.class, Binds.class); |
| 49 | |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 50 | private final Messager messager; |
| 51 | |
| ronshapiro | ed0d852 | 2018-01-04 12:01:06 -0800 | [diff] [blame^] | 52 | @Inject |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 53 | 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(); |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 67 | if (!isAnyAnnotationPresent(element, VALID_BINDING_ANNOTATIONS)) { |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 68 | AnnotationMirror annotation = getAnnotationMirror(entry.getValue(), entry.getKey()).get(); |
| 69 | messager.printMessage( |
| ronshapiro | 846fd94 | 2016-06-01 11:50:01 -0700 | [diff] [blame] | 70 | Kind.ERROR, MULTIBINDING_ANNOTATION_NOT_ON_BINDING_METHOD, element, annotation); |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | return ImmutableSet.of(); |
| 74 | } |
| ronshapiro | 665a4a5 | 2016-04-12 21:10:00 -0700 | [diff] [blame] | 75 | } |