public class ClassHelper
extends Object
Helper for ClassNode creation and type management. Contains cached instances for all commonly used Groovy and Java types, plus factory methods for creating ClassNodes efficiently.
Provides:
Performance Note: ClassNodes are cached by reference for all predefined types.
Using make(Class) or make(String) returns cached instances when available,
providing O(1) lookup. Use makeWithoutCaching() only when a unique instance is needed.
Type Categories:
| Modifiers | Name | Description |
|---|---|---|
static ClassNode |
DYNAMIC_TYPE |
|
protected static ClassNode[] |
EMPTY_TYPE_ARRAY |
|
static String |
OBJECT |
|
static ClassNode |
OBJECT_TYPE |
|
static Class[] |
TUPLE_CLASSES |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public static ClassNode |
dynamicType()Provides the dynamic type representation, used by Groovy for dynamic typing when type information is unknown. |
|
public static MethodNode |
findSAM(ClassNode type)Returns the single abstract method of a class node, if it is a SAM type, or null otherwise. |
|
public static ClassNode |
getNextSuperClass(ClassNode source, ClassNode target)Returns a super class or interface for a given class depending on supplied target. |
|
public static ClassNode |
getUnwrapper(ClassNode cn) |
|
public static ClassNode |
getWrapper(ClassNode cn)Creates a ClassNode containing the wrapper of a ClassNode of primitive type. |
|
public static boolean |
isBigDecimalType(ClassNode type) |
|
public static boolean |
isBigIntegerType(ClassNode type) |
|
public static boolean |
isCachedType(ClassNode type) |
|
public static boolean |
isClassType(ClassNode type) |
|
public static boolean |
isDynamicTyped(ClassNode type) |
|
public static boolean |
isFunctionalInterface(ClassNode type) |
|
public static boolean |
isGStringType(ClassNode type) |
|
public static boolean |
isGeneratedFunction(ClassNode type)Checks if the type is a generated function, i.e. closure or lambda. |
|
public static boolean |
isGroovyObjectType(ClassNode type) |
|
public static boolean |
isNumberType(ClassNode cn) |
|
public static boolean |
isObjectType(ClassNode type) |
|
public static boolean |
isPrimitiveBoolean(ClassNode type) |
|
public static boolean |
isPrimitiveByte(ClassNode type) |
|
public static boolean |
isPrimitiveChar(ClassNode type) |
|
public static boolean |
isPrimitiveDouble(ClassNode type) |
|
public static boolean |
isPrimitiveFloat(ClassNode type) |
|
public static boolean |
isPrimitiveInt(ClassNode type) |
|
public static boolean |
isPrimitiveLong(ClassNode type) |
|
public static boolean |
isPrimitiveShort(ClassNode type) |
|
public static boolean |
isPrimitiveType(ClassNode cn)Test to determine if a ClassNode is a primitive type. |
|
public static boolean |
isPrimitiveVoid(ClassNode type) |
|
public static boolean |
isSAMType(ClassNode type) |
|
public static boolean |
isStaticConstantInitializerType(ClassNode cn)Test to determine if a ClassNode is a type belongs to the list of types which are allowed to initialize constants directly in bytecode instead of using <cinit> |
|
public static boolean |
isStringType(ClassNode type) |
|
public static boolean |
isWrapperBoolean(ClassNode type) |
|
public static boolean |
isWrapperByte(ClassNode type) |
|
public static boolean |
isWrapperCharacter(ClassNode type) |
|
public static boolean |
isWrapperDouble(ClassNode type) |
|
public static boolean |
isWrapperFloat(ClassNode type) |
|
public static boolean |
isWrapperInteger(ClassNode type) |
|
public static boolean |
isWrapperLong(ClassNode type) |
|
public static boolean |
isWrapperShort(ClassNode type) |
|
public static boolean |
isWrapperVoid(ClassNode type) |
|
public static ClassNode[] |
make(Class[] classes)Creates an array of ClassNodes using an array of Java classes, using caching where available. |
|
public static ClassNode |
make(Class c)Creates a ClassNode for the given Java class, using caching when available. |
|
public static ClassNode |
make(Class c, boolean includeGenerics)Creates a ClassNode for the given Java class, optionally including generic type parameters. |
|
public static ClassNode |
make(String name)Creates a ClassNode for the given class name, returning cached instances for predefined types. |
|
public static ClassNode |
makeCached(Class c)Creates a cached ClassNode for the given Java class. |
|
public static ClassNode |
makeReference() |
|
public static ClassNode |
makeWithoutCaching(Class c)Creates a new ClassNode for the given Java class without caching. |
|
public static ClassNode |
makeWithoutCaching(Class c, boolean includeGenerics)Creates a new ClassNode for the given Java class without caching, optionally including generic types. |
|
public static ClassNode |
makeWithoutCaching(String name)Creates a new ClassNode for the given class name without caching. |
Provides the dynamic type representation, used by Groovy for dynamic typing when type information is unknown. Returns a ClassNode marked with metadata indicating it represents a dynamic type.
Returns the single abstract method of a class node, if it is a SAM type, or null otherwise.
type - a type for which to search for a single abstract methodReturns a super class or interface for a given class depending on supplied target. If the target is not a super class or interface, then null will be returned. For a non-primitive array type -- if the target is also an array -- returns an array of the component type's super class or interface.
Creates a ClassNode containing the wrapper of a ClassNode of primitive type. Any ClassNode representing a primitive type should be created using the predefined types used in class. The method will check the parameter for known references of ClassNode representing a primitive type. If Reference is found, then a ClassNode will be contained that represents the wrapper class. For example for boolean, the wrapper class is java.lang.Boolean.
If the parameter is no primitive type, the redirected ClassNode will be returned
cn - the ClassNode containing a possible primitive typeChecks if the type is a generated function, i.e. closure or lambda.
Test to determine if a ClassNode is a primitive type. Note: this only works for ClassNodes created using a predefined ClassNode
cn - the ClassNode containing a possible primitive typeTest to determine if a ClassNode is a type belongs to the list of types which are allowed to initialize constants directly in bytecode instead of using <cinit>
Note: this only works for ClassNodes created using a predefined ClassNode
cn - the ClassNode to be testedCreates an array of ClassNodes using an array of Java classes, using caching where available. Each class is converted to a ClassNode via make(Class).
classes - the Java classes to convert to ClassNodesCreates a ClassNode for the given Java class, using caching when available. Predefined types (like Integer, String, etc.) return cached instances. For first-time requests of non-predefined types, a new ClassNode is created via makeWithoutCaching(Class).
c - the Java class to create a ClassNode forCreates a ClassNode for the given Java class, optionally including generic type parameters. Predefined types return cached instances; others are created uncached.
c - the Java class to create a ClassNode forincludeGenerics - whether to include generic type parameters from the class definitionCreates a ClassNode for the given class name, returning cached instances for predefined types. Returns the dynamic type for null or empty names. Checks both primitive names ("int", "boolean") and fully-qualified names ("java.lang.String", "java.util.Map").
name - the fully-qualified or simple class name, or null/empty for dynamic typeCreates a cached ClassNode for the given Java class. Cached instances are reused across the compilation unit, reducing memory overhead. If the cache does not contain an entry, a new ClassNode is created, cached, and returned.
c - the Java class to create a ClassNode forCreates a new ClassNode for the given Java class without caching. Each call creates a unique ClassNode instance, suitable for cases requiring independent instances.
c - the Java class to create a ClassNode forCreates a new ClassNode for the given Java class without caching, optionally including generic types. Always creates a unique instance, even for predefined types.
c - the Java class to create a ClassNode forincludeGenerics - whether to include generic type parameters from the classCreates a new ClassNode for the given class name without caching. Always creates a unique instance. The ClassNode is set up with Object as superclass. This is typically used for dynamically-created or parsed class names.
name - the fully-qualified or simple class name