Skip to content

Commit

Permalink
Migrate more tests to XProcessing testing APIs.
Browse files Browse the repository at this point in the history
RELNOTES=N/A
PiperOrigin-RevId: 474341790
  • Loading branch information
bcorso authored and Dagger Team committed Sep 14, 2022
1 parent 96e2b8a commit a15f8df
Show file tree
Hide file tree
Showing 4 changed files with 368 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
import static com.google.common.collect.Maps.uniqueIndex;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import dagger.multibindings.IntoSet;
import java.util.Set;

/**
* Binds each {@link BindingMethodValidator} into a map, keyed by {@link
Expand All @@ -34,27 +32,18 @@
public interface BindingMethodValidatorsModule {
@Provides
static ImmutableMap<ClassName, BindingMethodValidator> indexValidators(
Set<BindingMethodValidator> validators) {
return uniqueIndex(validators, BindingMethodValidator::methodAnnotation);
ProvidesMethodValidator providesMethodValidator,
ProducesMethodValidator producesMethodValidator,
BindsMethodValidator bindsMethodValidator,
MultibindsMethodValidator multibindsMethodValidator,
BindsOptionalOfMethodValidator bindsOptionalOfMethodValidator) {
return uniqueIndex(
ImmutableSet.of(
providesMethodValidator,
producesMethodValidator,
bindsMethodValidator,
multibindsMethodValidator,
bindsOptionalOfMethodValidator),
BindingMethodValidator::methodAnnotation);
}

@Binds
@IntoSet
BindingMethodValidator provides(ProvidesMethodValidator validator);

@Binds
@IntoSet
BindingMethodValidator produces(ProducesMethodValidator validator);

@Binds
@IntoSet
BindingMethodValidator binds(BindsMethodValidator validator);

@Binds
@IntoSet
BindingMethodValidator multibinds(MultibindsMethodValidator validator);

@Binds
@IntoSet
BindingMethodValidator bindsOptionalOf(BindsOptionalOfMethodValidator validator);
}
32 changes: 18 additions & 14 deletions java/dagger/internal/codegen/validation/ComponentValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,13 @@ private ComponentAnnotation componentAnnotation() {

ValidationReport validateElement() {
if (componentKinds.size() > 1) {
return moreThanOneComponentAnnotation();
return report.addError(moreThanOneComponentAnnotationError(), component).build();
}
if (!(component.isInterface() || component.isClass())) {
// If the annotated element is not a class or interface skip the rest of the checks since
// the remaining checks will likely just output unhelpful noise in such cases.
return report.addError(invalidTypeError(), component).build();
}

validateUseOfCancellationPolicy();
validateIsAbstractType();
validateCreators();
Expand All @@ -177,12 +181,10 @@ ValidationReport validateElement() {
return report.build();
}

private ValidationReport moreThanOneComponentAnnotation() {
String error =
"Components may not be annotated with more than one component annotation: found "
+ annotationsFor(componentKinds);
report.addError(error, component);
return report.build();
private String moreThanOneComponentAnnotationError() {
return String.format(
"Components may not be annotated with more than one component annotation: found %s",
annotationsFor(componentKinds));
}

private void validateUseOfCancellationPolicy() {
Expand All @@ -194,15 +196,17 @@ private void validateUseOfCancellationPolicy() {
}

private void validateIsAbstractType() {
if (!component.isInterface() && !(component.isClass() && component.isAbstract())) {
report.addError(
String.format(
"@%s may only be applied to an interface or abstract class",
componentKind().annotation().simpleName()),
component);
if (!component.isAbstract()) {
report.addError(invalidTypeError(), component);
}
}

private String invalidTypeError() {
return String.format(
"@%s may only be applied to an interface or abstract class",
componentKind().annotation().simpleName());
}

private void validateCreators() {
ImmutableSet<XTypeElement> creators =
enclosedAnnotatedTypes(component, creatorAnnotationsFor(componentAnnotation()));
Expand Down

0 comments on commit a15f8df

Please sign in to comment.