| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 1 | /* |
| ronshapiro | 5dde42d | 2016-06-17 09:03:35 -0700 | [diff] [blame] | 2 | * Copyright (C) 2014 The Dagger Authors. |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 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 | */ |
| dpb | 1b65b6a | 2016-07-11 12:11:24 -0700 | [diff] [blame] | 16 | |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 17 | package dagger.internal.codegen; |
| 18 | |
| beder | c82d8ab | 2015-12-30 06:45:27 -0800 | [diff] [blame] | 19 | import com.google.auto.common.MoreElements; |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
| gak | 4d6bb33 | 2014-12-10 12:16:44 -0800 | [diff] [blame] | 21 | import java.lang.annotation.Annotation; |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 22 | import java.util.Set; |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 23 | import javax.inject.Inject; |
| 24 | import javax.lang.model.element.Element; |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame] | 25 | import javax.lang.model.element.ElementVisitor; |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 26 | import javax.lang.model.element.ExecutableElement; |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 27 | import javax.lang.model.element.VariableElement; |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame] | 28 | import javax.lang.model.util.ElementKindVisitor8; |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * An annotation processor for generating Dagger implementation code based on the {@link Inject} |
| 32 | * annotation. |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 33 | */ |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame] | 34 | // TODO(gak): add some error handling for bad source files |
| 35 | final class InjectProcessingStep extends TypeCheckingProcessingStep<Element> { |
| 36 | private final ElementVisitor<Void, Void> visitor; |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 37 | |
| ronshapiro | ed0d852 | 2018-01-04 12:01:06 -0800 | [diff] [blame] | 38 | @Inject |
| ronshapiro | 6863e7e | 2017-11-07 08:54:22 -0800 | [diff] [blame] | 39 | InjectProcessingStep(InjectBindingRegistry injectBindingRegistry) { |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame] | 40 | super(e -> e); |
| 41 | this.visitor = |
| 42 | new ElementKindVisitor8<Void, Void>() { |
| 43 | @Override |
| 44 | public Void visitExecutableAsConstructor( |
| 45 | ExecutableElement constructorElement, Void aVoid) { |
| 46 | injectBindingRegistry.tryRegisterConstructor(constructorElement); |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public Void visitVariableAsField(VariableElement fieldElement, Void aVoid) { |
| 52 | injectBindingRegistry.tryRegisterMembersInjectedType( |
| 53 | MoreElements.asType(fieldElement.getEnclosingElement())); |
| 54 | return null; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public Void visitExecutableAsMethod(ExecutableElement methodElement, Void aVoid) { |
| 59 | injectBindingRegistry.tryRegisterMembersInjectedType( |
| 60 | MoreElements.asType(methodElement.getEnclosingElement())); |
| 61 | return null; |
| 62 | } |
| 63 | }; |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| gak | 4d6bb33 | 2014-12-10 12:16:44 -0800 | [diff] [blame] | 67 | public Set<Class<? extends Annotation>> annotations() { |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame] | 68 | return ImmutableSet.of(Inject.class); |
| gak | 4d6bb33 | 2014-12-10 12:16:44 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| dpb | 3b0fcc8 | 2018-11-29 13:26:30 -0800 | [diff] [blame] | 72 | protected void process( |
| 73 | Element injectElement, ImmutableSet<Class<? extends Annotation>> annotations) { |
| 74 | injectElement.accept(visitor, null); |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 75 | } |
| Christian Edward Gruber | 54b3017 | 2014-03-17 08:39:49 -0700 | [diff] [blame] | 76 | } |