ClassNodeUtils (Groovy 5.1.0)
Description: declaration: package: org.apache.groovy.ast.tools, class: ClassNodeUtils

Package org.apache.groovy.ast.tools

# Class ClassNodeUtils

java.lang.Object
org.apache.groovy.ast.tools.ClassNodeUtils

public class ClassNodeUtils extends Object
Utility class for working with ClassNodes

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `addDeclaredMethodsFromAllInterfaces(ClassNode cNode, Map<String,MethodNode> methodsMap)`
  Adds methods from interfaces and parent interfaces.
  `static void`
  `addDeclaredMethodsFromInterfaces(ClassNode cNode, Map<String,MethodNode> methodsMap)`
  Adds methods from all interfaces.
  `static ConstructorNode`
  `addGeneratedConstructor(ClassNode classNode, int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code)`
  Adds a method that is marked as `@Generated`.
  `static void`
  `addGeneratedConstructor(ClassNode classNode, ConstructorNode ctorNode)`
  Adds a method that is marked as `@Generated`.
  `static void`
  `addGeneratedInnerClass(ClassNode cNode, ClassNode inner)`
  Adds an inner class that is marked as `@Generated`.
  `static MethodNode`
  `addGeneratedMethod(ClassNode cNode, String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code)`
  Returns an existing method if one exists or else create a new method and mark it as `@Generated`.
  `static void`
  `addGeneratedMethod(ClassNode cNode, MethodNode mNode)`
  Adds a method and mark it as `@Generated`.
  `static void`
  `addGeneratedMethod(ClassNode cNode, MethodNode mNode, boolean skipChecks)`
  Adds a method and mark it as `@Generated`.
  `static String`
  `formatTypeName(ClassNode cNode)`
  Formats a type name into a human-readable version.
  `static Map<String,MethodNode>`
  `getDeclaredMethodsFromInterfaces(ClassNode cNode)`
  Gets methods from all interfaces.
  `static Map<String,MethodNode>`
  `getDeclaredMethodsFromSuper(ClassNode cNode)`
  Gets methods from the super class.
  `static FieldNode`
  `getField(ClassNode classNode, String fieldName)`
  Searches the class for a field that matches specified name.
  `static FieldNode`
  `getField(ClassNode classNode, String fieldName, Predicate<? super FieldNode> acceptability)`
  Searches the class for a field that matches specified name and test.
  `static MethodNode`
  `getMethod(ClassNode classNode, String methodName, Predicate<? super MethodNode> acceptability)`
  Searches the class for a method that matches specified name and test.
  `static ClassNode`
  `getNestHost(ClassNode cNode)`
  Returns the nest host of the given class, which may be the class itself.
  `static String`
  `getPropNameForAccessor(String accessorName)`
  Returns the property name, e.g.
  `static PropertyNode`
  `getStaticProperty(ClassNode cNode, String propName)`
  Finds static property within the class or super class.
  `static boolean`
  `hasExplicitConstructor(AbstractASTTransformation xform, ClassNode cNode)`
  Determines if the given ClassNode declares an explicit (non-generated) constructor.
  `static boolean`
  `hasNoArgConstructor(ClassNode cNode)`
  Determines if the given ClassNode declares a zero argument constructor.
  `static boolean`
  `hasPossibleStaticMethod(ClassNode cNode, String name, Expression arguments, boolean trySpread)`
  Determines if the given method has a possibly matching static method with the given name and arguments.
  `static boolean`
  `hasPossibleStaticProperty(ClassNode cNode, String methodName)`
  Determines if the given class has the named static property.
  `static boolean`
  `hasStaticProperty(ClassNode cNode, String propName)`
  Determines if the given class has the named static property.
  `static boolean`
  `isCompatibleWith(ClassNode source, ClassNode target)`
  Determines if the source ClassNode is compatible with the target ClassNode.
  `static boolean`
  `isInnerClass(ClassNode cNode)`
  Determines if the given ClassNode is a non-static inner class.
  `static boolean`
  `isNonSealed(ClassNode cn)`
  Determines if the type is declared `non-sealed`.
  `static boolean`
  `isSubtype(ClassNode maybeExtendedOrImplemented, ClassNode cNode)`
  Determines if given class is, extends or implements a class or interface.
  `static boolean`
  `isValidAccessorName(String accessorName)`
  Determines if the given accessor name starts with "get", "set" or "is" followed by at least one character.
  `static boolean`
  `samePackageName(ClassNode first, ClassNode second)`
  Determines if the given ClassNode values have the same package name.

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

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

