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.android.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 dagger.hilt.processor.internal.BaseProcessor;
27 import dagger.hilt.processor.internal.ClassNames;
28 import dagger.hilt.processor.internal.ProcessorErrors;
29 import dagger.hilt.processor.internal.Processors;
30 import java.util.Set;
31 import javax.annotation.processing.Processor;
32 import javax.lang.model.element.Element;
33 import javax.lang.model.element.TypeElement;
34 import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;
35 
36 /** Validates {@link dagger.hilt.android.testing.UninstallModules} usages. */
37 @IncrementalAnnotationProcessor(ISOLATING)
38 @AutoService(Processor.class)
39 public final class UninstallModulesProcessor extends BaseProcessor {
40 
41   @Override
getSupportedAnnotationTypes()42   public Set<String> getSupportedAnnotationTypes() {
43     return ImmutableSet.of(ClassNames.IGNORE_MODULES.toString());
44   }
45 
46   @Override
processEach(TypeElement annotation, Element element)47   public void processEach(TypeElement annotation, Element element) throws Exception {
48     // TODO(bcorso): Consider using RootType to check this?
49     // TODO(bcorso): Loosen this restriction to allow defining sets of ignored modules in libraries.
50     ProcessorErrors.checkState(
51         MoreElements.isType(element)
52             && Processors.hasAnnotation(element, ClassNames.HILT_ANDROID_TEST),
53         element,
54         "@%s should only be used on test classes annotated with @%s, but found: %s",
55         annotation.getSimpleName(),
56         ClassNames.HILT_ANDROID_TEST.simpleName(),
57         element);
58 
59     ImmutableList<TypeElement> invalidModules =
60         Processors.getAnnotationClassValues(
61                 getElementUtils(),
62                 Processors.getAnnotationMirror(element, ClassNames.IGNORE_MODULES),
63                 "value")
64             .stream()
65             .filter(
66                 module ->
67                     !(Processors.hasAnnotation(module, ClassNames.MODULE)
68                         && Processors.hasAnnotation(module, ClassNames.INSTALL_IN)))
69             .collect(toImmutableList());
70 
71     ProcessorErrors.checkState(
72         invalidModules.isEmpty(),
73         // TODO(b/152801981): Point to the annotation value rather than the annotated element.
74         element,
75         "@%s should only include modules annotated with both @Module and @InstallIn, but found: "
76           + "%s.",
77         annotation.getSimpleName(),
78         invalidModules);
79   }
80 }
81