1 /* 2 * Copyright (C) 2014 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.internal.codegen.processingstep; 18 19 import static androidx.room.compiler.processing.XElementKt.isConstructor; 20 import static androidx.room.compiler.processing.XElementKt.isField; 21 import static androidx.room.compiler.processing.XElementKt.isMethod; 22 import static dagger.internal.codegen.xprocessing.XElements.asConstructor; 23 import static dagger.internal.codegen.xprocessing.XElements.asField; 24 import static dagger.internal.codegen.xprocessing.XElements.asMethod; 25 26 import androidx.room.compiler.processing.XElement; 27 import com.google.common.collect.ImmutableSet; 28 import com.google.common.collect.Sets; 29 import com.squareup.javapoet.ClassName; 30 import dagger.internal.codegen.binding.InjectBindingRegistry; 31 import dagger.internal.codegen.javapoet.TypeNames; 32 import java.util.Set; 33 import javax.inject.Inject; 34 35 /** 36 * An annotation processor for generating Dagger implementation code based on the {@link Inject} 37 * annotation. 38 */ 39 // TODO(gak): add some error handling for bad source files 40 // TODO(bcorso): Add support in TypeCheckingProcessingStep to perform custom validation and use 41 // SuperficialInjectValidator rather than SuperficialValidator. 42 final class InjectProcessingStep extends TypeCheckingProcessingStep<XElement> { 43 private final InjectBindingRegistry injectBindingRegistry; 44 private final Set<XElement> processedElements = Sets.newHashSet(); 45 46 @Inject InjectProcessingStep(InjectBindingRegistry injectBindingRegistry)47 InjectProcessingStep(InjectBindingRegistry injectBindingRegistry) { 48 this.injectBindingRegistry = injectBindingRegistry; 49 } 50 51 @Override annotationClassNames()52 public ImmutableSet<ClassName> annotationClassNames() { 53 return ImmutableSet.of(TypeNames.INJECT, TypeNames.INJECT_JAVAX, TypeNames.ASSISTED_INJECT); 54 } 55 56 // Override to avoid prevalidation. The InjectProcessingStep does all of the required validation 57 // within InjectValidator so there's no need to prevalidate the nearest enclosing type element. 58 // TODO(bcorso): Once all processing steps handle their own validation we can remove this. 59 @Override requiresPreValidation()60 protected boolean requiresPreValidation() { 61 return false; 62 } 63 64 @Override process(XElement injectElement, ImmutableSet<ClassName> annotations)65 protected void process(XElement injectElement, ImmutableSet<ClassName> annotations) { 66 // Only process an element once to avoid getting duplicate errors when an element is annotated 67 // with multiple inject annotations. 68 if (processedElements.contains(injectElement)) { 69 return; 70 } 71 72 if (isConstructor(injectElement)) { 73 injectBindingRegistry.tryRegisterInjectConstructor(asConstructor(injectElement)); 74 } else if (isField(injectElement)) { 75 injectBindingRegistry.tryRegisterInjectField(asField(injectElement)); 76 } else if (isMethod(injectElement)) { 77 injectBindingRegistry.tryRegisterInjectMethod(asMethod(injectElement)); 78 } 79 80 processedElements.add(injectElement); 81 } 82 } 83