Package: org.codehaus.groovy.ast.tools

## [Java] Class GeneralUtils

- org.codehaus.groovy.ast.tools.GeneralUtils

- Handy methods when working with the Groovy AST

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `static Token` | `AND` |
    `static Token` | `ASSIGN` |
    `static Token` | `CMP` |
    `static Token` | `EQ` |
    `static Token` | `LT` |
    `static Token` | `NE` |
    `static Token` | `OR` |

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `static BinaryExpression` | `andX(Expression lhv, Expression rhv)`
    `` | `static ArgumentListExpression` | `args(Expression... expressions)`
    `` | `static ArgumentListExpression` | `args(List<Expression> expressions)`
    `` | `static ArgumentListExpression` | `args(Parameter[] parameters)`
    `` | `static ArgumentListExpression` | `args(String... names)`
    `` | `static Statement` | `assignS(Expression target, Expression value)`
    `` | `static Expression` | `assignX(Expression target, Expression value)`
    `` | `static Expression` | `attrX(Expression oe, Expression prop)`
    `` | `static BinaryExpression` | `binX(Expression left, Token token, Expression right)`
    `` | `static BlockStatement` | `block(VariableScope varScope, Statement... stmts)`
    `` | `static BlockStatement` | `block(VariableScope varScope, List<Statement> stmts)`
    `` | `static BlockStatement` | `block(Statement... stmts)`
    `` | `static BooleanExpression` | `boolX(Expression boolExpr)`
    `` | `static MethodCallExpression` | `callSuperX(String methodName, Expression args)`
    `` | `static MethodCallExpression` | `callSuperX(String methodName)`
    `` | `static MethodCallExpression` | `callThisX(String methodName, Expression args)`
    `` | `static MethodCallExpression` | `callThisX(String methodName)`
    `` | `static MethodCallExpression` | `callX(Expression receiver, String methodName, Expression args)`
    `` | `static MethodCallExpression` | `callX(Expression receiver, Expression method, Expression args)`
    `` | `static MethodCallExpression` | `callX(Expression receiver, String methodName)`
    `` | `static StaticMethodCallExpression` | `callX(ClassNode receiver, String methodName, Expression args)`
    `` | `static StaticMethodCallExpression` | `callX(ClassNode receiver, String methodName)`
    `` | `static CastExpression` | `castX(ClassNode type, Expression expression)`
    `` | `static CastExpression` | `castX(ClassNode type, Expression expression, boolean ignoreAutoboxing)`
    `` | `static CatchStatement` | `catchS(Parameter variable, Statement code)`
    `` | `static ListExpression` | `classList2args(List<String> args)`
    `` | `static ClassExpression` | `classX(ClassNode clazz)`
    `` | `static ClassExpression` | `classX(Class clazz)`
    `` | `static Parameter[]` | `cloneParams(Parameter[] source)`
    `` | `static ClosureExpression` | `closureX(Parameter[] params, Statement code)`
    `` | `static ClosureExpression` | `closureX(Statement code)`
    `` | `static BinaryExpression` | `cmpX(Expression lhv, Expression rhv)` Build a binary expression that compares two values
    `` | `static ConstantExpression` | `constX(Object val)`
    `` | `static ConstantExpression` | `constX(Object val, boolean keepPrimitive)`
    `` | `static String` | `convertASTToSource(ReaderSource readerSource, ASTNode expression)` Converts an expression into the String source.
    `` | `static void` | `copyAnnotatedNodeAnnotations(AnnotatedNode annotatedNode, List<AnnotationNode> copied, List<AnnotationNode> notCopied)` Copies all `candidateAnnotations` with retention policy RetentionPolicy.RUNTIME and RetentionPolicy.CLASS.
    `` | `static boolean` | `copyStatementsWithSuperAdjustment(ClosureExpression pre, BlockStatement body)`
    `` | `static Statement` | `createConstructorStatementDefault(FieldNode fNode)`
    `` | `static Statement` | `ctorSuperS(Expression args)`
    `` | `static Statement` | `ctorSuperS()`
    `` | `static Statement` | `ctorThisS(Expression args)`
    `` | `static Statement` | `ctorThisS()`
    `` | `static ConstructorCallExpression` | `ctorX(ClassNode type, Expression args)`
    `` | `static ConstructorCallExpression` | `ctorX(ClassNode type)`
    `` | `static Statement` | `declS(Expression target, Expression init)`
    `` | `static BinaryExpression` | `eqX(Expression lhv, Expression rhv)`
    `` | `static BooleanExpression` | `equalsNullX(Expression argExpr)`
    `` | `static FieldExpression` | `fieldX(FieldNode fieldNode)`
    `` | `static FieldExpression` | `fieldX(ClassNode owner, String fieldName)`
    `` | `static ClassNode` | `findActualTypeByGenericsPlaceholderName(String placeholderName, Map<GenericsType, GenericsType> genericsPlaceholderAndTypeMap)` Get the actual type according to the placeholder name
    `` | `static Expression` | `findArg(String argName)`
    `` | `static List<MethodNode>` | `getAllMethods(ClassNode type)`
    `` | `static List<PropertyNode>` | `getAllProperties(ClassNode type)`
    `` | `static List<PropertyNode>` | `getAllProperties(Set<String> names, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly)`
    `` | `static List<PropertyNode>` | `getAllProperties(Set<String> names, ClassNode origType, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly)`
    `` | `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)`
    `` | `static String` | `getGetterName(PropertyNode pNode)`
    `` | `static List<String>` | `getInstanceNonPropertyFieldNames(ClassNode cNode)`
    `` | `static List<FieldNode>` | `getInstanceNonPropertyFields(ClassNode cNode)`
    `` | `static List<PropertyNode>` | `getInstanceProperties(ClassNode cNode)`
    `` | `static List<FieldNode>` | `getInstancePropertyFields(ClassNode cNode)`
    `` | `static List<String>` | `getInstancePropertyNames(ClassNode cNode)`
    `` | `static Set<ClassNode>` | `getInterfacesAndSuperInterfaces(ClassNode type)`
    `` | `static String` | `getSetterName(String name)`
    `` | `static List<FieldNode>` | `getSuperNonPropertyFields(ClassNode cNode)`
    `` | `static List<FieldNode>` | `getSuperPropertyFields(ClassNode cNode)`
    `` | `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.
    `` | `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.
    `` | `static BinaryExpression` | `hasClassX(Expression instance, ClassNode cNode)`
    `` | `static boolean` | `hasDeclaredMethod(ClassNode cNode, String name, int argsCount)`
    `` | `static BinaryExpression` | `hasEqualFieldX(FieldNode fNode, Expression other)`
    `` | `static BinaryExpression` | `hasEqualPropertyX(ClassNode annotatedNode, PropertyNode pNode, VariableExpression other)`
    `` | `static BinaryExpression` | `hasEqualPropertyX(PropertyNode pNode, Expression other)`
    `` | `static BooleanExpression` | `hasSameFieldX(FieldNode fNode, Expression other)`
    `` | `static BooleanExpression` | `hasSamePropertyX(PropertyNode pNode, Expression other)`
    `` | `static Statement` | `ifElseS(Expression cond, Statement thenStmt, Statement elseStmt)`
    `` | `static Statement` | `ifS(Expression cond, Expression trueExpr)`
    `` | `static Statement` | `ifS(Expression cond, Statement trueStmt)`
    `` | `static boolean` | `inSamePackage(ClassNode first, ClassNode second)`
    `` | `static boolean` | `inSamePackage(Class first, Class second)`
    `` | `static Expression` | `indexX(Expression target, Expression value)`
    `` | `static boolean` | `isDefaultVisibility(int modifiers)`
    `` | `static BooleanExpression` | `isInstanceOfX(Expression objectExpression, ClassNode cNode)`
    `` | `static BooleanExpression` | `isOneX(Expression expr)`
    `` | `static boolean` | `isOrImplements(ClassNode type, ClassNode interfaceType)`
    `` | `static BooleanExpression` | `isTrueX(Expression argExpr)`
    `` | `static BooleanExpression` | `isZeroX(Expression expr)`
    `` | `static ListExpression` | `list2args(List args)`
    `` | `static BinaryExpression` | `ltX(Expression lhv, Expression rhv)`
    `` | `static Map<GenericsType, GenericsType>` | `makeDeclaringAndActualGenericsTypeMap(ClassNode declaringClass, ClassNode actualReceiver)` map declaring generics type to actual generics type, e.g.
    `` | `static String` | `makeDescriptorWithoutReturnType(MethodNode mn)` @deprecated use MethodNodeUtils#methodDescriptorWithoutReturnType(MethodNode) instead
    `` | `static BinaryExpression` | `neX(Expression lhv, Expression rhv)`
    `` | `static BooleanExpression` | `notNullX(Expression argExpr)`
    `` | `static NotExpression` | `notX(Expression expr)`
    `` | `static BinaryExpression` | `orX(Expression lhv, Expression rhv)`
    `` | `static Parameter` | `param(ClassNode type, String name)`
    `` | `static Parameter` | `param(ClassNode type, String name, Expression initialExpression)`
    `` | `static Parameter[]` | `params(Parameter... params)`
    `` | `static BinaryExpression` | `plusX(Expression lhv, Expression rhv)`
    `` | `static Expression` | `propX(Expression owner, String property)`
    `` | `static Expression` | `propX(Expression owner, Expression property)`
    `` | `static Statement` | `returnS(Expression expr)`
    `` | `static Statement` | `safeExpression(Expression fieldExpr, Expression expression)`
    `` | `static BooleanExpression` | `sameX(Expression self, Expression other)`
    `` | `static Statement` | `stmt(Expression expr)`
    `` | `static TernaryExpression` | `ternaryX(Expression cond, Expression trueExpr, Expression elseExpr)`
    `` | `static ThrowStatement` | `throwS(Expression expr)`
    `` | `static VariableExpression` | `varX(String name)`
    `` | `static VariableExpression` | `varX(Variable variable)`
    `` | `static VariableExpression` | `varX(String name, ClassNode type)`

  - ### Inherited Methods Summary

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

