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 androidx.room.compiler.processing.XElementKt.isMethod; 20 import static androidx.room.compiler.processing.XElementKt.isMethodParameter; 21 22 import androidx.room.compiler.processing.XElement; 23 import androidx.room.compiler.processing.XExecutableParameterElement; 24 import androidx.room.compiler.processing.XMessager; 25 import androidx.room.compiler.processing.XMethodElement; 26 import com.google.common.collect.ImmutableSet; 27 import com.squareup.javapoet.ClassName; 28 import dagger.internal.codegen.javapoet.TypeNames; 29 import dagger.internal.codegen.validation.BindsInstanceMethodValidator; 30 import dagger.internal.codegen.validation.BindsInstanceParameterValidator; 31 import javax.inject.Inject; 32 33 /** 34 * Processing step that validates that the {@code BindsInstance} annotation is applied to the 35 * correct elements. 36 */ 37 final class BindsInstanceProcessingStep extends TypeCheckingProcessingStep<XElement> { 38 private final BindsInstanceMethodValidator methodValidator; 39 private final BindsInstanceParameterValidator parameterValidator; 40 private final XMessager messager; 41 42 @Inject BindsInstanceProcessingStep( BindsInstanceMethodValidator methodValidator, BindsInstanceParameterValidator parameterValidator, XMessager messager)43 BindsInstanceProcessingStep( 44 BindsInstanceMethodValidator methodValidator, 45 BindsInstanceParameterValidator parameterValidator, 46 XMessager messager) { 47 this.methodValidator = methodValidator; 48 this.parameterValidator = parameterValidator; 49 this.messager = messager; 50 } 51 52 @Override annotationClassNames()53 public ImmutableSet<ClassName> annotationClassNames() { 54 return ImmutableSet.of(TypeNames.BINDS_INSTANCE); 55 } 56 57 @Override process(XElement element, ImmutableSet<ClassName> annotations)58 protected void process(XElement element, ImmutableSet<ClassName> annotations) { 59 if (isMethod(element)) { 60 methodValidator.validate((XMethodElement) element).printMessagesTo(messager); 61 } else if (isMethodParameter(element)) { 62 parameterValidator.validate((XExecutableParameterElement) element).printMessagesTo(messager); 63 } else { 64 throw new AssertionError(element); 65 } 66 } 67 } 68