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 dagger.hilt.processor.internal.BaseProcessor; 26 import dagger.hilt.processor.internal.ClassNames; 27 import dagger.hilt.processor.internal.Processors; 28 import dagger.hilt.processor.internal.definecomponent.DefineComponentBuilderMetadatas.DefineComponentBuilderMetadata; 29 import dagger.hilt.processor.internal.definecomponent.DefineComponentMetadatas.DefineComponentMetadata; 30 import java.io.IOException; 31 import java.util.Set; 32 import javax.annotation.processing.Processor; 33 import javax.lang.model.element.Element; 34 import javax.lang.model.element.TypeElement; 35 import net.ltgt.gradle.incap.IncrementalAnnotationProcessor; 36 37 /** 38 * A processor for {@link dagger.hilt.DefineComponent} and {@link 39 * dagger.hilt.DefineComponent.Builder}. 40 */ 41 @IncrementalAnnotationProcessor(ISOLATING) 42 @AutoService(Processor.class) 43 public final class DefineComponentProcessor extends BaseProcessor { 44 private final DefineComponentMetadatas componentMetadatas = DefineComponentMetadatas.create(); 45 private final DefineComponentBuilderMetadatas componentBuilderMetadatas = 46 DefineComponentBuilderMetadatas.create(componentMetadatas); 47 48 @Override getSupportedAnnotationTypes()49 public Set<String> getSupportedAnnotationTypes() { 50 return ImmutableSet.of( 51 ClassNames.DEFINE_COMPONENT.toString(), ClassNames.DEFINE_COMPONENT_BUILDER.toString()); 52 } 53 54 @Override processEach(TypeElement annotation, Element element)55 protected void processEach(TypeElement annotation, Element element) throws Exception { 56 if (ClassName.get(annotation).equals(ClassNames.DEFINE_COMPONENT)) { 57 // TODO(bcorso): For cycles we currently process each element in the cycle. We should skip 58 // processing of subsequent elements in a cycle, but this requires ensuring that the first 59 // element processed is always the same so that our failure tests are stable. 60 DefineComponentMetadata metadata = componentMetadatas.get(element); 61 generateFile("component", metadata.component()); 62 } else if (ClassName.get(annotation).equals(ClassNames.DEFINE_COMPONENT_BUILDER)) { 63 DefineComponentBuilderMetadata metadata = componentBuilderMetadatas.get(element); 64 generateFile("builder", metadata.builder()); 65 } else { 66 throw new AssertionError("Unhandled annotation type: " + annotation); 67 } 68 } 69 generateFile(String member, TypeElement typeElement)70 private void generateFile(String member, TypeElement typeElement) throws IOException { 71 Processors.generateAggregatingClass( 72 ClassNames.DEFINE_COMPONENT_CLASSES_PACKAGE, 73 AnnotationSpec.builder(ClassNames.DEFINE_COMPONENT_CLASSES) 74 .addMember(member, "$S", typeElement.getQualifiedName()) 75 .build(), 76 typeElement, 77 getClass(), 78 getProcessingEnv()); 79 } 80 } 81