| dpb | 3a8d6ea | 2016-12-12 10:04:38 -0800 | [diff] [blame] | 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; |
| 18 | |
| 19 | import static com.google.common.base.Preconditions.checkArgument; |
| dpb | 3a8d6ea | 2016-12-12 10:04:38 -0800 | [diff] [blame] | 20 | |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame] | 21 | import com.google.auto.common.MoreElements; |
| dpb | 3a8d6ea | 2016-12-12 10:04:38 -0800 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
| dpb | 3a8d6ea | 2016-12-12 10:04:38 -0800 | [diff] [blame] | 23 | import java.lang.annotation.Annotation; |
| 24 | import java.util.Set; |
| 25 | import javax.annotation.processing.Messager; |
| ronshapiro | ed0d852 | 2018-01-04 12:01:06 -0800 | [diff] [blame] | 26 | import javax.inject.Inject; |
| dpb | 3a8d6ea | 2016-12-12 10:04:38 -0800 | [diff] [blame] | 27 | import javax.lang.model.element.ExecutableElement; |
| 28 | |
| 29 | /** A step that validates all binding methods that were not validated while processing modules. */ |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame] | 30 | final class BindingMethodProcessingStep extends TypeCheckingProcessingStep<ExecutableElement> { |
| dpb | 3a8d6ea | 2016-12-12 10:04:38 -0800 | [diff] [blame] | 31 | |
| 32 | private final Messager messager; |
| 33 | private final AnyBindingMethodValidator anyBindingMethodValidator; |
| 34 | |
| ronshapiro | ed0d852 | 2018-01-04 12:01:06 -0800 | [diff] [blame] | 35 | @Inject |
| dpb | 3a8d6ea | 2016-12-12 10:04:38 -0800 | [diff] [blame] | 36 | BindingMethodProcessingStep( |
| 37 | Messager messager, AnyBindingMethodValidator anyBindingMethodValidator) { |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame] | 38 | super(MoreElements::asExecutable); |
| dpb | 3a8d6ea | 2016-12-12 10:04:38 -0800 | [diff] [blame] | 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 |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame] | 49 | protected void process( |
| 50 | ExecutableElement method, ImmutableSet<Class<? extends Annotation>> annotations) { |
| 51 | checkArgument( |
| 52 | anyBindingMethodValidator.isBindingMethod(method), |
| 53 | "%s is not annotated with any of %s", |
| 54 | method, |
| 55 | annotations()); |
| 56 | if (!anyBindingMethodValidator.wasAlreadyValidated(method)) { |
| 57 | anyBindingMethodValidator.validate(method).printMessagesTo(messager); |
| dpb | 3a8d6ea | 2016-12-12 10:04:38 -0800 | [diff] [blame] | 58 | } |
| dpb | 3a8d6ea | 2016-12-12 10:04:38 -0800 | [diff] [blame] | 59 | } |
| 60 | } |