Package: org.apache.groovy.contracts.generation

## [Java] Class OldVariableGenerationUtility

- 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
    Modifiers | Name | Description
    --- | --- | ---
    `static String` | `OLD_VARIABLES_METHOD` | Synthetic helper method name used to compute the `old` variable map for postconditions.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public static void` | `addOldVariableMethodNode(ClassNode classNode)` Creates a synthetic method handling generation of the `old` variable map.
    `` | `public static Expression` | `snapshotExpression(ClassNode type, Expression value)` Returns an expression that snapshots `value` (of declared type `type`) for storage in the `old` map.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### public static final String OLD_VARIABLES_METHOD

      Synthetic helper method name used to compute the `old` variable map for postconditions.

  - ### Method Detail

    - #### 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.

    - #### 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