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