blob: be8c975a11a86a80fa418c018a7bf9b8e52b95cc [file] [log] [blame]
Christian Edward Gruber54b30172014-03-17 08:39:49 -07001/*
ronshapiro5dde42d2016-06-17 09:03:35 -07002 * Copyright (C) 2014 The Dagger Authors.
Christian Edward Gruber54b30172014-03-17 08:39:49 -07003 *
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 */
dpb1b65b6a2016-07-11 12:11:24 -070016
Christian Edward Gruber54b30172014-03-17 08:39:49 -070017package dagger.internal.codegen;
18
bederc82d8ab2015-12-30 06:45:27 -080019import com.google.auto.common.MoreElements;
Christian Edward Gruber54b30172014-03-17 08:39:49 -070020import com.google.common.collect.ImmutableSet;
gak4d6bb332014-12-10 12:16:44 -080021import java.lang.annotation.Annotation;
Christian Edward Gruber54b30172014-03-17 08:39:49 -070022import java.util.Set;
Christian Edward Gruber54b30172014-03-17 08:39:49 -070023import javax.inject.Inject;
24import javax.lang.model.element.Element;
dpb3b0fcc82018-11-29 13:26:30 -080025import javax.lang.model.element.ElementVisitor;
Christian Edward Gruber54b30172014-03-17 08:39:49 -070026import javax.lang.model.element.ExecutableElement;
Christian Edward Gruber54b30172014-03-17 08:39:49 -070027import javax.lang.model.element.VariableElement;
dpb3b0fcc82018-11-29 13:26:30 -080028import javax.lang.model.util.ElementKindVisitor8;
Christian Edward Gruber54b30172014-03-17 08:39:49 -070029
30/**
31 * An annotation processor for generating Dagger implementation code based on the {@link Inject}
32 * annotation.
Christian Edward Gruber54b30172014-03-17 08:39:49 -070033 */
dpb3b0fcc82018-11-29 13:26:30 -080034// TODO(gak): add some error handling for bad source files
35final class InjectProcessingStep extends TypeCheckingProcessingStep<Element> {
36 private final ElementVisitor<Void, Void> visitor;
Christian Edward Gruber54b30172014-03-17 08:39:49 -070037
ronshapiroed0d8522018-01-04 12:01:06 -080038 @Inject
ronshapiro6863e7e2017-11-07 08:54:22 -080039 InjectProcessingStep(InjectBindingRegistry injectBindingRegistry) {
dpb3b0fcc82018-11-29 13:26:30 -080040 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 Gruber54b30172014-03-17 08:39:49 -070064 }
65
66 @Override
gak4d6bb332014-12-10 12:16:44 -080067 public Set<Class<? extends Annotation>> annotations() {
dpb3b0fcc82018-11-29 13:26:30 -080068 return ImmutableSet.of(Inject.class);
gak4d6bb332014-12-10 12:16:44 -080069 }
70
71 @Override
dpb3b0fcc82018-11-29 13:26:30 -080072 protected void process(
73 Element injectElement, ImmutableSet<Class<? extends Annotation>> annotations) {
74 injectElement.accept(visitor, null);
Christian Edward Gruber54b30172014-03-17 08:39:49 -070075 }
Christian Edward Gruber54b30172014-03-17 08:39:49 -070076}