- ## Method Details

  - ### formatTypeName

    public static String formatTypeName(ClassNode cNode)
    Formats a type name into a human-readable version. For arrays, appends "[]" to the formatted type name of the component. For unit class nodes, uses the class node name.
    Parameters:
      `cNode` - the type to format
    Returns:
      a human-readable version of the type name (java.lang.String[] for example)
    Since:
      2.5.0

  - ### addGeneratedMethod

    public static MethodNode addGeneratedMethod(ClassNode cNode, String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code)
    Returns an existing method if one exists or else create a new method and mark it as `@Generated`.
    Since:
      2.5.3
    See Also:
      - `ClassNode.addMethod(String, int, ClassNode, Parameter[], ClassNode[], Statement)`

  - ### addGeneratedMethod

    public static void addGeneratedMethod(ClassNode cNode, MethodNode mNode)
    Adds a method and mark it as `@Generated`.
    Since:
      2.5.3
    See Also:
      - `ClassNode.addMethod(MethodNode)`

  - ### addGeneratedMethod

    public static void addGeneratedMethod(ClassNode cNode, MethodNode mNode, boolean skipChecks)
    Adds a method and mark it as `@Generated`.
    Since:
      3.0.8
    See Also:
      - `ClassNode.addMethod(MethodNode)`

  - ### addGeneratedInnerClass

    public static void addGeneratedInnerClass(ClassNode cNode, ClassNode inner)
    Adds an inner class that is marked as `@Generated`.
    Since:
      2.5.7
    See Also:
      - `ModuleNode.addClass(ClassNode)`

  - ### addGeneratedConstructor

    public static ConstructorNode addGeneratedConstructor(ClassNode classNode, int modifiers, Parameter[] parameters, ClassNode[] exceptions, Statement code)
    Adds a method that is marked as `@Generated`.
    Since:
      2.5.3
    See Also:
      - `ClassNode.addConstructor(int, Parameter[], ClassNode[], Statement)`

  - ### addGeneratedConstructor

    public static void addGeneratedConstructor(ClassNode classNode, ConstructorNode ctorNode)
    Adds a method that is marked as `@Generated`.
    Since:
      2.5.3
    See Also:
      - `ClassNode.addConstructor(ConstructorNode)`

  - ### getDeclaredMethodsFromSuper

    public static Map<String,MethodNode> getDeclaredMethodsFromSuper(ClassNode cNode)
    Gets methods from the super class.
    Since:
      2.5.0

  - ### addDeclaredMethodsFromInterfaces

    public static void addDeclaredMethodsFromInterfaces(ClassNode cNode, Map<String,MethodNode> methodsMap)
    Adds methods from all interfaces. Existing entries in the methods map take precedence. Methods from interfaces visited early take precedence over later ones.
    Parameters:
      `cNode` - The ClassNode
      `methodsMap` - A map of existing methods to alter
    Since:
      2.5.0

  - ### getDeclaredMethodsFromInterfaces

    public static Map<String,MethodNode> getDeclaredMethodsFromInterfaces(ClassNode cNode)
    Gets methods from all interfaces. Methods from interfaces visited early take precedence over later ones.
    Parameters:
      `cNode` - The ClassNode
    Returns:
      A map of methods
    Since:
      2.5.0

  - ### addDeclaredMethodsFromAllInterfaces

    public static void addDeclaredMethodsFromAllInterfaces(ClassNode cNode, Map<String,MethodNode> methodsMap)
    Adds methods from interfaces and parent interfaces. Existing entries in the methods map take precedence. Methods from interfaces visited early take precedence over later ones.
    Parameters:
      `cNode` - The ClassNode
      `methodsMap` - A map of existing methods to alter
    Since:
      2.5.0

  - ### hasPossibleStaticMethod

    public static boolean hasPossibleStaticMethod(ClassNode cNode, String name, Expression arguments, boolean trySpread)
    Determines if the given method has a possibly matching static method with the given name and arguments. Handles default arguments and (optionally) spread expressions.
    Parameters:
      `cNode` - the ClassNode of interest
      `name` - the name of the method of interest
      `arguments` - the arguments to match against
      `trySpread` - whether to try to account for SpreadExpressions within the arguments
    Returns:
      `true` if a matching method was found.
    Since:
      2.5.0

  - ### hasPossibleStaticProperty

    public static boolean hasPossibleStaticProperty(ClassNode cNode, String methodName)
    Determines if the given class has the named static property.
    Since:
      2.5.0

  - ### getPropNameForAccessor

    public static String getPropNameForAccessor(String accessorName)
    Returns the property name, e.g. age, given an accessor name, e.g. getAge. Returns the original if a valid prefix cannot be removed.
    Parameters:
      `accessorName` - the accessor name of interest, e.g. getAge
    Returns:
      the property name, e.g. age, or original if not a valid property accessor name
    Since:
      2.5.0

  - ### isValidAccessorName

    public static boolean isValidAccessorName(String accessorName)
    Determines if the given accessor name starts with "get", "set" or "is" followed by at least one character.
    Parameters:
      `accessorName` - the accessor name of interest, e.g. getAge
    Returns:
      true if a valid prefix is found
    Since:
      2.5.0

  - ### isNonSealed

    public static boolean isNonSealed(ClassNode cn)
    Determines if the type is declared `non-sealed`.
    Since:
      5.0.0

  - ### hasStaticProperty

    public static boolean hasStaticProperty(ClassNode cNode, String propName)
    Determines if the given class has the named static property.
    Since:
      2.5.0

  - ### getStaticProperty

    public static PropertyNode getStaticProperty(ClassNode cNode, String propName)
    Finds static property within the class or super class.
    Parameters:
      `cNode` - the ClassNode of interest
      `propName` - the property name
    Returns:
      the static property if found or else null
    Since:
      2.5.0

  - ### getNestHost

    public static ClassNode getNestHost(ClassNode cNode)
    Returns the nest host of the given class, which may be the class itself.
    Since:
      5.0.0

  - ### isInnerClass

    public static boolean isInnerClass(ClassNode cNode)
    Determines if the given ClassNode is a non-static inner class.
    Parameters:
      `cNode` - the ClassNode of interest
    Returns:
      true if the given node is a (non-static) inner class, else false
    Since:
      2.5.0

  - ### isCompatibleWith

    public static boolean isCompatibleWith(ClassNode source, ClassNode target)
    Determines if the source ClassNode is compatible with the target ClassNode.
    Since:
      4.0.0

  - ### hasNoArgConstructor

    public static boolean hasNoArgConstructor(ClassNode cNode)
    Determines if the given ClassNode declares a zero argument constructor.
    Since:
      2.5.0

  - ### hasExplicitConstructor

    public static boolean hasExplicitConstructor(AbstractASTTransformation xform, ClassNode cNode)
    Determines if the given ClassNode declares an explicit (non-generated) constructor.
    Parameters:
      `xform` - if non-null, add an error if an explicit constructor is found
      `cNode` - the type of the containing class
    Returns:
      true if an explicit (non-generated) constructor was found
    Since:
      2.5.0

  - ### samePackageName

    public static boolean samePackageName(ClassNode first, ClassNode second)
    Determines if the given ClassNode values have the same package name.
    Parameters:
      `first` - a ClassNode
      `second` - a ClassNode
    Returns:
      true if both given nodes have the same package name
    Since:
      2.5.0

  - ### getField

    public static FieldNode getField(ClassNode classNode, String fieldName)
    Searches the class for a field that matches specified name.
    Since:
      3.0.0

  - ### getField

    public static FieldNode getField(ClassNode classNode, String fieldName, Predicate<? super FieldNode> acceptability)
    Searches the class for a field that matches specified name and test.
    Since:
      4.0.0

  - ### getMethod

    public static MethodNode getMethod(ClassNode classNode, String methodName, Predicate<? super MethodNode> acceptability)
    Searches the class for a method that matches specified name and test.
    Since:
      5.0.0

  - ### isSubtype

    public static boolean isSubtype(ClassNode maybeExtendedOrImplemented, ClassNode cNode)
    Determines if given class is, extends or implements a class or interface.
    Since:
      4.0.16