Class ThrowsIfSupport

java.lang.Object
org.apache.groovy.contracts.ThrowsIfSupport

public final class ThrowsIfSupport extends Object
Runtime support for checked @ThrowsIf arm-sets (invoked from generated code; not intended to be called by user code). The checks report a broken contract implementation as a ThrowsIfViolation — never as the arm's declared exception, which is defined behaviour delivered at method entry.
Since:
6.0.0
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    mustThrow(boolean[] held, String[] conditions)
    The must-throw check, run when the method body completes normally: no unwoven arm's condition may have held on entry (a woven arm's guard makes this unreachable for that arm by construction).
    static Throwable
    onlyWhen(Throwable thrown, boolean[] held, Class<?>[] types, String[] conditions, boolean exhaustive)
    The only-when check, run when the method body throws: a throwable matching some arm's exception type must be justified by some matching arm's condition having held on entry.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • onlyWhen

      public static Throwable onlyWhen(Throwable thrown, boolean[] held, Class<?>[] types, String[] conditions, boolean exhaustive)
      The only-when check, run when the method body throws: a throwable matching some arm's exception type must be justified by some matching arm's condition having held on entry. A justified (or unmatched, or non-exhaustive) throwable is returned for rethrow — defined behaviour propagates untouched.

      VirtualMachineErrors (OutOfMemoryError, StackOverflowError, ...) are never judged, whatever the arm types: a VM resource condition is outside contract semantics — no @ThrowsIf claim, however exhaustive, reasons about it — so it always propagates untouched.

      Parameters:
      thrown - the throwable escaping the method body
      held - per-arm: whether the arm's condition held on entry
      types - per-arm: the arm's declared exception type
      conditions - per-arm: the arm's condition text (for the violation message)
      exhaustive - whether the whole arm-set claims exhaustiveness (an iff); one-directional arm-sets are never only-when checked
      Returns:
      thrown, for rethrow, when it is in-contract
      Throws:
      ThrowsIfViolation - when thrown matches an arm's type but no matching arm's condition held
    • mustThrow

      public static void mustThrow(boolean[] held, String[] conditions)
      The must-throw check, run when the method body completes normally: no unwoven arm's condition may have held on entry (a woven arm's guard makes this unreachable for that arm by construction).
      Parameters:
      held - per-unwoven-arm: whether the arm's condition held on entry
      conditions - per-unwoven-arm: the arm's condition text (for the violation message)
      Throws:
      ThrowsIfViolation - when some condition held and the method nevertheless returned