Package org.apache.groovy.contracts
Class VariantSupport
java.lang.Object
org.apache.groovy.contracts.VariantSupport
Shared runtime support for
Decreases termination
measures, used by both the loop variant (@Decreases on a loop) and the
recursion variant (@Decreases on a method, via MethodVariantSupport).
It is the single source of truth for what "strictly decreases and stays
well-founded" means — a scalar Comparable (with a non-negative floor for
Numbers) or a List compared lexicographically — and for whether
the check runs at all under the -ea/-da configuration.
The comparison itself is violation-agnostic (describeFailure(java.lang.Object, java.lang.Object) returns a
description rather than throwing) so each caller can raise the appropriate
violation type.
- Since:
- 6.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckLoopVariant(Object prev, Object curr, String className) Loop-variant entry point invoked from generated loop-body code: if enabled, verifycurrstrictly decreased fromprevand stayed well-founded, throwingLoopVariantViolationotherwise.static voidcheckLoopVariantEntry(Object curr, String className) Loop-variant entry point for the first iteration of a loop, where no previous value exists to compare against: if enabled, verify a scalarNumbermeasure is non-negative at loop entry (GROOVY-12128), throwingLoopVariantViolationotherwise.static StringdescribeFailure(Object prev, Object curr) Describe howcurrfails to be a valid strict, well-founded decrease fromprev, ornullif it is valid.static booleanWhether termination-measure checks are enabled forclassNameunder the current-ea/-daconfiguration (anullclass name uses the global default).
-
Method Details
-
enabled
Whether termination-measure checks are enabled forclassNameunder the current-ea/-daconfiguration (anullclass name uses the global default). Mirrors how@Requires/@Ensuresgate.- Parameters:
className- the enclosing class name, ornullfor the global default- Returns:
trueif the check should run
-
describeFailure
Describe howcurrfails to be a valid strict, well-founded decrease fromprev, ornullif it is valid.Listmeasures are compared lexicographically.- Parameters:
prev- the measure beforecurr- the measure after- Returns:
- a failure description suffix, or
nullif the decrease is valid
-
checkLoopVariant
Loop-variant entry point invoked from generated loop-body code: if enabled, verifycurrstrictly decreased fromprevand stayed well-founded, throwingLoopVariantViolationotherwise.- Parameters:
prev- the variant before the iterationcurr- the variant after the iterationclassName- the enclosing class name (for-ea/-dagating)
-
checkLoopVariantEntry
Loop-variant entry point for the first iteration of a loop, where no previous value exists to compare against: if enabled, verify a scalarNumbermeasure is non-negative at loop entry (GROOVY-12128), throwingLoopVariantViolationotherwise. This mirrors the non-negative floordescribeFailure(java.lang.Object, java.lang.Object)applies to every subsequent iteration-start value.List(lexicographic) measures have no single deciding component at entry, so they are not floor-checked here.- Parameters:
curr- the variant at first loop entryclassName- the enclosing class name (for-ea/-dagating)
-