1 /* 2 * Copyright (C) 2016 The Dagger Authors. 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 */ 16 17 package dagger.internal.codegen; 18 19 import static dagger.internal.codegen.langmodel.DaggerElements.getAnnotationMirror; 20 import static javax.tools.Diagnostic.Kind.ERROR; 21 22 import com.google.auto.common.MoreElements; 23 import com.google.common.collect.ImmutableSet; 24 import dagger.multibindings.ElementsIntoSet; 25 import dagger.multibindings.IntoMap; 26 import dagger.multibindings.IntoSet; 27 import java.lang.annotation.Annotation; 28 import java.util.Set; 29 import javax.annotation.processing.Messager; 30 import javax.inject.Inject; 31 import javax.lang.model.element.ExecutableElement; 32 33 /** 34 * Processing step that verifies that {@link IntoSet}, {@link ElementsIntoSet} and {@link IntoMap} 35 * are not present on non-binding methods. 36 */ 37 final class MultibindingAnnotationsProcessingStep 38 extends TypeCheckingProcessingStep<ExecutableElement> { 39 private final AnyBindingMethodValidator anyBindingMethodValidator; 40 private final Messager messager; 41 42 @Inject MultibindingAnnotationsProcessingStep( AnyBindingMethodValidator anyBindingMethodValidator, Messager messager)43 MultibindingAnnotationsProcessingStep( 44 AnyBindingMethodValidator anyBindingMethodValidator, Messager messager) { 45 super(MoreElements::asExecutable); 46 this.anyBindingMethodValidator = anyBindingMethodValidator; 47 this.messager = messager; 48 } 49 50 @Override annotations()51 public Set<? extends Class<? extends Annotation>> annotations() { 52 return ImmutableSet.of(IntoSet.class, ElementsIntoSet.class, IntoMap.class); 53 } 54 55 @Override process( ExecutableElement method, ImmutableSet<Class<? extends Annotation>> annotations)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 } 67 } 68 } 69