blob: cc1e164dba9afda403dc01ab5b262a5195b443d7 [file] [log] [blame]
dpb3a8d6ea2016-12-12 10:04:38 -08001/*
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
17package dagger.internal.codegen;
18
19import static com.google.common.base.Preconditions.checkArgument;
20import static javax.lang.model.util.ElementFilter.methodsIn;
21
22import com.google.auto.common.BasicAnnotationProcessor.ProcessingStep;
23import com.google.common.collect.ImmutableSet;
24import com.google.common.collect.SetMultimap;
25import java.lang.annotation.Annotation;
26import java.util.Set;
27import javax.annotation.processing.Messager;
28import javax.lang.model.element.Element;
29import javax.lang.model.element.ExecutableElement;
30
31/** A step that validates all binding methods that were not validated while processing modules. */
32final class BindingMethodProcessingStep implements ProcessingStep {
33
34 private final Messager messager;
35 private final AnyBindingMethodValidator anyBindingMethodValidator;
36
37 BindingMethodProcessingStep(
38 Messager messager, AnyBindingMethodValidator anyBindingMethodValidator) {
39 this.messager = messager;
40 this.anyBindingMethodValidator = anyBindingMethodValidator;
41 }
42
43 @Override
44 public Set<? extends Class<? extends Annotation>> annotations() {
45 return anyBindingMethodValidator.methodAnnotations();
46 }
47
48 @Override
49 public Set<Element> process(
50 SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) {
51 for (ExecutableElement method : methodsIn(elementsByAnnotation.values())) {
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 return ImmutableSet.of();
62 }
63}