- - ### Field Detail

    - #### public static final Token AND

    - #### public static final Token ASSIGN

    - #### public static final Token CMP

    - #### public static final Token EQ

    - #### public static final Token LT

    - #### public static final Token NE

    - #### public static final Token OR

  - ### Method Detail

    - #### public static BinaryExpression andX(Expression lhv, Expression rhv)

    - #### public static ArgumentListExpression args(Expression... expressions)

    - #### public static ArgumentListExpression args(List<Expression> expressions)

    - #### public static ArgumentListExpression args(Parameter[] parameters)

    - #### public static ArgumentListExpression args(String... names)

    - #### public static Statement assignS(Expression target, Expression value)

    - #### public static Expression assignX(Expression target, Expression value)

    - #### public static Expression attrX(Expression oe, Expression prop)

    - #### public static BinaryExpression binX(Expression left, Token token, Expression right)

    - #### public static BlockStatement block(VariableScope varScope, Statement... stmts)

    - #### public static BlockStatement block(VariableScope varScope, List<Statement> stmts)

    - #### public static BlockStatement block(Statement... stmts)

    - #### public static BooleanExpression boolX(Expression boolExpr)

    - #### public static MethodCallExpression callSuperX(String methodName, Expression args)

    - #### public static MethodCallExpression callSuperX(String methodName)

    - #### public static MethodCallExpression callThisX(String methodName, Expression args)

    - #### public static MethodCallExpression callThisX(String methodName)

    - #### public static MethodCallExpression callX(Expression receiver, String methodName, Expression args)

    - #### public static MethodCallExpression callX(Expression receiver, Expression method, Expression args)

    - #### public static MethodCallExpression callX(Expression receiver, String methodName)

    - #### public static StaticMethodCallExpression callX(ClassNode receiver, String methodName, Expression args)

    - #### public static StaticMethodCallExpression callX(ClassNode receiver, String methodName)

    - #### public static CastExpression castX(ClassNode type, Expression expression)

    - #### public static CastExpression castX(ClassNode type, Expression expression, boolean ignoreAutoboxing)

    - #### public static CatchStatement catchS(Parameter variable, Statement code)

    - #### public static ListExpression classList2args(List<String> args)

    - #### public static ClassExpression classX(ClassNode clazz)

    - #### public static ClassExpression classX(Class clazz)

    - #### public static Parameter[] cloneParams(Parameter[] source)

    - #### public static ClosureExpression closureX(Parameter[] params, Statement code)

    - #### public static ClosureExpression closureX(Statement code)

    - #### public static BinaryExpression cmpX(Expression lhv, Expression rhv)

      Build a binary expression that compares two values

      Parameters:
        `lhv` - expression for the value to compare from
        `rhv` - expression for the value value to compare to
      Returns:
        the expression comparing two values

    - #### public static ConstantExpression constX(Object val)

    - #### public static ConstantExpression constX(Object val, boolean keepPrimitive)

    - #### public static String convertASTToSource(ReaderSource readerSource, ASTNode expression)

      Converts an expression into the String source. Only some specific expressions like closure expression support this.

      throws:
        java.lang.IllegalArgumentException when expression is null
      throws:
        java.lang.Exception when closure can't be read from source
      Parameters:
        `readerSource` - a source
        `expression` - an expression. Can't be null
      Returns:
        the source the closure was created from

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

    - #### public static boolean copyStatementsWithSuperAdjustment(ClosureExpression pre, BlockStatement body)

    - #### public static Statement createConstructorStatementDefault(FieldNode fNode)

    - #### public static Statement ctorSuperS(Expression args)

    - #### public static Statement ctorSuperS()

    - #### public static Statement ctorThisS(Expression args)

    - #### public static Statement ctorThisS()

    - #### public static ConstructorCallExpression ctorX(ClassNode type, Expression args)

    - #### public static ConstructorCallExpression ctorX(ClassNode type)

    - #### public static Statement declS(Expression target, Expression init)

    - #### public static BinaryExpression eqX(Expression lhv, Expression rhv)

    - #### public static BooleanExpression equalsNullX(Expression argExpr)

    - #### public static FieldExpression fieldX(FieldNode fieldNode)

    - #### public static FieldExpression fieldX(ClassNode owner, String fieldName)

    - #### public static ClassNode findActualTypeByGenericsPlaceholderName(String placeholderName, Map<GenericsType, GenericsType> genericsPlaceholderAndTypeMap)

      Get the actual type according to the placeholder name

      Parameters:
        `placeholderName` - the placeholder name, e.g. T, E
        `genericsPlaceholderAndTypeMap` - the result of makeDeclaringAndActualGenericsTypeMap
      Returns:
        the actual type

    - #### public static Expression findArg(String argName)

    - #### public static List<MethodNode> getAllMethods(ClassNode type)

    - #### public static List<PropertyNode> getAllProperties(ClassNode type)

    - #### public static List<PropertyNode> getAllProperties(Set<String> names, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly)

    - #### public static List<PropertyNode> getAllProperties(Set<String> names, ClassNode origType, ClassNode cNode, boolean includeProperties, boolean includeFields, boolean includePseudoGetters, boolean includePseudoSetters, boolean traverseSuperClasses, boolean skipReadonly)

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

    - #### public static String getGetterName(PropertyNode pNode)

    - #### public static List<String> getInstanceNonPropertyFieldNames(ClassNode cNode)

    - #### public static List<FieldNode> getInstanceNonPropertyFields(ClassNode cNode)

    - #### public static List<PropertyNode> getInstanceProperties(ClassNode cNode)

    - #### public static List<FieldNode> getInstancePropertyFields(ClassNode cNode)

    - #### public static List<String> getInstancePropertyNames(ClassNode cNode)

    - #### public static Set<ClassNode> getInterfacesAndSuperInterfaces(ClassNode type)

    - #### public static String getSetterName(String name)

    - #### public static List<FieldNode> getSuperNonPropertyFields(ClassNode cNode)

    - #### public static List<FieldNode> getSuperPropertyFields(ClassNode cNode)

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

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

    - #### public static BinaryExpression hasClassX(Expression instance, ClassNode cNode)

    - #### public static boolean hasDeclaredMethod(ClassNode cNode, String name, int argsCount)

    - #### public static BinaryExpression hasEqualFieldX(FieldNode fNode, Expression other)

    - #### public static BinaryExpression hasEqualPropertyX(ClassNode annotatedNode, PropertyNode pNode, VariableExpression other)

    - #### @Deprecated public static BinaryExpression hasEqualPropertyX(PropertyNode pNode, Expression other)

    - #### public static BooleanExpression hasSameFieldX(FieldNode fNode, Expression other)

    - #### public static BooleanExpression hasSamePropertyX(PropertyNode pNode, Expression other)

    - #### public static Statement ifElseS(Expression cond, Statement thenStmt, Statement elseStmt)

    - #### public static Statement ifS(Expression cond, Expression trueExpr)

    - #### public static Statement ifS(Expression cond, Statement trueStmt)

    - #### public static boolean inSamePackage(ClassNode first, ClassNode second)

    - #### public static boolean inSamePackage(Class first, Class second)

    - #### public static Expression indexX(Expression target, Expression value)

    - #### public static boolean isDefaultVisibility(int modifiers)

    - #### public static BooleanExpression isInstanceOfX(Expression objectExpression, ClassNode cNode)

    - #### public static BooleanExpression isOneX(Expression expr)

    - #### public static boolean isOrImplements(ClassNode type, ClassNode interfaceType)

    - #### public static BooleanExpression isTrueX(Expression argExpr)

    - #### public static BooleanExpression isZeroX(Expression expr)

    - #### public static ListExpression list2args(List args)

    - #### public static BinaryExpression ltX(Expression lhv, Expression rhv)

    - #### public static Map<GenericsType, GenericsType> makeDeclaringAndActualGenericsTypeMap(ClassNode declaringClass, ClassNode actualReceiver)

      map declaring generics type to actual generics type, e.g. GROOVY-7204: declaring generics types: T, S extends Serializable actual generics types : String, Long the result map is [ T: String, S: Long ] The resolved types can not help us to choose methods correctly if the argument is a string: T: Object, S: Serializable so we need actual types: T: String, S: Long

    - #### @Deprecated public static String makeDescriptorWithoutReturnType(MethodNode mn)

      deprecated:
        use MethodNodeUtils#methodDescriptorWithoutReturnType(MethodNode) instead

    - #### public static BinaryExpression neX(Expression lhv, Expression rhv)

    - #### public static BooleanExpression notNullX(Expression argExpr)

    - #### public static NotExpression notX(Expression expr)

    - #### public static BinaryExpression orX(Expression lhv, Expression rhv)

    - #### public static Parameter param(ClassNode type, String name)

    - #### public static Parameter param(ClassNode type, String name, Expression initialExpression)

    - #### public static Parameter[] params(Parameter... params)

    - #### public static BinaryExpression plusX(Expression lhv, Expression rhv)

    - #### public static Expression propX(Expression owner, String property)

    - #### public static Expression propX(Expression owner, Expression property)

    - #### public static Statement returnS(Expression expr)

    - #### public static Statement safeExpression(Expression fieldExpr, Expression expression)

    - #### public static BooleanExpression sameX(Expression self, Expression other)

    - #### public static Statement stmt(Expression expr)

    - #### public static TernaryExpression ternaryX(Expression cond, Expression trueExpr, Expression elseExpr)

    - #### public static ThrowStatement throwS(Expression expr)

    - #### public static VariableExpression varX(String name)

    - #### public static VariableExpression varX(Variable variable)

    - #### public static VariableExpression varX(String name, ClassNode type)