|
26 | 26 | import static dagger.internal.codegen.extension.DaggerCollectors.toOptional; |
27 | 27 | import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList; |
28 | 28 | 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; |
29 | 31 | import static dagger.internal.codegen.xprocessing.XAnnotations.getClassName; |
30 | 32 | import static dagger.internal.codegen.xprocessing.XElements.getSimpleName; |
31 | 33 | import static dagger.internal.codegen.xprocessing.XElements.hasAnyAnnotation; |
@@ -91,8 +93,8 @@ public final class ModuleValidator { |
91 | 93 | TypeNames.PRODUCTION_SUBCOMPONENT_BUILDER, |
92 | 94 | TypeNames.PRODUCTION_SUBCOMPONENT_FACTORY); |
93 | 95 | 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"); |
96 | 98 | private static final String ANDROID_PROCESSOR_NAME = "dagger.android.processor.AndroidProcessor"; |
97 | 99 |
|
98 | 100 | static { |
@@ -165,38 +167,21 @@ private ValidationReport validate(XTypeElement module, Set<XTypeElement> visited |
165 | 167 | private ValidationReport validateUncached(XTypeElement module, Set<XTypeElement> visitedModules) { |
166 | 168 | ValidationReport.Builder builder = ValidationReport.about(module); |
167 | 169 | ModuleKind moduleKind = ModuleKind.forAnnotatedElement(module).get(); |
168 | | - Optional<XType> contributesAndroidInjector = |
169 | | - Optional.ofNullable(processingEnv.findTypeElement(CONTRIBUTES_ANDROID_INJECTOR_NAME)) |
170 | | - .map(XTypeElement::getType); |
171 | 170 | List<XMethodElement> moduleMethods = module.getDeclaredMethods(); |
172 | 171 | List<XMethodElement> bindingMethods = new ArrayList<>(); |
173 | 172 | for (XMethodElement moduleMethod : moduleMethods) { |
174 | 173 | if (anyBindingMethodValidator.isBindingMethod(moduleMethod)) { |
175 | 174 | builder.addSubreport(anyBindingMethodValidator.validate(moduleMethod)); |
176 | 175 | bindingMethods.add(moduleMethod); |
177 | 176 | } |
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 | | - } |
193 | 177 | } |
194 | 178 |
|
| 179 | + validateDaggerAndroidProcessorRequirements(module, builder); |
| 180 | + |
195 | 181 | if (bindingMethods.stream() |
196 | 182 | .map(ModuleMethodKind::ofMethod) |
197 | 183 | .collect(toImmutableSet()) |
198 | | - .containsAll( |
199 | | - EnumSet.of(ModuleMethodKind.ABSTRACT_DECLARATION, ModuleMethodKind.INSTANCE_BINDING))) { |
| 184 | + .containsAll(EnumSet.of(ABSTRACT_DECLARATION, INSTANCE_BINDING))) { |
200 | 185 | builder.addError( |
201 | 186 | String.format( |
202 | 187 | "A @%s may not contain both non-static and abstract binding methods", |
@@ -234,6 +219,26 @@ && areEquivalentTypes(contributesAndroidInjector.get(), annotation.getType())) { |
234 | 219 | return builder.build(); |
235 | 220 | } |
236 | 221 |
|
| 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 | + |
237 | 242 | private void validateReferencedSubcomponents( |
238 | 243 | XTypeElement subject, ModuleKind moduleKind, ValidationReport.Builder builder) { |
239 | 244 | XAnnotation moduleAnnotation = moduleKind.getModuleAnnotation(subject); |
@@ -467,13 +472,12 @@ private void validateBindingMethodOverrides( |
467 | 472 | // a binding method in Parent, and "c" because Child is defining a binding method that overrides |
468 | 473 | // Parent. |
469 | 474 | XTypeElement currentClass = subject; |
470 | | - XType objectType = processingEnv.findType(TypeName.OBJECT); |
471 | 475 | // We keep track of visited methods so we don't spam with multiple failures. |
472 | 476 | Set<XMethodElement> visitedMethods = Sets.newHashSet(); |
473 | 477 | ListMultimap<String, XMethodElement> allMethodsByName = |
474 | 478 | MultimapBuilder.hashKeys().arrayListValues().build(moduleMethodsByName); |
475 | 479 |
|
476 | | - while (!currentClass.getSuperType().isSameType(objectType)) { |
| 480 | + while (!currentClass.getSuperType().getTypeName().equals(TypeName.OBJECT)) { |
477 | 481 | currentClass = currentClass.getSuperType().getTypeElement(); |
478 | 482 | List<XMethodElement> superclassMethods = currentClass.getDeclaredMethods(); |
479 | 483 | for (XMethodElement superclassMethod : superclassMethods) { |
|
0 commit comments