Class GeneralUtils

java.lang.Object
org.codehaus.groovy.ast.tools.GeneralUtils

public class GeneralUtils extends Object
Handy methods when working with the Groovy AST. Provides factory methods for creating common AST nodes and utility methods for expression and statement construction.

This utility class offers shorthand methods (often with abbreviated names) for building AST structures:

  • Statement constructors: block(), expr(), assign(), ret(), etc.
  • Expression constructors: var(), constX(), classX(), cast(), etc.
  • Operator expressions: binX(), andX(), orX(), cmp(), etc.
  • Collection literals: list(), map(), tuple(), array(), etc.
  • Method calls: call(), invokeMethod(), staticCall(), etc.

Common patterns use abbreviated names like X suffix for expression factories (e.g., varX() for VariableExpression) and S suffix for statement factories (e.g., blockS() for BlockStatement).

Null Handling: Most methods handle null gracefully, often returning empty or no-op structures rather than throwing exceptions.

See Also:
  • Field Details

    • ASSIGN

      public static final Token ASSIGN
      AST assignment operator token ("=").
    • CMP

      public static final Token CMP
      AST compare-to operator token ("<=>").
    • EQ

      public static final Token EQ
      AST equality operator token ("==").
    • NE

      public static final Token NE
      AST inequality operator token ("!=").
    • NOT_IDENTICAL

      public static final Token NOT_IDENTICAL
      AST non-identity operator token ("!==").
    • GE

      public static final Token GE
      AST greater-than-or-equal operator token (">=").
    • GT

      public static final Token GT
      AST greater-than operator token (">").
    • LE

      public static final Token LE
      AST less-than-or-equal operator token ("<=").
    • LT

      public static final Token LT
      AST less-than operator token ("<").
    • AND

      public static final Token AND
      AST logical AND operator token ("&&").
    • OR

      public static final Token OR
      AST logical OR operator token ("||").
    • INSTANCEOF

      public static final Token INSTANCEOF
      AST instanceof keyword token.
    • MINUS

      public static final Token MINUS
      AST subtraction operator token ("-").
    • PLUS

      public static final Token PLUS
      AST addition operator token ("+").
  • Constructor Details

    • GeneralUtils

      public GeneralUtils()
  • Method Details

    • andX

      public static BinaryExpression andX(Expression lhv, Expression rhv)
      Creates a binary expression with the AND operator joining two expressions.
      Parameters:
      lhv - the left-hand-side operand
      rhv - the right-hand-side operand
      Returns:
      a BinaryExpression with AND operator
      See Also:
    • args

      public static ArgumentListExpression args(Expression... expressions)
      Creates an ArgumentListExpression from individual expression arguments.
      Parameters:
      expressions - the argument expressions (may be empty)
      Returns:
      an ArgumentListExpression containing the arguments
    • args

      public static ArgumentListExpression args(List<Expression> expressions)
      Creates an ArgumentListExpression from a list of expressions.
      Parameters:
      expressions - the list of argument expressions (may be empty)
      Returns:
      an ArgumentListExpression containing the arguments
    • args

      public static ArgumentListExpression args(Parameter... parameters)
      Creates an ArgumentListExpression from parameter nodes, extracting their names as variable references.
      Parameters:
      parameters - the parameter nodes
      Returns:
      an ArgumentListExpression with variable expressions for each parameter
    • args

      public static ArgumentListExpression args(String... names)
      Creates an ArgumentListExpression from variable names, converting each to a VariableExpression.
      Parameters:
      names - the variable names (non-null strings)
      Returns:
      an ArgumentListExpression with variable expressions for each name
    • arrayX

      public static ArrayExpression arrayX(ClassNode elementType, List<Expression> initExpressions)
      Creates an ArrayExpression for the given element type and initial value expressions.
      Parameters:
      elementType - the ClassNode representing the array element type
      initExpressions - the expressions providing initial array values
      Returns:
      an ArrayExpression with the given element type and values
    • arrayX

      public static ArrayExpression arrayX(ClassNode elementType, List<Expression> initExpressions, List<Expression> sizeExpressions)
      Creates an ArrayExpression for the given element type, dimensions, and initial values.
      Parameters:
      elementType - the ClassNode representing the array element type
      initExpressions - the expressions providing initial array values
      sizeExpressions - the expressions providing array dimensions
      Returns:
      an ArrayExpression with the given element type, dimensions, and values
    • asX

      public static CastExpression asX(ClassNode type, Expression expression)
      Creates a CastExpression converting the given expression to the specified type.
      Parameters:
      type - the ClassNode to cast to
      expression - the expression to cast
      Returns:
      a CastExpression with the given type and expression
    • assignNullS

      public static Statement assignNullS(Expression target)
      Creates a statement that assigns a null/empty value to the target expression. Used to clear or reset variable values.
      Parameters:
      target - the expression to assign to
      Returns:
      an ExpressionStatement assigning null to the target
    • assignS

      public static Statement assignS(Expression target, Expression value)
      Creates an assignment statement for the supplied target and value.
      Parameters:
      target - the assignment target expression
      value - the value expression
      Returns:
      the resulting Statement
    • assignX

      public static Expression assignX(Expression target, Expression value)
      Creates an assignment expression using ASSIGN.
      Parameters:
      target - the assignment target expression
      value - the value expression
      Returns:
      the resulting Expression
    • attrX

      public static AttributeExpression attrX(Expression owner, String attribute)
      Since:
      5.0.0
    • attrX

      public static AttributeExpression attrX(Expression owner, Expression attribute)
      Creates an attribute access expression.
      Parameters:
      owner - the owner expression
      attribute - the attribute expression
      Returns:
      the resulting AttributeExpression
    • attrX$$bridge

      @Deprecated public static Expression attrX$$bridge(Expression owner, Expression attribute)
      Deprecated.
      Deprecated compatibility bridge for attrX(Expression, Expression).
      Parameters:
      owner - the owner expression
      attribute - the attribute expression
      Returns:
      the resulting Expression
    • binX

      public static BinaryExpression binX(Expression left, Token token, Expression right)
      Creates a binary expression from operands and an operator token.
      Parameters:
      left - the left-hand operand
      token - the operator token
      right - the right-hand operand
      Returns:
      the resulting BinaryExpression
    • block

      public static BlockStatement block(VariableScope scope, Statement... stmts)
      Creates a block statement from statements and optional scope.
      Parameters:
      scope - the variable scope for the block
      stmts - the statements to include in the block
      Returns:
      the resulting BlockStatement
    • block

      public static BlockStatement block(VariableScope scope, List<Statement> stmts)
      Creates a block statement from statements and optional scope.
      Parameters:
      scope - the variable scope for the block
      stmts - the statements to include in the block
      Returns:
      the resulting BlockStatement
    • block

      public static BlockStatement block(Statement... stmts)
      Creates a block statement from statements and optional scope.
      Parameters:
      stmts - the statements to include in the block
      Returns:
      the resulting BlockStatement
    • boolX

      public static BooleanExpression boolX(Expression expr)
      Wraps an expression in a BooleanExpression.
      Parameters:
      expr - the expression
      Returns:
      the resulting BooleanExpression
    • bytecodeX

      public static BytecodeExpression bytecodeX(Consumer<org.objectweb.asm.MethodVisitor> writer)
      Creates a custom BytecodeExpression.
      Parameters:
      writer - the bytecode writer callback
      Returns:
      the resulting BytecodeExpression
    • bytecodeX

      public static BytecodeExpression bytecodeX(ClassNode type, Consumer<org.objectweb.asm.MethodVisitor> writer)
      Creates a custom BytecodeExpression.
      Parameters:
      type - the target type
      writer - the bytecode writer callback
      Returns:
      the resulting BytecodeExpression
    • callSuperX

      public static MethodCallExpression callSuperX(String methodName)
      Creates a method call expression on super.
      Parameters:
      methodName - the method name
      Returns:
      the resulting MethodCallExpression
    • callSuperX

      public static MethodCallExpression callSuperX(String methodName, Expression args)
      Creates a method call expression on super.
      Parameters:
      methodName - the method name
      args - the argument expression(s)
      Returns:
      the resulting MethodCallExpression
    • callThisX

      public static MethodCallExpression callThisX(String methodName)
      Creates a method call expression on this.
      Parameters:
      methodName - the method name
      Returns:
      the resulting MethodCallExpression
    • callThisX

      public static MethodCallExpression callThisX(String methodName, Expression args)
      Creates a method call expression on this.
      Parameters:
      methodName - the method name
      args - the argument expression(s)
      Returns:
      the resulting MethodCallExpression
    • callX

      public static MethodCallExpression callX(Expression receiver, String methodName)
      Creates a method call expression.
      Parameters:
      receiver - the call receiver
      methodName - the method name
      Returns:
      the resulting MethodCallExpression
    • callX

      public static MethodCallExpression callX(Expression receiver, String methodName, Expression args)
      Creates a method call expression.
      Parameters:
      receiver - the call receiver
      methodName - the method name
      args - the argument expression(s)
      Returns:
      the resulting MethodCallExpression
    • callX

      public static MethodCallExpression callX(Expression receiver, Expression method, Expression args)
      Creates a method call expression.
      Parameters:
      receiver - the call receiver
      method - the method expression
      args - the argument expression(s)
      Returns:
      the resulting MethodCallExpression
    • callX

      public static StaticMethodCallExpression callX(ClassNode receiver, String methodName)
      Creates a method call expression.
      Parameters:
      receiver - the call receiver
      methodName - the method name
      Returns:
      the resulting StaticMethodCallExpression
    • callX

      public static StaticMethodCallExpression callX(ClassNode receiver, String methodName, Expression args)
      Creates a method call expression.
      Parameters:
      receiver - the call receiver
      methodName - the method name
      args - the argument expression(s)
      Returns:
      the resulting StaticMethodCallExpression
    • caseS

      public static CaseStatement caseS(Expression expression, Statement code)
      Creates a switch-case statement.
      Parameters:
      expression - the expression
      code - the statement body
      Returns:
      the resulting CaseStatement
    • castX

      public static CastExpression castX(ClassNode type, Expression expression)
      Creates a cast expression.
      Parameters:
      type - the target type
      expression - the expression
      Returns:
      the resulting CastExpression
    • castX

      public static CastExpression castX(ClassNode type, Expression expression, boolean ignoreAutoboxing)
      Creates a cast expression.
      Parameters:
      type - the target type
      expression - the expression
      ignoreAutoboxing - whether autoboxing should be ignored
      Returns:
      the resulting CastExpression
    • catchS

      public static CatchStatement catchS(Parameter variable, Statement code)
      Creates a catch statement.
      Parameters:
      variable - the loop or catch parameter
      code - the statement body
      Returns:
      the resulting CatchStatement
    • classX

      public static ClassExpression classX(ClassNode clazz)
      Creates a class literal expression.
      Parameters:
      clazz - the class reference
      Returns:
      the resulting ClassExpression
    • classX

      public static ClassExpression classX(Class<?> clazz)
      Creates a class literal expression.
      Parameters:
      clazz - the class reference
      Returns:
      the resulting ClassExpression
    • closureX

      public static ClosureExpression closureX(Parameter[] params, Statement code)
      Creates a closure expression.
      Parameters:
      params - the parameter array
      code - the statement body
      Returns:
      the resulting ClosureExpression
    • closureX

      public static ClosureExpression closureX(Statement code)
      Creates a closure expression.
      Parameters:
      code - the statement body
      Returns:
      the resulting ClosureExpression
    • lambdaX

      public static LambdaExpression lambdaX(Parameter[] params, Statement code)
      Builds a lambda expression
      Parameters:
      params - lambda parameters
      code - lambda code
      Returns:
      the lambda expression
    • lambdaX

      public static LambdaExpression lambdaX(Statement code)
      Builds a lambda expression with no parameters
      Parameters:
      code - lambda code
      Returns:
      the lambda expression
    • cmpX

      public static BinaryExpression cmpX(Expression lhv, Expression rhv)
      Builds a binary expression that compares two values.
      Parameters:
      lhv - expression for the value to compare from
      rhv - expression for the value to compare to
      Returns:
      the expression comparing two values
    • constX

      public static ConstantExpression constX(Object val)
      Creates a constant expression.
      Parameters:
      val - the val
      Returns:
      the resulting ConstantExpression
    • constX

      public static ConstantExpression constX(Object val, boolean keepPrimitive)
      Creates a constant expression.
      Parameters:
      val - the val
      keepPrimitive - whether primitive-wrapper constants should be preserved
      Returns:
      the resulting ConstantExpression
    • ctorX

      public static ConstructorCallExpression ctorX(ClassNode type, Expression args)
      Creates a constructor call expression.
      Parameters:
      type - the target type
      args - the argument expression(s)
      Returns:
      the resulting ConstructorCallExpression
    • ctorX

      public static ConstructorCallExpression ctorX(ClassNode type)
      Creates a constructor call expression.
      Parameters:
      type - the target type
      Returns:
      the resulting ConstructorCallExpression
    • ctorSuperS

      public static Statement ctorSuperS(Expression args)
      Creates a super(...) constructor call statement.
      Parameters:
      args - the argument expression(s)
      Returns:
      the resulting Statement
    • ctorSuperX

      public static ConstructorCallExpression ctorSuperX(Expression args)
      Creates a super(...) constructor call expression.
      Parameters:
      args - the argument expression(s)
      Returns:
      the resulting ConstructorCallExpression
    • ctorThisS

      public static Statement ctorThisS(Expression args)
      Creates a this(...) constructor call statement.
      Parameters:
      args - the argument expression(s)
      Returns:
      the resulting Statement
    • ctorThisX

      public static ConstructorCallExpression ctorThisX(Expression args)
      Creates a this(...) constructor call expression.
      Parameters:
      args - the argument expression(s)
      Returns:
      the resulting ConstructorCallExpression
    • ctorSuperS

      public static Statement ctorSuperS()
      Creates a super(...) constructor call statement.
      Returns:
      the resulting Statement
    • ctorSuperX

      public static ConstructorCallExpression ctorSuperX()
      Creates a super(...) constructor call expression.
      Returns:
      the resulting ConstructorCallExpression
    • ctorThisS

      public static Statement ctorThisS()
      Creates a this(...) constructor call statement.
      Returns:
      the resulting Statement
    • ctorThisX

      public static ConstructorCallExpression ctorThisX()
      Creates a this(...) constructor call expression.
      Returns:
      the resulting ConstructorCallExpression
    • declS

      public static Statement declS(Expression target, Expression init)
      Creates a variable declaration statement.
      Parameters:
      target - the assignment target expression
      init - the initializer expression
      Returns:
      the resulting Statement
    • declX

      public static DeclarationExpression declX(Expression target, Expression init)
      Creates a variable declaration expression.
      Parameters:
      target - the assignment target expression
      init - the initializer expression
      Returns:
      the resulting DeclarationExpression
    • defaultValueX

      public static ConstantExpression defaultValueX(ClassNode type)
      Returns a constant expression with the default value for the given type (i.e., false for boolean, 0 for numbers or null).
      Since:
      4.0.0
    • elvisX

      public static ElvisOperatorExpression elvisX(Expression base, Expression otherwise)
      Creates an Elvis operator expression.
      Parameters:
      base - the base expression
      otherwise - the fallback expression
      Returns:
      the resulting ElvisOperatorExpression
    • entryX

      public static MapEntryExpression entryX(Expression key, Expression value)
      Creates a map entry expression.
      Parameters:
      key - the key expression
      value - the value expression
      Returns:
      the resulting MapEntryExpression
    • eqX

      public static BinaryExpression eqX(Expression left, Expression right)
      Creates an equality comparison expression.
      Parameters:
      left - the left-hand operand
      right - the right-hand operand
      Returns:
      the resulting BinaryExpression
    • equalsNullX

      public static BooleanExpression equalsNullX(Expression expr)
      Creates a null-equality test expression.
      Parameters:
      expr - the expression
      Returns:
      the resulting BooleanExpression
    • fieldX

      public static FieldExpression fieldX(FieldNode fieldNode)
      Creates a field expression.
      Parameters:
      fieldNode - the fieldNode
      Returns:
      the resulting FieldExpression
    • fieldX

      public static FieldExpression fieldX(ClassNode owner, String fieldName)
      Creates a field expression.
      Parameters:
      owner - the owner expression
      fieldName - the field name
      Returns:
      the resulting FieldExpression
    • findArg

      public static Expression findArg(String argName)
      Creates an expression that resolves an entry from script args.
      Parameters:
      argName - the argument name
      Returns:
      the resulting Expression
    • forS

      public static ForStatement forS(Parameter variable, Expression collectionExpression, Statement loopS)
      Creates a for-loop statement.
      Parameters:
      variable - the loop or catch parameter
      collectionExpression - the collection expression
      loopS - the loop body statement
      Returns:
      the resulting ForStatement
    • getAllMethods

      public static List<MethodNode> getAllMethods(ClassNode type)
      Collects methods declared on a class and its super classes.
      Parameters:
      type - the target type
      Returns:
      the resulting List
    • getAllProperties

      public static List<PropertyNode> getAllProperties(ClassNode type)
      Collects properties according to the supplied inclusion flags.
      Parameters:
      type - the target type
      Returns:
      the resulting List
    • getInstanceNonPropertyFields

      public static List<FieldNode> getInstanceNonPropertyFields(ClassNode cNode)
      Collects instance fields that are not exposed as properties.
      Parameters:
      cNode - the class node
      Returns:
      the resulting List
    • getInstanceNonPropertyFieldNames

      public static List<String> getInstanceNonPropertyFieldNames(ClassNode cNode)
      Collects names of instance fields that are not exposed as properties.
      Parameters:
      cNode - the class node
      Returns:
      the resulting List
    • getInstanceProperties

      public static List<PropertyNode> getInstanceProperties(ClassNode cNode)
      Collects instance (non-static) properties.
      Parameters:
      cNode - the class node
      Returns:
      the resulting List
    • getInstancePropertyNames

      public static List<String> getInstancePropertyNames(ClassNode cNode)
      Collects names of instance properties.
      Parameters:
      cNode - the class node
      Returns:
      the resulting List
    • getInstancePropertyFields

      public static List<FieldNode> getInstancePropertyFields(ClassNode cNode)
      Collects backing fields for instance properties.
      Parameters:
      cNode - the class node
      Returns:
      the resulting List
    • getInterfacesAndSuperInterfaces

      public static Set<ClassNode> getInterfacesAndSuperInterfaces(ClassNode cNode)
      Collects interfaces implemented directly or transitively.
      Parameters:
      cNode - the class node
      Returns:
      the resulting Set
    • getSuperNonPropertyFields

      public static List<FieldNode> getSuperNonPropertyFields(ClassNode cNode)
      Collects inherited instance fields that are not properties.
      Parameters:
      cNode - the class node
      Returns:
      the resulting List
    • getSuperPropertyFields

      public static List<FieldNode> getSuperPropertyFields(ClassNode cNode)
      Collects inherited property backing fields.
      Parameters:
      cNode - the class node
      Returns:
      the resulting List
    • getAllProperties

      public static List<PropertyNode> getAllProperties(Set<String> names, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly)
      Collects properties according to the supplied inclusion flags.
      Parameters:
      names - the set tracking already-added property names
      cNode - the class node
      includeProperties - whether declared properties are included
      includeFields - whether fields are exposed as properties
      includePseudoGetters - whether pseudo-getter properties are included
      includePseudoSetters - whether pseudo-setter properties are included
      traverseSuperClasses - whether super classes are traversed
      skipReadonly - whether read-only initialized fields are excluded
      Returns:
      the resulting List
    • getAllProperties

      public static List<PropertyNode> getAllProperties(Set<String> names, ClassNode origType, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly)
      Collects properties according to the supplied inclusion flags.
      Parameters:
      names - the set tracking already-added property names
      origType - the original class node used for pseudo-property resolution
      cNode - the class node
      includeProperties - whether declared properties are included
      includeFields - whether fields are exposed as properties
      includePseudoGetters - whether pseudo-getter properties are included
      includePseudoSetters - whether pseudo-setter properties are included
      traverseSuperClasses - whether super classes are traversed
      skipReadonly - whether read-only initialized fields are excluded
      Returns:
      the resulting List
    • getAllProperties

      public static List<PropertyNode> getAllProperties(Set<String> names, ClassNode origType, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly, boolean reverse, boolean allNames, boolean includeStatic)
      Collects properties according to the supplied inclusion flags.
      Parameters:
      names - the set tracking already-added property names
      origType - the original class node used for pseudo-property resolution
      cNode - the class node
      includeProperties - whether declared properties are included
      includeFields - whether fields are exposed as properties
      includePseudoGetters - whether pseudo-getter properties are included
      includePseudoSetters - whether pseudo-setter properties are included
      traverseSuperClasses - whether super classes are traversed
      skipReadonly - whether read-only initialized fields are excluded
      reverse - whether superclass traversal order is reversed
      allNames - whether special/internal field names are included
      includeStatic - whether static members are included
      Returns:
      the resulting List
    • getterThisX

      public static Expression getterThisX(ClassNode annotatedNode, PropertyNode pNode)
      This method is similar to propX(Expression, Expression) but will make sure that if the property being accessed is defined inside the classnode provided as a parameter, then a getter call is generated instead of a field access.
      Parameters:
      annotatedNode - the class node where the property node is accessed from
      pNode - the property being accessed
      Returns:
      a method call expression or a property expression
    • getterX

      public static Expression getterX(ClassNode annotatedNode, Expression receiver, PropertyNode pNode)
      This method is similar to propX(Expression, Expression) but will make sure that if the property being accessed is defined inside the classnode provided as a parameter, then a getter call is generated instead of a field access.
      Parameters:
      annotatedNode - the class node where the property node is accessed from
      receiver - the object having the property
      pNode - the property being accessed
      Returns:
      a method call expression or a property expression
    • geX

      public static BinaryExpression geX(Expression lhv, Expression rhv)
      Creates a greater-than-or-equal comparison expression.
      Parameters:
      lhv - the left-hand operand
      rhv - the right-hand operand
      Returns:
      the resulting BinaryExpression
    • gtX

      public static BinaryExpression gtX(Expression lhv, Expression rhv)
      Creates a greater-than comparison expression.
      Parameters:
      lhv - the left-hand operand
      rhv - the right-hand operand
      Returns:
      the resulting BinaryExpression
    • hasClassX

      public static BinaryExpression hasClassX(Expression instance, ClassNode cNode)
      Creates an expression testing whether instance.getClass() equals the supplied class.
      Parameters:
      instance - the instance expression
      cNode - the class node
      Returns:
      the resulting BinaryExpression
    • hasEqualFieldX

      public static BinaryExpression hasEqualFieldX(FieldNode fNode, Expression other)
      Creates an equality comparison against another object's field value.
      Parameters:
      fNode - the field node
      other - the other object/value expression
      Returns:
      the resulting BinaryExpression
    • hasEqualPropertyX

      public static BinaryExpression hasEqualPropertyX(ClassNode cNode, PropertyNode pNode, VariableExpression other)
      Creates an equality comparison against another object's property value.
      Parameters:
      cNode - the class node
      pNode - the property node
      other - the other object/value expression
      Returns:
      the resulting BinaryExpression
    • hasEqualPropertyX

      @Deprecated public static BinaryExpression hasEqualPropertyX(PropertyNode pNode, Expression other)
      Deprecated.
      Creates an equality comparison against another object's property value.
      Parameters:
      pNode - the property node
      other - the other object/value expression
      Returns:
      the resulting BinaryExpression
    • hasSameFieldX

      public static BooleanExpression hasSameFieldX(FieldNode fNode, Expression other)
      Creates an identity comparison against another object's field value.
      Parameters:
      fNode - the field node
      other - the other object/value expression
      Returns:
      the resulting BooleanExpression
    • hasSamePropertyX

      public static BooleanExpression hasSamePropertyX(PropertyNode pNode, Expression other)
      Creates an identity comparison against another object's property value.
      Parameters:
      pNode - the property node
      other - the other object/value expression
      Returns:
      the resulting BooleanExpression
    • ifElseS$$bridge

      @Deprecated public static Statement ifElseS$$bridge(Expression cond, Statement thenStmt, Statement elseStmt)
      Deprecated.
      Deprecated compatibility bridge for ifElseS(Expression, Statement, Statement).
      Parameters:
      cond - the condition expression
      thenStmt - the true-branch statement
      elseStmt - the false-branch statement
      Returns:
      the resulting Statement
    • ifElseS

      public static IfStatement ifElseS(Expression cond, Statement thenStmt, Statement elseStmt)
      Creates an if/else statement.
      Parameters:
      cond - the condition expression
      thenStmt - the true-branch statement
      elseStmt - the false-branch statement
      Returns:
      the resulting IfStatement
    • ifS$$bridge

      @Deprecated public static Statement ifS$$bridge(Expression cond, Expression trueExpr)
      Deprecated.
      Deprecated compatibility bridge for the ifS overloads.
      Parameters:
      cond - the condition expression
      trueExpr - the true-branch expression
      Returns:
      the resulting Statement
    • ifS

      public static IfStatement ifS(Expression cond, Expression trueExpr)
      Creates an if statement with an empty else branch.
      Parameters:
      cond - the condition expression
      trueExpr - the true-branch expression
      Returns:
      the resulting IfStatement
    • ifS$$bridge

      @Deprecated public static Statement ifS$$bridge(Expression cond, Statement trueStmt)
      Deprecated.
      Deprecated compatibility bridge for the ifS overloads.
      Parameters:
      cond - the condition expression
      trueStmt - the true-branch statement
      Returns:
      the resulting Statement
    • ifS

      public static IfStatement ifS(Expression cond, Statement trueStmt)
      Creates an if statement with an empty else branch.
      Parameters:
      cond - the condition expression
      trueStmt - the true-branch statement
      Returns:
      the resulting IfStatement
    • indexX

      public static Expression indexX(Expression target, Expression value)
      Creates an index access expression.
      Parameters:
      target - the assignment target expression
      value - the value expression
      Returns:
      the resulting Expression
    • isInstanceOfX

      public static BooleanExpression isInstanceOfX(Expression expr, ClassNode type)
      Creates an instanceof test expression.
      Parameters:
      expr - the expression
      type - the target type
      Returns:
      the resulting BooleanExpression
    • isNullOrInstanceOfX

      public static BooleanExpression isNullOrInstanceOfX(Expression expr, ClassNode type)
      Since:
      4.0.8
    • isNullX

      public static BooleanExpression isNullX(Expression expr)
    • isOneX

      public static BooleanExpression isOneX(Expression expr)
      Creates an equality test against numeric one.
      Parameters:
      expr - the expression
      Returns:
      the resulting BooleanExpression
    • isTrueX

      public static BooleanExpression isTrueX(Expression argExpr)
      Creates an equality test against Boolean.TRUE.
      Parameters:
      argExpr - the argument expression
      Returns:
      the resulting BooleanExpression
    • isZeroX

      public static BooleanExpression isZeroX(Expression expr)
      Creates an equality test against numeric zero.
      Parameters:
      expr - the expression
      Returns:
      the resulting BooleanExpression
    • listX

      public static ListExpression listX(List<Expression> args)
      Creates a list expression.
      Parameters:
      args - the argument expression(s)
      Returns:
      the resulting ListExpression
    • list2args

      public static ListExpression list2args(List<?> args)
      Converts values into a constant-based list expression.
      Parameters:
      args - the argument expression(s)
      Returns:
      the resulting ListExpression
    • classList2args

      public static ListExpression classList2args(List<String> args)
      Converts class-name strings into class-literal expressions.
      Parameters:
      args - the argument expression(s)
      Returns:
      the resulting ListExpression
    • localVarX

      public static VariableExpression localVarX(String name)
      Creates a local variable expression and marks it as the accessed variable.
      Parameters:
      name - the property name
      Returns:
      the resulting VariableExpression
    • localVarX

      public static VariableExpression localVarX(String name, ClassNode type)
      Creates a local variable expression and marks it as the accessed variable.
      Parameters:
      name - the property name
      type - the target type
      Returns:
      the resulting VariableExpression
    • leX

      public static BinaryExpression leX(Expression lhv, Expression rhv)
      Creates a less-than-or-equal comparison expression.
      Parameters:
      lhv - the left-hand operand
      rhv - the right-hand operand
      Returns:
      the resulting BinaryExpression
    • ltX

      public static BinaryExpression ltX(Expression lhv, Expression rhv)
      Creates a less-than comparison expression.
      Parameters:
      lhv - the left-hand operand
      rhv - the right-hand operand
      Returns:
      the resulting BinaryExpression
    • mapEntryX

      public static MapEntryExpression mapEntryX(Expression keyExpr, Expression valueExpr)
      Creates a map entry expression.
      Parameters:
      keyExpr - the map-entry key expression
      valueExpr - the map-entry value expression
      Returns:
      the resulting MapEntryExpression
    • mapEntryX

      public static MapEntryExpression mapEntryX(String key, Expression valueExpr)
      Creates a map entry expression.
      Parameters:
      key - the key expression
      valueExpr - the map-entry value expression
      Returns:
      the resulting MapEntryExpression
    • mapX

      public static MapExpression mapX()
      Creates a map expression.
      Returns:
      the resulting MapExpression
    • mapX

      public static MapExpression mapX(List<MapEntryExpression> expressions)
      Creates a map expression.
      Parameters:
      expressions - the map-entry expressions
      Returns:
      the resulting MapExpression
    • minusX

      public static BinaryExpression minusX(Expression lhv, Expression rhv)
      Creates a subtraction expression.
      Parameters:
      lhv - the left-hand operand
      rhv - the right-hand operand
      Returns:
      the resulting BinaryExpression
    • neX

      public static BinaryExpression neX(Expression lhv, Expression rhv)
      Creates an inequality comparison expression.
      Parameters:
      lhv - the left-hand operand
      rhv - the right-hand operand
      Returns:
      the resulting BinaryExpression
    • notIdenticalX

      public static BinaryExpression notIdenticalX(Expression lhv, Expression rhv)
      Creates a non-identity comparison expression.
      Parameters:
      lhv - the left-hand operand
      rhv - the right-hand operand
      Returns:
      the resulting BinaryExpression
    • notNullX

      public static BooleanExpression notNullX(Expression expr)
      Creates a non-null test expression.
      Parameters:
      expr - the expression
      Returns:
      the resulting BooleanExpression
    • notX

      public static NotExpression notX(Expression expr)
      Creates a logical NOT expression.
      Parameters:
      expr - the expression
      Returns:
      the resulting NotExpression
    • nullX

      public static ConstantExpression nullX()
      Creates a null constant expression.
      Returns:
      the resulting ConstantExpression
    • orX

      public static BinaryExpression orX(Expression lhv, Expression rhv)
      Creates a logical OR expression.
      Parameters:
      lhv - the left-hand operand
      rhv - the right-hand operand
      Returns:
      the resulting BinaryExpression
    • param

      public static Parameter param(ClassNode type, String name)
      Creates a parameter node.
      Parameters:
      type - the target type
      name - the property name
      Returns:
      the resulting Parameter
    • param

      public static Parameter param(ClassNode type, String name, Expression initialExpression)
      Creates a parameter node.
      Parameters:
      type - the target type
      name - the property name
      initialExpression - the initial expression for the parameter
      Returns:
      the resulting Parameter
    • params

      public static Parameter[] params(Parameter... params)
      Returns the input parameters or Parameter.EMPTY_ARRAY when input is null.
      Parameters:
      params - the parameter array
      Returns:
      the resulting Parameter[]
    • plusX

      public static BinaryExpression plusX(Expression lhv, Expression rhv)
      Creates an addition expression.
      Parameters:
      lhv - the left-hand operand
      rhv - the right-hand operand
      Returns:
      the resulting BinaryExpression
    • propX

      public static PropertyExpression propX(Expression owner, String property)
      Creates a property access expression.
      Parameters:
      owner - the owner expression
      property - the property name/expression
      Returns:
      the resulting PropertyExpression
    • propX$$bridge

      @Deprecated public static Expression propX$$bridge(Expression owner, String property)
      Deprecated.
      Deprecated compatibility bridge for the propX overloads.
      Parameters:
      owner - the owner expression
      property - the property name/expression
      Returns:
      the resulting Expression
    • propX

      public static PropertyExpression propX(Expression owner, Expression property)
      Creates a property access expression.
      Parameters:
      owner - the owner expression
      property - the property name/expression
      Returns:
      the resulting PropertyExpression
    • propX$$bridge

      @Deprecated public static Expression propX$$bridge(Expression owner, Expression property)
      Deprecated.
      Deprecated compatibility bridge for the propX overloads.
      Parameters:
      owner - the owner expression
      property - the property name/expression
      Returns:
      the resulting Expression
    • propX

      public static PropertyExpression propX(Expression owner, Expression property, boolean safe)
      Creates a property access expression.
      Parameters:
      owner - the owner expression
      property - the property name/expression
      safe - whether safe-navigation is enabled
      Returns:
      the resulting PropertyExpression
    • returnS

      public static Statement returnS(Expression expr)
      Creates a return statement.
      Parameters:
      expr - the expression
      Returns:
      the resulting Statement
    • safeExpression

      public static Statement safeExpression(Expression fieldExpr, Expression expression)
      Creates a null-guarded statement sequence.
      Parameters:
      fieldExpr - the field expression used in the guard
      expression - the expression
      Returns:
      the resulting Statement
    • sameX

      public static BooleanExpression sameX(Expression self, Expression other)
      Creates an identity test expression using Groovy's is method.
      Parameters:
      self - the left-hand expression
      other - the other object/value expression
      Returns:
      the resulting BooleanExpression
    • stmt

      public static Statement stmt(Expression expr)
      Wraps an expression in an ExpressionStatement.
      Parameters:
      expr - the expression
      Returns:
      the resulting Statement
    • spreadX

      public static SpreadExpression spreadX(Expression expr)
      Creates a spread expression.
      Parameters:
      expr - the expression
      Returns:
      the resulting SpreadExpression
    • switchS

      public static SwitchStatement switchS(Expression expr)
      Creates a switch statement.
      Parameters:
      expr - the expression
      Returns:
      the resulting SwitchStatement
    • switchS

      public static SwitchStatement switchS(Expression expr, Statement defaultStatement)
      Creates a switch statement.
      Parameters:
      expr - the expression
      defaultStatement - the default branch statement
      Returns:
      the resulting SwitchStatement
    • switchS

      public static SwitchStatement switchS(Expression expr, List<CaseStatement> caseStatements, Statement defaultStatement)
      Creates a switch statement.
      Parameters:
      expr - the expression
      caseStatements - the case statements
      defaultStatement - the default branch statement
      Returns:
      the resulting SwitchStatement
    • ternaryX

      public static TernaryExpression ternaryX(Expression cond, Expression trueExpr, Expression elseExpr)
      Creates a ternary expression.
      Parameters:
      cond - the condition expression
      trueExpr - the true-branch expression
      elseExpr - the false-branch expression
      Returns:
      the resulting TernaryExpression
    • thisPropX

      public static PropertyExpression thisPropX(boolean implicit, String property)
      Creates a property expression on this with configurable implicit-this behavior.
      Parameters:
      implicit - whether the receiver is implicit this
      property - the property name/expression
      Returns:
      the resulting PropertyExpression
    • throwS

      public static ThrowStatement throwS(Expression expr)
      Creates a throw statement.
      Parameters:
      expr - the expression
      Returns:
      the resulting ThrowStatement
    • tryCatchS

      public static TryCatchStatement tryCatchS(Statement tryStatement)
      Creates a try/catch/finally statement.
      Parameters:
      tryStatement - the try-block statement
      Returns:
      the resulting TryCatchStatement
    • tryCatchS

      public static TryCatchStatement tryCatchS(Statement tryStatement, Statement finallyStatement)
      Creates a try/catch/finally statement.
      Parameters:
      tryStatement - the try-block statement
      finallyStatement - the finally-block statement
      Returns:
      the resulting TryCatchStatement
    • tryCatchS

      public static TryCatchStatement tryCatchS(Statement tryStatement, Statement finallyStatement, CatchStatement... catchStatements)
      Creates a try/catch/finally statement.
      Parameters:
      tryStatement - the try-block statement
      finallyStatement - the finally-block statement
      catchStatements - the catch statements
      Returns:
      the resulting TryCatchStatement
    • varX

      public static VariableExpression varX(String name)
      Creates a variable expression.
      Parameters:
      name - the property name
      Returns:
      the resulting VariableExpression
    • varX

      public static VariableExpression varX(Variable variable)
      Creates a variable expression.
      Parameters:
      variable - the loop or catch parameter
      Returns:
      the resulting VariableExpression
    • varX

      public static VariableExpression varX(String name, ClassNode type)
      Creates a variable expression.
      Parameters:
      name - the property name
      type - the target type
      Returns:
      the resulting VariableExpression
    • cloneParams

      public static Parameter[] cloneParams(Parameter[] parameters)
      Clones parameter metadata as new Parameter instances with original type and name.
      Parameters:
      parameters - the parameters to clone
      Returns:
      the resulting Parameter[]
    • copyAnnotatedNodeAnnotations

      public static void copyAnnotatedNodeAnnotations(AnnotatedNode annotatedNode, List<AnnotationNode> copied, List<AnnotationNode> notCopied)
      Copies all candidateAnnotations with retention policy RetentionPolicy.RUNTIME and RetentionPolicy.CLASS.

      Annotations with GeneratedClosure members are not supported at present.

    • copyAnnotatedNodeAnnotations

      public static void copyAnnotatedNodeAnnotations(AnnotatedNode annotatedNode, List<AnnotationNode> copied, List<AnnotationNode> notCopied, boolean includeGenerated)
      Copies all candidateAnnotations with retention policy RetentionPolicy.RUNTIME and RetentionPolicy.CLASS. Generated annotations will be copied if includeGenerated is true.

      Annotations with GeneratedClosure members are not supported at present.

    • createConstructorStatementDefault

      public static Statement createConstructorStatementDefault(FieldNode fNode)
      Builds default constructor-assignment logic for a field and matching argument.
      Parameters:
      fNode - the field node
      Returns:
      the resulting Statement
    • getGetterName

      public static String getGetterName(PropertyNode pNode)
      Generally preferred to use PropertyNode.getGetterNameOrDefault() directly.
    • getGetterName

      public static String getGetterName(String name, Class<?> type)
      WARNING: Avoid this method unless just the name and type are available. Use getGetterName(PropertyNode) if the propertyNode is available.
    • getGetterName

      public static String getGetterName(String name)
      WARNING: Avoid this method unless just the name is available. Use getGetterName(PropertyNode) if the propertyNode is available. Use getGetterName(String, Class) if the type is available.
    • getSetterName

      public static String getSetterName(String name)
      Computes the JavaBean setter name for a property.
      Parameters:
      name - the property name
      Returns:
      the resulting String
    • convertASTToSource

      public static String convertASTToSource(ReaderSource readerSource, ASTNode expression) throws Exception
      Converts an expression into the String source. Only some specific expressions like closure expression support this.
      Parameters:
      readerSource - a source
      expression - an expression. Can't be null
      Returns:
      the source the closure was created from
      Throws:
      IllegalArgumentException - when expression is null
      Exception - when closure can't be read from source
    • copyStatementsWithSuperAdjustment

      public static boolean copyStatementsWithSuperAdjustment(ClosureExpression pre, BlockStatement body)
      Copies closure statements into a block and rewrites a leading super(...) call when present.
      Parameters:
      pre - the closure containing statements to copy
      body - the destination block
      Returns:
      true if a leading super call was rewritten; false otherwise
    • hasDeclaredMethod

      public static boolean hasDeclaredMethod(ClassNode cNode, String name, int argsCount)
      Checks whether a class declares a method with the specified name and arity.
      Parameters:
      cNode - the class node
      name - the property name
      argsCount - the expected argument count
      Returns:
      true if a matching declared method exists; false otherwise
    • findDeclaredMethod

      public static MethodNode findDeclaredMethod(ClassNode cNode, String name, int argsCount)
      Finds the first declared method with the specified name and arity.
      Parameters:
      cNode - the class node
      name - the property name
      argsCount - the expected argument count
      Returns:
      the matching method node, or null if not found
    • inSamePackage

      public static boolean inSamePackage(ClassNode first, ClassNode second)
      Checks whether two classes belong to the same package.
      Parameters:
      first - the first class/type
      second - the second class/type
      Returns:
      true if both classes are in the same package; false otherwise
    • inSamePackage

      public static boolean inSamePackage(Class<?> first, Class<?> second)
      Checks whether two classes belong to the same package.
      Parameters:
      first - the first class/type
      second - the second class/type
      Returns:
      true if both classes are in the same package; false otherwise
    • isDefaultVisibility

      public static boolean isDefaultVisibility(int modifiers)
      Checks whether modifier bits indicate package-private visibility.
      Parameters:
      modifiers - the modifier bit mask
      Returns:
      true if no public/protected/private visibility flag is set; false otherwise
    • isOrImplements

      public static boolean isOrImplements(ClassNode type, ClassNode interfaceType)
      Checks whether a type equals or implements an interface type.
      Parameters:
      type - the target type
      interfaceType - the interface to test against
      Returns:
      true if the type equals or implements the interface type; false otherwise
    • makeDescriptorWithoutReturnType

      @Deprecated public static String makeDescriptorWithoutReturnType(MethodNode mn)
      Deprecated.
      use MethodNodeUtils#methodDescriptorWithoutReturnType(MethodNode) instead
    • maybeFallsThrough

      public static boolean maybeFallsThrough(Statement statement)
      Determines whether execution may continue past the supplied statement.
      Parameters:
      statement - the statement to analyze
      Returns:
      true if execution may continue after the statement; false otherwise
    • isEmptyStatement

      public static boolean isEmptyStatement(Statement statement)
      Returns true if the given statement is effectively empty — i.e. it is null, an EmptyStatement, or a BlockStatement whose every nested statement is itself empty (recursively). This is subtly different from Statement.isEmpty(), which only checks whether a BlockStatement has zero entries, not whether those entries are all empty.
    • constantBooleanValue

      public static GeneralUtils.ConditionValue constantBooleanValue(Expression expression)
      Returns the compile-time constant boolean value of a loop condition expression. Handles nested BooleanExpression / NotExpression wrappers and an absent (empty) condition, which is always true (e.g. for(;;)). Returns GeneralUtils.ConditionValue.UNKNOWN when the value cannot be determined statically.
    • maybeFallsThroughToNextSwitchCase

      public static boolean maybeFallsThroughToNextSwitchCase(Statement statement, SwitchStatement switchStatement)
      Indicates whether switch-case code may fall through into the next case body.
    • mayReachLoopCondition

      public static boolean mayReachLoopCondition(LoopingStatement statement)
      Indicates whether execution of a loop body may reach the loop's condition/update step.