Package org.apache.groovy.contracts.generation

# Class BaseGenerator

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

Direct Known Subclasses:
  `ClassInvariantGenerator`, `PostconditionGenerator`, `PreconditionGenerator`

public abstract class BaseGenerator extends Object
Base class for groovy-contracts code generators.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `INVARIANT_CLOSURE_PREFIX`
  Prefix used for synthetic methods that evaluate class invariants.
  `static final String`
  `META_DATA_USE_INLINE_MODE`
  Node metadata flag indicating that assertions can be emitted in inline mode.
  `protected final ReaderSource`
  `source`
  Reader source used by generators that need source-aware AST construction.

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `BaseGenerator(ReaderSource source)`
  Creates a generator bound to the supplied reader source.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected BooleanExpression`
  `addCallsToSuperMethodNodeAnnotationClosure(ClassNode type, MethodNode methodNode, Class<? extends Annotation> annotationType, BooleanExpression booleanExpression, boolean isPostcondition)`
  Merges inherited contract closures into the supplied predicate for the given method.
  `protected static void`
  `ensureBlockBody(MethodNode method)`
  Ensures the given method's body is a `BlockStatement` so that contract code generators can weave assertions into it.
  `protected BlockStatement`
  `getInlineModeBlockStatement(BlockStatement blockStatement)`
  Extracts the inline-mode assertion block from the supplied wrapper block.
  `static String`
  `getInvariantMethodName(ClassNode classNode)`
  `static MethodNode`
  `getInvariantMethodNode(ClassNode classNode)`
  `protected static String`
  `inlineModeKey(Class<? extends Annotation> annotationType)`
  The inline-mode node-metadata key for one contract kind.
  `protected BlockStatement`
  `wrapAssertionBooleanExpression(ClassNode type, MethodNode methodNode, BooleanExpression classInvariantExpression, String assertionType)`
  Wraps a contract predicate with tracking, violation rethrowing, and execution guards.

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

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

- ## Field Details

  - ### INVARIANT_CLOSURE_PREFIX

    public static final String INVARIANT_CLOSURE_PREFIX
    Prefix used for synthetic methods that evaluate class invariants.
    See Also:
      - Constant Field Values

  - ### META_DATA_USE_INLINE_MODE

    public static final String META_DATA_USE_INLINE_MODE
    Node metadata flag indicating that assertions can be emitted in inline mode.
    See Also:
      - Constant Field Values

  - ### source

    protected final ReaderSource source
    Reader source used by generators that need source-aware AST construction.

- ## Constructor Details

  - ### BaseGenerator

    protected BaseGenerator(ReaderSource source)
    Creates a generator bound to the supplied reader source.
    Parameters:
      `source` - the reader source backing the current source unit

- ## Method Details

  - ### inlineModeKey

    protected static String inlineModeKey(Class<? extends Annotation> annotationType)
    The inline-mode node-metadata key for one contract kind. The flag is written while composing inherited contract calls and read when generating that kind's assertion; it MUST be qualified by kind — preconditions are processed before postconditions, and a single shared key let the precondition pass's "no inherited preconditions" verdict leak into the postcondition generator, whose inline path then dropped composed inherited postconditions.
    Parameters:
      `annotationType` - the contract kind's meta annotation (Precondition/Postcondition)
    Returns:
      the per-kind metadata key

  - ### ensureBlockBody

    protected static void ensureBlockBody(MethodNode method)
    Ensures the given method's body is a `BlockStatement` so that contract code generators can weave assertions into it. Earlier transforms (e.g. `@Synchronized`, which runs at CANONICALIZATION) may have replaced the body with a non-block statement such as a `SynchronizedStatement`; in that case the original statement is wrapped in a fresh block. A `null` body (e.g. an abstract method or a not-yet-populated synthetic body) is left untouched. Mirrors the body normalization the precondition path already performs (GROOVY-12066) so the post-condition and invariant generators see a consistent body shape (GROOVY-12084).
    Parameters:
      `method` - the method whose body should be normalized to a block

  - ### getInvariantMethodName

    public static String getInvariantMethodName(ClassNode classNode)
    Parameters:
      `classNode` - the `ClassNode` used to look up the invariant closure field
    Returns:
      the field name of the invariant closure field of the given `classNode`

  - ### getInvariantMethodNode

    public static MethodNode getInvariantMethodNode(ClassNode classNode)
    Parameters:
      `classNode` - the `ClassNode` used to look up the invariant closure field
    Returns:
      the `MethodNode` which contains the invariant of the given `classNode`

  - ### getInlineModeBlockStatement

    protected BlockStatement getInlineModeBlockStatement(BlockStatement blockStatement)
    Extracts the inline-mode assertion block from the supplied wrapper block.
    Parameters:
      `blockStatement` - the block containing generated assertion statements
    Returns:
      an inline-mode block guarded by the contracts-enabled flag

  - ### wrapAssertionBooleanExpression

    protected BlockStatement wrapAssertionBooleanExpression(ClassNode type, MethodNode methodNode, BooleanExpression classInvariantExpression, String assertionType)
    Wraps a contract predicate with tracking, violation rethrowing, and execution guards.
    Parameters:
      `type` - the declaring class
      `methodNode` - the method whose assertion is being emitted
      `classInvariantExpression` - the predicate to evaluate
      `assertionType` - the logical assertion kind
    Returns:
      the generated wrapper block

  - ### addCallsToSuperMethodNodeAnnotationClosure

    protected BooleanExpression addCallsToSuperMethodNodeAnnotationClosure(ClassNode type, MethodNode methodNode, Class<? extends Annotation> annotationType, BooleanExpression booleanExpression, boolean isPostcondition)
    Merges inherited contract closures into the supplied predicate for the given method.
    Parameters:
      `type` - the declaring class
      `methodNode` - the method whose inherited contracts should be consulted
      `annotationType` - the contract meta-annotation to search for
      `booleanExpression` - the predicate built from the current declaration
      `isPostcondition` - whether postcondition calling conventions should be used
    Returns:
      the predicate augmented with inherited contract closures