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 com.google.auto.common.MoreElements; 20 import com.google.common.collect.ImmutableSet; 21 import dagger.BindsInstance; 22 import java.lang.annotation.Annotation; 23 import java.util.Set; 24 import javax.annotation.processing.Messager; 25 import javax.inject.Inject; 26 import javax.lang.model.element.Element; 27 28 /** 29 * Processing step that validates that the {@code BindsInstance} annotation is applied to the 30 * correct elements. 31 */ 32 public final class BindsInstanceProcessingStep extends TypeCheckingProcessingStep<Element> { 33 private final BindsInstanceMethodValidator methodValidator; 34 private final BindsInstanceParameterValidator parameterValidator; 35 private final Messager messager; 36 37 @Inject BindsInstanceProcessingStep( BindsInstanceMethodValidator methodValidator, BindsInstanceParameterValidator parameterValidator, Messager messager)38 BindsInstanceProcessingStep( 39 BindsInstanceMethodValidator methodValidator, 40 BindsInstanceParameterValidator parameterValidator, 41 Messager messager) { 42 super(element -> element); 43 this.methodValidator = methodValidator; 44 this.parameterValidator = parameterValidator; 45 this.messager = messager; 46 } 47 48 @Override annotations()49 public Set<? extends Class<? extends Annotation>> annotations() { 50 return ImmutableSet.of(BindsInstance.class); 51 } 52 53 @Override process(Element element, ImmutableSet<Class<? extends Annotation>> annotations)54 protected void process(Element element, ImmutableSet<Class<? extends Annotation>> annotations) { 55 switch (element.getKind()) { 56 case PARAMETER: 57 parameterValidator.validate(MoreElements.asVariable(element)).printMessagesTo(messager); 58 break; 59 case METHOD: 60 methodValidator.validate(MoreElements.asExecutable(element)).printMessagesTo(messager); 61 break; 62 default: 63 throw new AssertionError(element); 64 } 65 } 66 } 67