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.processingstep; 18 19 import static javax.tools.Diagnostic.Kind.ERROR; 20 21 import androidx.room.compiler.processing.XExecutableElement; 22 import androidx.room.compiler.processing.XMessager; 23 import com.google.common.collect.ImmutableSet; 24 import com.squareup.javapoet.ClassName; 25 import dagger.internal.codegen.javapoet.TypeNames; 26 import dagger.internal.codegen.validation.AnyBindingMethodValidator; 27 import javax.inject.Inject; 28 29 /** 30 * Processing step that verifies that {@link dagger.multibindings.IntoSet}, {@link 31 * dagger.multibindings.ElementsIntoSet} and {@link dagger.multibindings.IntoMap} are not present on 32 * non-binding methods. 33 */ 34 final class MultibindingAnnotationsProcessingStep 35 extends TypeCheckingProcessingStep<XExecutableElement> { 36 private final AnyBindingMethodValidator anyBindingMethodValidator; 37 private final XMessager messager; 38 39 @Inject MultibindingAnnotationsProcessingStep( AnyBindingMethodValidator anyBindingMethodValidator, XMessager messager)40 MultibindingAnnotationsProcessingStep( 41 AnyBindingMethodValidator anyBindingMethodValidator, XMessager messager) { 42 this.anyBindingMethodValidator = anyBindingMethodValidator; 43 this.messager = messager; 44 } 45 46 @Override annotationClassNames()47 public ImmutableSet<ClassName> annotationClassNames() { 48 return ImmutableSet.of(TypeNames.INTO_SET, TypeNames.ELEMENTS_INTO_SET, TypeNames.INTO_MAP); 49 } 50 51 @Override process(XExecutableElement method, ImmutableSet<ClassName> annotations)52 protected void process(XExecutableElement method, ImmutableSet<ClassName> annotations) { 53 if (!anyBindingMethodValidator.isBindingMethod(method)) { 54 annotations.forEach( 55 annotation -> 56 messager.printMessage( 57 ERROR, 58 "Multibinding annotations may only be on @Provides, @Produces, or @Binds methods", 59 method, 60 method.getAnnotation(annotation))); 61 } 62 } 63 } 64