• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.base.Preconditions.checkArgument;
20 
21 import com.google.auto.common.MoreElements;
22 import com.google.common.collect.ImmutableSet;
23 import java.lang.annotation.Annotation;
24 import java.util.Set;
25 import javax.annotation.processing.Messager;
26 import javax.inject.Inject;
27 import javax.lang.model.element.ExecutableElement;
28 
29 /** A step that validates all binding methods that were not validated while processing modules. */
30 public final class BindingMethodProcessingStep
31     extends TypeCheckingProcessingStep<ExecutableElement> {
32 
33   private final Messager messager;
34   private final AnyBindingMethodValidator anyBindingMethodValidator;
35 
36   @Inject
BindingMethodProcessingStep( Messager messager, AnyBindingMethodValidator anyBindingMethodValidator)37   BindingMethodProcessingStep(
38       Messager messager, AnyBindingMethodValidator anyBindingMethodValidator) {
39     super(MoreElements::asExecutable);
40     this.messager = messager;
41     this.anyBindingMethodValidator = anyBindingMethodValidator;
42   }
43 
44   @Override
annotations()45   public Set<? extends Class<? extends Annotation>> annotations() {
46     return anyBindingMethodValidator.methodAnnotations();
47   }
48 
49   @Override
process( ExecutableElement method, ImmutableSet<Class<? extends Annotation>> annotations)50   protected void process(
51       ExecutableElement method, ImmutableSet<Class<? extends Annotation>> annotations) {
52     checkArgument(
53         anyBindingMethodValidator.isBindingMethod(method),
54         "%s is not annotated with any of %s",
55         method,
56         annotations());
57     if (!anyBindingMethodValidator.wasAlreadyValidated(method)) {
58       anyBindingMethodValidator.validate(method).printMessagesTo(messager);
59     }
60   }
61 }
62