Package org.apache.groovy.contracts.generation

# Class OldVariableGenerationUtility

java.lang.Object
org.apache.groovy.contracts.generation.OldVariableGenerationUtility

public class OldVariableGenerationUtility extends Object

Central place where code generation for the `old` closure variable takes place.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `OLD_VARIABLES_METHOD`
  Synthetic helper method name used to compute the `old` variable map for postconditions.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `OldVariableGenerationUtility()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `addOldVariableMethodNode(ClassNode classNode)`
  Creates a synthetic method handling generation of the `old` variable map.
  `static Expression`
  `snapshotExpression(ClassNode type, Expression value)`
  Returns an expression that snapshots `value` (of declared type `type`) for storage in the `old` map.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### OLD_VARIABLES_METHOD

    public static final String OLD_VARIABLES_METHOD
    Synthetic helper method name used to compute the `old` variable map for postconditions.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### OldVariableGenerationUtility

    public OldVariableGenerationUtility()

- ## Method Details

  - ### addOldVariableMethodNode

    public static void addOldVariableMethodNode(ClassNode classNode)
    Creates a synthetic method handling generation of the `old` variable map. If a super class declares the same synthetic method it will be called and the results will be merged.
    Parameters:
      `classNode` - which contains postconditions, so an old variable generating method makes sense here.

  - ### snapshotExpression

    public static Expression snapshotExpression(ClassNode type, Expression value)
    Returns an expression that snapshots `value` (of declared type `type`) for storage in the `old` map. The snapshot is only a defensive copy when one is needed and possible:
    - a value of a known-immutable type (per `ImmutablePropertyUtils`: primitives/wrappers, `String`, `BigInteger`/`BigDecimal`, the `java.time.*` types, enums, `@Immutable`/`@KnownImmutable` types, ...) is stored by reference, since it cannot change;
    - a mutable `Cloneable` value is defensively copied with a null-safe `clone()`, so an in-place change in the method body is not also seen through `old`;
    - any other value (e.g. a non-cloneable collection or a user type) cannot be generically copied, so its reference is stored as a best effort.
    Reuses the same immutability/clone detection as `@Immutable` rather than an ad-hoc list.
    Parameters:
      `type` - the declared type of the value being snapshotted
      `value` - the expression producing the value to snapshot
    Returns:
      the snapshot expression