aboutsummaryrefslogtreecommitdiff
path: root/java/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessor.java
blob: e0bfca9305e2e7e1df6e8fa51bd2fccdb951eec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright (C) 2019 The Dagger Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dagger.hilt.processor.internal.definecomponent;

import static net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;
import dagger.hilt.processor.internal.BaseProcessor;
import dagger.hilt.processor.internal.ClassNames;
import dagger.hilt.processor.internal.Processors;
import dagger.hilt.processor.internal.definecomponent.DefineComponentBuilderMetadatas.DefineComponentBuilderMetadata;
import dagger.hilt.processor.internal.definecomponent.DefineComponentMetadatas.DefineComponentMetadata;
import java.io.IOException;
import java.util.Set;
import javax.annotation.processing.Processor;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;

/**
 * A processor for {@link dagger.hilt.DefineComponent} and {@link
 * dagger.hilt.DefineComponent.Builder}.
 */
@IncrementalAnnotationProcessor(ISOLATING)
@AutoService(Processor.class)
public final class DefineComponentProcessor extends BaseProcessor {
  private final DefineComponentMetadatas componentMetadatas = DefineComponentMetadatas.create();
  private final DefineComponentBuilderMetadatas componentBuilderMetadatas =
      DefineComponentBuilderMetadatas.create(componentMetadatas);

  @Override
  public Set<String> getSupportedAnnotationTypes() {
    return ImmutableSet.of(
        ClassNames.DEFINE_COMPONENT.toString(), ClassNames.DEFINE_COMPONENT_BUILDER.toString());
  }

  @Override
  protected void processEach(TypeElement annotation, Element element) throws Exception {
    if (ClassName.get(annotation).equals(ClassNames.DEFINE_COMPONENT)) {
      // TODO(bcorso): For cycles we currently process each element in the cycle. We should skip
      // processing of subsequent elements in a cycle, but this requires ensuring that the first
      // element processed is always the same so that our failure tests are stable.
      DefineComponentMetadata metadata = componentMetadatas.get(element);
      generateFile("component", metadata.component());
    } else if (ClassName.get(annotation).equals(ClassNames.DEFINE_COMPONENT_BUILDER)) {
      DefineComponentBuilderMetadata metadata = componentBuilderMetadatas.get(element);
      generateFile("builder", metadata.builder());
    } else {
      throw new AssertionError("Unhandled annotation type: " + annotation);
    }
  }

  private void generateFile(String member, TypeElement typeElement) throws IOException {
    TypeSpec.Builder builder =
        TypeSpec.interfaceBuilder(Processors.getFullEnclosedName(typeElement))
            .addOriginatingElement(typeElement)
            .addAnnotation(
                AnnotationSpec.builder(ClassNames.DEFINE_COMPONENT_CLASSES)
                    .addMember(member, "$S", typeElement.getQualifiedName())
                    .build());

    Processors.addGeneratedAnnotation(builder, processingEnv, getClass());

    JavaFile.builder(DefineComponents.AGGREGATING_PACKAGE, builder.build())
        .build()
        .writeTo(processingEnv.getFiler());
  }
}