• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.hilt.processor.internal.definecomponent;
18 
19 import static net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING;
20 
21 import com.google.auto.service.AutoService;
22 import com.google.common.collect.ImmutableSet;
23 import com.squareup.javapoet.AnnotationSpec;
24 import com.squareup.javapoet.ClassName;
25 import com.squareup.javapoet.JavaFile;
26 import com.squareup.javapoet.TypeSpec;
27 import dagger.hilt.processor.internal.BaseProcessor;
28 import dagger.hilt.processor.internal.ClassNames;
29 import dagger.hilt.processor.internal.Processors;
30 import dagger.hilt.processor.internal.definecomponent.DefineComponentBuilderMetadatas.DefineComponentBuilderMetadata;
31 import dagger.hilt.processor.internal.definecomponent.DefineComponentMetadatas.DefineComponentMetadata;
32 import java.io.IOException;
33 import java.util.Set;
34 import javax.annotation.processing.Processor;
35 import javax.lang.model.element.Element;
36 import javax.lang.model.element.TypeElement;
37 import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;
38 
39 /**
40  * A processor for {@link dagger.hilt.DefineComponent} and {@link
41  * dagger.hilt.DefineComponent.Builder}.
42  */
43 @IncrementalAnnotationProcessor(ISOLATING)
44 @AutoService(Processor.class)
45 public final class DefineComponentProcessor extends BaseProcessor {
46   private final DefineComponentMetadatas componentMetadatas = DefineComponentMetadatas.create();
47   private final DefineComponentBuilderMetadatas componentBuilderMetadatas =
48       DefineComponentBuilderMetadatas.create(componentMetadatas);
49 
50   @Override
getSupportedAnnotationTypes()51   public Set<String> getSupportedAnnotationTypes() {
52     return ImmutableSet.of(
53         ClassNames.DEFINE_COMPONENT.toString(), ClassNames.DEFINE_COMPONENT_BUILDER.toString());
54   }
55 
56   @Override
processEach(TypeElement annotation, Element element)57   protected void processEach(TypeElement annotation, Element element) throws Exception {
58     if (ClassName.get(annotation).equals(ClassNames.DEFINE_COMPONENT)) {
59       // TODO(bcorso): For cycles we currently process each element in the cycle. We should skip
60       // processing of subsequent elements in a cycle, but this requires ensuring that the first
61       // element processed is always the same so that our failure tests are stable.
62       DefineComponentMetadata metadata = componentMetadatas.get(element);
63       generateFile("component", metadata.component());
64     } else if (ClassName.get(annotation).equals(ClassNames.DEFINE_COMPONENT_BUILDER)) {
65       DefineComponentBuilderMetadata metadata = componentBuilderMetadatas.get(element);
66       generateFile("builder", metadata.builder());
67     } else {
68       throw new AssertionError("Unhandled annotation type: " + annotation);
69     }
70   }
71 
generateFile(String member, TypeElement typeElement)72   private void generateFile(String member, TypeElement typeElement) throws IOException {
73     TypeSpec.Builder builder =
74         TypeSpec.interfaceBuilder(Processors.getFullEnclosedName(typeElement))
75             .addOriginatingElement(typeElement)
76             .addAnnotation(
77                 AnnotationSpec.builder(ClassNames.DEFINE_COMPONENT_CLASSES)
78                     .addMember(member, "$S", typeElement.getQualifiedName())
79                     .build());
80 
81     Processors.addGeneratedAnnotation(builder, processingEnv, getClass());
82 
83     JavaFile.builder(DefineComponents.AGGREGATING_PACKAGE, builder.build())
84         .build()
85         .writeTo(processingEnv.getFiler());
86   }
87 }
88