1 /* 2 * Copyright (C) 2020 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.aliasof; 18 19 import static com.google.auto.common.MoreElements.asType; 20 import static net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING; 21 22 import com.google.auto.service.AutoService; 23 import com.google.common.collect.ImmutableList; 24 import com.google.common.collect.ImmutableSet; 25 import dagger.hilt.processor.internal.BaseProcessor; 26 import dagger.hilt.processor.internal.ClassNames; 27 import dagger.hilt.processor.internal.ProcessorErrors; 28 import dagger.hilt.processor.internal.Processors; 29 import java.util.Set; 30 import javax.annotation.processing.Processor; 31 import javax.lang.model.element.AnnotationMirror; 32 import javax.lang.model.element.Element; 33 import javax.lang.model.element.TypeElement; 34 import net.ltgt.gradle.incap.IncrementalAnnotationProcessor; 35 36 /** Processes the annotations annotated with {@link dagger.hilt.migration.AliasOf} */ 37 @IncrementalAnnotationProcessor(ISOLATING) 38 @AutoService(Processor.class) 39 public final class AliasOfProcessor extends BaseProcessor { 40 @Override getSupportedAnnotationTypes()41 public Set<String> getSupportedAnnotationTypes() { 42 return ImmutableSet.of(ClassNames.ALIAS_OF.toString()); 43 } 44 45 @Override processEach(TypeElement annotation, Element element)46 public void processEach(TypeElement annotation, Element element) throws Exception { 47 ProcessorErrors.checkState( 48 Processors.hasAnnotation(element, ClassNames.SCOPE), 49 element, 50 "%s should only be used on scopes." + " However, it was found annotating %s", 51 annotation, 52 element); 53 54 AnnotationMirror annotationMirror = 55 Processors.getAnnotationMirror(element, ClassNames.ALIAS_OF); 56 57 ImmutableList<TypeElement> defineComponentScopes = 58 Processors.getAnnotationClassValues(getElementUtils(), annotationMirror, "value"); 59 60 ProcessorErrors.checkState( 61 defineComponentScopes.size() >= 1, 62 element, 63 "@AliasOf annotation %s must declare at least one scope to alias.", 64 annotationMirror); 65 66 new AliasOfPropagatedDataGenerator(getProcessingEnv(), asType(element), defineComponentScopes) 67 .generate(); 68 } 69 } 70