• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 com.google.auto.common.MoreElements;
20 import com.google.common.collect.ImmutableSet;
21 import java.lang.annotation.Annotation;
22 import java.util.Set;
23 import javax.inject.Inject;
24 import javax.lang.model.element.Element;
25 import javax.lang.model.element.ElementVisitor;
26 import javax.lang.model.element.ExecutableElement;
27 import javax.lang.model.element.VariableElement;
28 import javax.lang.model.util.ElementKindVisitor8;
29 
30 /**
31  * An annotation processor for generating Dagger implementation code based on the {@link Inject}
32  * annotation.
33  */
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;
37 
38   @Inject
InjectProcessingStep(InjectBindingRegistry injectBindingRegistry)39   InjectProcessingStep(InjectBindingRegistry injectBindingRegistry) {
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         };
64   }
65 
66   @Override
annotations()67   public Set<Class<? extends Annotation>> annotations() {
68     return ImmutableSet.of(Inject.class);
69   }
70 
71   @Override
process( Element injectElement, ImmutableSet<Class<? extends Annotation>> annotations)72   protected void process(
73       Element injectElement, ImmutableSet<Class<? extends Annotation>> annotations) {
74     injectElement.accept(visitor, null);
75   }
76 }
77