• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Google, Inc.
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 package dagger.internal.codegen;
17 
18 import com.google.auto.common.BasicAnnotationProcessor.ProcessingStep;
19 import com.google.auto.common.MoreElements;
20 import com.google.common.collect.ImmutableSet;
21 import com.google.common.collect.Maps;
22 import com.google.common.collect.SetMultimap;
23 import dagger.producers.ProductionComponent;
24 import java.lang.annotation.Annotation;
25 import java.util.Map;
26 import java.util.Set;
27 import javax.annotation.processing.Messager;
28 import javax.lang.model.element.Element;
29 import javax.lang.model.element.TypeElement;
30 
31 /**
32  * A {@link ProcessingStep} that is responsible for dealing with the {@link ProductionComponent}
33  * annotation as part of the {@link ComponentProcessor}.
34  *
35  * @author Jesse Beder
36  */
37 final class ProductionComponentProcessingStep extends AbstractComponentProcessingStep {
38   private final Messager messager;
39   private final ProductionComponentValidator componentValidator;
40   private final BuilderValidator componentBuilderValidator;
41 
ProductionComponentProcessingStep( Messager messager, ProductionComponentValidator componentValidator, BuilderValidator componentBuilderValidator, ComponentHierarchyValidator componentHierarchyValidator, BindingGraphValidator bindingGraphValidator, ComponentDescriptor.Factory componentDescriptorFactory, BindingGraph.Factory bindingGraphFactory, ComponentGenerator componentGenerator)42   ProductionComponentProcessingStep(
43       Messager messager,
44       ProductionComponentValidator componentValidator,
45       BuilderValidator componentBuilderValidator,
46       ComponentHierarchyValidator componentHierarchyValidator,
47       BindingGraphValidator bindingGraphValidator,
48       ComponentDescriptor.Factory componentDescriptorFactory,
49       BindingGraph.Factory bindingGraphFactory,
50       ComponentGenerator componentGenerator) {
51     super(
52         ProductionComponent.class,
53         messager,
54         componentHierarchyValidator,
55         bindingGraphValidator,
56         componentDescriptorFactory,
57         bindingGraphFactory,
58         componentGenerator);
59     this.messager = messager;
60     this.componentValidator = componentValidator;
61     this.componentBuilderValidator = componentBuilderValidator;
62   }
63 
64   @Override
annotations()65   public Set<Class<? extends Annotation>> annotations() {
66     return ImmutableSet.<Class<? extends Annotation>>of(
67         ProductionComponent.class, ProductionComponent.Builder.class);
68   }
69 
70   // TODO(beder): Move common logic into the AbstractComponentProcessingStep when implementing
71   // production subcomponents.
72   @Override
componentElementValidator( SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation)73   protected ComponentElementValidator componentElementValidator(
74       SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) {
75     final Map<Element, ValidationReport<TypeElement>> builderReportsByComponent =
76         processComponentBuilders(elementsByAnnotation.get(ProductionComponent.Builder.class));
77     return new ComponentElementValidator() {
78       @Override
79       boolean validateComponent(TypeElement componentTypeElement, Messager messager) {
80         ValidationReport<TypeElement> validationReport =
81             componentValidator.validate(componentTypeElement);
82         validationReport.printMessagesTo(messager);
83         if (!validationReport.isClean()) {
84           return false;
85         }
86         ValidationReport<?> builderReport = builderReportsByComponent.get(componentTypeElement);
87         return builderReport == null || builderReport.isClean();
88       }
89     };
90   }
91 
92   private Map<Element, ValidationReport<TypeElement>> processComponentBuilders(
93       Set<? extends Element> componentBuilderElements) {
94     Map<Element, ValidationReport<TypeElement>> builderReportsByComponent = Maps.newHashMap();
95     for (Element element : componentBuilderElements) {
96       ValidationReport<TypeElement> report =
97           componentBuilderValidator.validate(MoreElements.asType(element));
98       report.printMessagesTo(messager);
99       builderReportsByComponent.put(element.getEnclosingElement(), report);
100     }
101     return builderReportsByComponent;
102   }
103 }
104