From 2b78c9a133db8a49afd9c0184bbedba5e5fd16df Mon Sep 17 00:00:00 2001 From: Alexander Bashkirov Date: Mon, 4 Oct 2021 18:01:05 +0300 Subject: [PATCH] [util] Added usage example section to Checks javadocs GitOrigin-RevId: 1f9e46b8270fa31752155bcc335f83ee96fbfffc --- .../intellij/openapi/diagnostic/Checks.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/platform/util/src/com/intellij/openapi/diagnostic/Checks.java b/platform/util/src/com/intellij/openapi/diagnostic/Checks.java index 2fbefa6e937f..74d976fe06ee 100644 --- a/platform/util/src/com/intellij/openapi/diagnostic/Checks.java +++ b/platform/util/src/com/intellij/openapi/diagnostic/Checks.java @@ -32,6 +32,50 @@ import java.util.function.Supplier; * and do not throw exception, i.e do not break the control flow. * We suggest using overloads with {@link Attachment} provided, it may significantly reduce error investigation time.

* + *

Usage example: + *

{@code
+ * private @NotNull Result process(int index, int value) {
+ *   // critical, will throw if index is wrong
+ *   Checks.checkIndex(index, myRegistrar.getSize());
+ *   // non-critical, will log and continue
+ *   Checks.requireAndLog(
+ *     myRegistrar.contains(value), () -> buildErrorInfo(value));
+ *
+ *   // ...
+ *   // doing some work
+ *   // ...
+ *   // check and attach some info if it fails
+ *   Checks.checkAndLog(
+ *     MyProcessor.class,
+ *     allInvariantsHoldFor(myRegistrar),
+ *     "Registrar is in inconsistent state",
+ *     AttachmentFactory.createAttachment(myFile));
+ *   // ...
+ *
+ *   // ...
+ *   if (isSomethingWentWrongWithGivenValue(index, value)) {
+ *     Checks.logWarn(MyProcessor.class, "Unexpected situation");
+ *     // doing some fallback scenario
+ *   }
+ *
+ *   // ...
+ *   if      (isVariant1()) { ... }
+ *   else if (isVariant2()) { ... }
+ *   else if (isVariant3()) { ... }
+ *   else {
+ *     // can not tell the compiler that this branch is impossible
+ *     Checks.unreachable();
+ *   }
+ *
+ *   // ...
+ *   // finishing work
+ *
+ *   // checking the result validity
+ *   Checks.ensure(isValid(result));
+ * }
+ * }
+ *

+ * *

NOTE: for performance critical parts of code of for the checks * which should not be enabled under some circumstances it is likely better to use standard Java assertions * via {@code assert statement : message} construction.

@@ -507,6 +551,14 @@ public class Checks { throw new IllegalStateException(message.toString()); } + /** + * @throws IllegalStateException saying that this function call must have been unreachable. + */ + @Contract("-> fail") + public static void unreachable() { + throw new IllegalStateException("Must be unreachable"); + } + /** * Logs {@link IllegalStateException} with the given {@code message} and {@code attachments} as an error. */