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.uninstallmodules; 18 19 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList; 20 import static net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.ISOLATING; 21 22 import com.google.auto.common.MoreElements; 23 import com.google.auto.service.AutoService; 24 import com.google.common.collect.ImmutableList; 25 import com.google.common.collect.ImmutableSet; 26 import com.squareup.javapoet.ClassName; 27 import dagger.hilt.processor.internal.BaseProcessor; 28 import dagger.hilt.processor.internal.ClassNames; 29 import dagger.hilt.processor.internal.ProcessorErrors; 30 import dagger.hilt.processor.internal.Processors; 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 /** Validates {@link dagger.hilt.android.testing.UninstallModules} usages. */ 38 @IncrementalAnnotationProcessor(ISOLATING) 39 @AutoService(Processor.class) 40 public final class UninstallModulesProcessor extends BaseProcessor { 41 42 @Override getSupportedAnnotationTypes()43 public Set<String> getSupportedAnnotationTypes() { 44 return ImmutableSet.of(ClassNames.UNINSTALL_MODULES.toString()); 45 } 46 47 @Override processEach(TypeElement annotation, Element element)48 public void processEach(TypeElement annotation, Element element) throws Exception { 49 // TODO(bcorso): Consider using RootType to check this? 50 // TODO(bcorso): Loosen this restriction to allow defining sets of ignored modules in libraries. 51 ProcessorErrors.checkState( 52 MoreElements.isType(element) 53 && Processors.hasAnnotation(element, ClassNames.HILT_ANDROID_TEST), 54 element, 55 "@%s should only be used on test classes annotated with @%s, but found: %s", 56 annotation.getSimpleName(), 57 ClassNames.HILT_ANDROID_TEST.simpleName(), 58 element); 59 60 TypeElement testElement = MoreElements.asType(element); 61 ImmutableList<TypeElement> uninstallModules = 62 Processors.getAnnotationClassValues( 63 getElementUtils(), 64 Processors.getAnnotationMirror(testElement, ClassNames.UNINSTALL_MODULES), 65 "value"); 66 67 checkModulesHaveInstallIn(testElement, uninstallModules); 68 checkModulesDontOriginateFromTest(testElement, uninstallModules); 69 70 new AggregatedUninstallModulesGenerator(testElement, uninstallModules, getProcessingEnv()) 71 .generate(); 72 } 73 checkModulesHaveInstallIn( TypeElement testElement, ImmutableList<TypeElement> uninstallModules)74 private void checkModulesHaveInstallIn( 75 TypeElement testElement, ImmutableList<TypeElement> uninstallModules) { 76 ImmutableList<TypeElement> invalidModules = 77 uninstallModules.stream() 78 .filter( 79 module -> 80 !(Processors.hasAnnotation(module, ClassNames.MODULE) 81 && Processors.hasAnnotation(module, ClassNames.INSTALL_IN))) 82 .collect(toImmutableList()); 83 84 ProcessorErrors.checkState( 85 invalidModules.isEmpty(), 86 // TODO(b/152801981): Point to the annotation value rather than the annotated element. 87 testElement, 88 "@UninstallModules should only include modules annotated with both @Module and @InstallIn, " 89 + "but found: %s.", 90 invalidModules); 91 } 92 checkModulesDontOriginateFromTest( TypeElement testElement, ImmutableList<TypeElement> uninstallModules)93 private void checkModulesDontOriginateFromTest( 94 TypeElement testElement, ImmutableList<TypeElement> uninstallModules) { 95 ImmutableList<ClassName> invalidModules = 96 uninstallModules.stream() 97 .filter( 98 module -> 99 Processors.getOriginatingTestElement(module, getElementUtils()).isPresent()) 100 .map(ClassName::get) 101 .collect(toImmutableList()); 102 103 ProcessorErrors.checkState( 104 invalidModules.isEmpty(), 105 // TODO(b/152801981): Point to the annotation value rather than the annotated element. 106 testElement, 107 "@UninstallModules should not contain test modules, but found: %s", 108 invalidModules); 109 } 110 } 111