Skip to content

Commit ebf8e9e

Browse files
bcorsoDagger Team
authored andcommitted
Cleanup: Move dagger.android validation into its own method in ModuleValidator.
RELNOTES=N/A PiperOrigin-RevId: 506093477
1 parent 6a1e5bd commit ebf8e9e

1 file changed

Lines changed: 28 additions & 24 deletions

File tree

java/dagger/internal/codegen/validation/ModuleValidator.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import static dagger.internal.codegen.extension.DaggerCollectors.toOptional;
2727
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
2828
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
29+
import static dagger.internal.codegen.validation.ModuleValidator.ModuleMethodKind.ABSTRACT_DECLARATION;
30+
import static dagger.internal.codegen.validation.ModuleValidator.ModuleMethodKind.INSTANCE_BINDING;
2931
import static dagger.internal.codegen.xprocessing.XAnnotations.getClassName;
3032
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
3133
import static dagger.internal.codegen.xprocessing.XElements.hasAnyAnnotation;
@@ -91,8 +93,8 @@ public final class ModuleValidator {
9193
TypeNames.PRODUCTION_SUBCOMPONENT_BUILDER,
9294
TypeNames.PRODUCTION_SUBCOMPONENT_FACTORY);
9395
private static final Optional<Class<?>> ANDROID_PROCESSOR;
94-
private static final String CONTRIBUTES_ANDROID_INJECTOR_NAME =
95-
"dagger.android.ContributesAndroidInjector";
96+
private static final ClassName CONTRIBUTES_ANDROID_INJECTOR_NAME =
97+
ClassName.get("dagger.android", "ContributesAndroidInjector");
9698
private static final String ANDROID_PROCESSOR_NAME = "dagger.android.processor.AndroidProcessor";
9799

98100
static {
@@ -165,38 +167,21 @@ private ValidationReport validate(XTypeElement module, Set<XTypeElement> visited
165167
private ValidationReport validateUncached(XTypeElement module, Set<XTypeElement> visitedModules) {
166168
ValidationReport.Builder builder = ValidationReport.about(module);
167169
ModuleKind moduleKind = ModuleKind.forAnnotatedElement(module).get();
168-
Optional<XType> contributesAndroidInjector =
169-
Optional.ofNullable(processingEnv.findTypeElement(CONTRIBUTES_ANDROID_INJECTOR_NAME))
170-
.map(XTypeElement::getType);
171170
List<XMethodElement> moduleMethods = module.getDeclaredMethods();
172171
List<XMethodElement> bindingMethods = new ArrayList<>();
173172
for (XMethodElement moduleMethod : moduleMethods) {
174173
if (anyBindingMethodValidator.isBindingMethod(moduleMethod)) {
175174
builder.addSubreport(anyBindingMethodValidator.validate(moduleMethod));
176175
bindingMethods.add(moduleMethod);
177176
}
178-
179-
for (XAnnotation annotation : moduleMethod.getAllAnnotations()) {
180-
if (!ANDROID_PROCESSOR.isPresent()
181-
&& contributesAndroidInjector.isPresent()
182-
&& areEquivalentTypes(contributesAndroidInjector.get(), annotation.getType())) {
183-
builder.addSubreport(
184-
ValidationReport.about(moduleMethod)
185-
.addError(
186-
String.format(
187-
"@%s was used, but %s was not found on the processor path",
188-
CONTRIBUTES_ANDROID_INJECTOR_NAME, ANDROID_PROCESSOR_NAME))
189-
.build());
190-
break;
191-
}
192-
}
193177
}
194178

179+
validateDaggerAndroidProcessorRequirements(module, builder);
180+
195181
if (bindingMethods.stream()
196182
.map(ModuleMethodKind::ofMethod)
197183
.collect(toImmutableSet())
198-
.containsAll(
199-
EnumSet.of(ModuleMethodKind.ABSTRACT_DECLARATION, ModuleMethodKind.INSTANCE_BINDING))) {
184+
.containsAll(EnumSet.of(ABSTRACT_DECLARATION, INSTANCE_BINDING))) {
200185
builder.addError(
201186
String.format(
202187
"A @%s may not contain both non-static and abstract binding methods",
@@ -234,6 +219,26 @@ && areEquivalentTypes(contributesAndroidInjector.get(), annotation.getType())) {
234219
return builder.build();
235220
}
236221

222+
private void validateDaggerAndroidProcessorRequirements(
223+
XTypeElement module, ValidationReport.Builder builder) {
224+
if (ANDROID_PROCESSOR.isPresent()
225+
|| processingEnv.findTypeElement(CONTRIBUTES_ANDROID_INJECTOR_NAME) == null) {
226+
return;
227+
}
228+
module.getDeclaredMethods().stream()
229+
.filter(method -> method.hasAnnotation(CONTRIBUTES_ANDROID_INJECTOR_NAME))
230+
.forEach(
231+
method ->
232+
builder.addSubreport(
233+
ValidationReport.about(method)
234+
.addError(
235+
String.format(
236+
"@%s was used, but %s was not found on the processor path",
237+
CONTRIBUTES_ANDROID_INJECTOR_NAME.simpleName(),
238+
ANDROID_PROCESSOR_NAME))
239+
.build()));
240+
}
241+
237242
private void validateReferencedSubcomponents(
238243
XTypeElement subject, ModuleKind moduleKind, ValidationReport.Builder builder) {
239244
XAnnotation moduleAnnotation = moduleKind.getModuleAnnotation(subject);
@@ -467,13 +472,12 @@ private void validateBindingMethodOverrides(
467472
// a binding method in Parent, and "c" because Child is defining a binding method that overrides
468473
// Parent.
469474
XTypeElement currentClass = subject;
470-
XType objectType = processingEnv.findType(TypeName.OBJECT);
471475
// We keep track of visited methods so we don't spam with multiple failures.
472476
Set<XMethodElement> visitedMethods = Sets.newHashSet();
473477
ListMultimap<String, XMethodElement> allMethodsByName =
474478
MultimapBuilder.hashKeys().arrayListValues().build(moduleMethodsByName);
475479

476-
while (!currentClass.getSuperType().isSameType(objectType)) {
480+
while (!currentClass.getSuperType().getTypeName().equals(TypeName.OBJECT)) {
477481
currentClass = currentClass.getSuperType().getTypeElement();
478482
List<XMethodElement> superclassMethods = currentClass.getDeclaredMethods();
479483
for (XMethodElement superclassMethod : superclassMethods) {

0 commit comments

Comments
 (0)