diff options
author | Brad Corso <bcorso@google.com> | 2021-04-19 14:43:35 -0700 |
---|---|---|
committer | Dagger Team <dagger-dev+copybara@google.com> | 2021-04-19 14:45:02 -0700 |
commit | fa49bdf2defb8ea6bdf3395491315125f89f0d1c (patch) | |
tree | bb801988351542b269d0bffd7e1bed4f8ca25200 | |
parent | e0f510a679f37e06d272ab919596149a50219975 (diff) | |
download | dagger2-fa49bdf2defb8ea6bdf3395491315125f89f0d1c.tar.gz |
Allow ProcessorErrors.checkState without an associated element
RELNOTES=N/A
PiperOrigin-RevId: 369306095
3 files changed, 51 insertions, 0 deletions
diff --git a/java/dagger/hilt/processor/internal/BadInputException.java b/java/dagger/hilt/processor/internal/BadInputException.java index d9617688e..f57a34a5e 100644 --- a/java/dagger/hilt/processor/internal/BadInputException.java +++ b/java/dagger/hilt/processor/internal/BadInputException.java @@ -36,6 +36,11 @@ public final class BadInputException extends RuntimeException { this.badElements = ImmutableList.copyOf(badElements); } + public BadInputException(String message) { + super(message); + this.badElements = ImmutableList.of(); + } + public ImmutableList<Element> getBadElements() { return badElements; } diff --git a/java/dagger/hilt/processor/internal/ProcessorErrorHandler.java b/java/dagger/hilt/processor/internal/ProcessorErrorHandler.java index 460e8a946..2ecc0f098 100644 --- a/java/dagger/hilt/processor/internal/ProcessorErrorHandler.java +++ b/java/dagger/hilt/processor/internal/ProcessorErrorHandler.java @@ -62,6 +62,9 @@ final class ProcessorErrorHandler { if (t instanceof BadInputException) { BadInputException badInput = (BadInputException) t; + if (badInput.getBadElements().isEmpty()) { + hiltErrors.add(HiltError.of(badInput.getMessage())); + } for (Element element : badInput.getBadElements()) { hiltErrors.add(HiltError.of(badInput.getMessage(), element)); } diff --git a/java/dagger/hilt/processor/internal/ProcessorErrors.java b/java/dagger/hilt/processor/internal/ProcessorErrors.java index b1578daa3..d002292fd 100644 --- a/java/dagger/hilt/processor/internal/ProcessorErrors.java +++ b/java/dagger/hilt/processor/internal/ProcessorErrors.java @@ -31,6 +31,49 @@ import javax.lang.model.type.TypeMirror; /** Static helper methods for throwing errors during code generation. */ public final class ProcessorErrors { + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + * @param expression a boolean expression + * @param errorMessage the exception message to use if the check fails; will be converted to a + * string using {@link String#valueOf(Object)} + * @throws BadInputException if {@code expression} is false + */ + public static void checkState( + boolean expression, + @Nullable Object errorMessage) { + if (!expression) { + throw new BadInputException(String.valueOf(errorMessage)); + } + } + + /** + * Ensures the truth of an expression involving the state of the calling instance, but not + * involving any parameters to the calling method. + * + * @param expression a boolean expression + * @param errorMessageTemplate a template for the exception message should the check fail. The + * message is formed by replacing each {@code %s} placeholder in the template with an + * argument. These are matched by position - the first {@code %s} gets {@code + * errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in + * square braces. Unmatched placeholders will be left as-is. + * @param errorMessageArgs the arguments to be substituted into the message template. Arguments + * are converted to strings using {@link String#valueOf(Object)}. + * @throws BadInputException if {@code expression} is false + * @throws NullPointerException if the check fails and either {@code errorMessageTemplate} or + * {@code errorMessageArgs} is null (don't let this happen) + */ + @FormatMethod + public static void checkState( + boolean expression, + @Nullable @FormatString String errorMessageTemplate, + @Nullable Object... errorMessageArgs) { + if (!expression) { + throw new BadInputException(String.format(errorMessageTemplate, errorMessageArgs)); + } + } + /** * Ensures the truth of an expression involving the state of the calling instance, but not * involving any parameters to the calling method. |