Package org.codehaus.groovy.ast
Class ClassHelper
java.lang.Object
org.codehaus.groovy.ast.ClassHelper
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:
- Predefined
ClassNodeconstants for primitive types, wrapper types, and common classes - Caching mechanisms to avoid creating duplicate ClassNode instances for the same class
- Factory methods for constructing ClassNodes from Java
Classor String names - Type utility methods for wrapper/unwrapper conversions and type comparisons
- Dynamic type representation for Groovy's dynamic typing system
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:
- Cached types: Common Java types (Object, String, Number, etc.) and all primitive types. These are pre-created once and reused for performance.
- Uncached types: Collection types (Map, List, Set) created without caching.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodeDeprecated.static final ClassNodeprotected static final ClassNode[]static final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final Stringstatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final ClassNodestatic final Class[]static final ClassNodestatic final ClassNodestatic final ClassNode -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ClassNodeProvides the dynamic type representation, used by Groovy for dynamic typing when type information is unknown.static MethodNodeReturns the single abstract method of a class node, if it is a SAM type, or null otherwise.static ClassNodegetNextSuperClass(ClassNode source, ClassNode target) Returns a super class or interface for a given class depending on supplied target.static ClassNodestatic ClassNodegetWrapper(ClassNode cn) Creates a ClassNode containing the wrapper of a ClassNode of primitive type.static booleanisBigDecimalType(ClassNode type) static booleanisBigIntegerType(ClassNode type) static booleanisCachedType(ClassNode type) static booleanisClassType(ClassNode type) static booleanisDynamicTyped(ClassNode type) static booleanstatic booleanisGeneratedFunction(ClassNode type) Checks if the type is a generated function, i.e.static booleanisGroovyObjectType(ClassNode type) static booleanisGStringType(ClassNode type) static booleanstatic booleanisObjectType(ClassNode type) static booleanisPrimitiveBoolean(ClassNode type) static booleanisPrimitiveByte(ClassNode type) static booleanisPrimitiveChar(ClassNode type) static booleanisPrimitiveDouble(ClassNode type) static booleanisPrimitiveFloat(ClassNode type) static booleanisPrimitiveInt(ClassNode type) static booleanisPrimitiveLong(ClassNode type) static booleanisPrimitiveShort(ClassNode type) static booleanTest to determine if a ClassNode is a primitive type.static booleanisPrimitiveVoid(ClassNode type) static booleanstatic booleanTest 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>static booleanisStringType(ClassNode type) static booleanisWrapperBoolean(ClassNode type) static booleanisWrapperByte(ClassNode type) static booleanisWrapperCharacter(ClassNode type) static booleanisWrapperDouble(ClassNode type) static booleanisWrapperFloat(ClassNode type) static booleanisWrapperInteger(ClassNode type) static booleanisWrapperLong(ClassNode type) static booleanisWrapperShort(ClassNode type) static booleanisWrapperVoid(ClassNode type) static ClassNodeCreates a ClassNode for the given Java class, using caching when available.static ClassNode[]Creates an array of ClassNodes using an array of Java classes, using caching where available.static ClassNodeCreates a ClassNode for the given Java class, optionally including generic type parameters.static ClassNodeCreates a ClassNode for the given class name, returning cached instances for predefined types.static ClassNodemakeCached(Class c) Creates a cachedClassNodefor the given Java class.static ClassNodestatic ClassNodeCreates a new ClassNode for the given Java class without caching.static ClassNodemakeWithoutCaching(Class c, boolean includeGenerics) Creates a new ClassNode for the given Java class without caching, optionally including generic types.static ClassNodemakeWithoutCaching(String name) Creates a new ClassNode for the given class name without caching.
-
Field Details
-
TUPLE_CLASSES
-
OBJECT_TYPE
-
CLOSURE_TYPE
-
GSTRING_TYPE
-
RANGE_TYPE
-
PATTERN_TYPE
-
STRING_TYPE
-
SCRIPT_TYPE
-
BINDING_TYPE
-
THROWABLE_TYPE
-
boolean_TYPE
-
char_TYPE
-
byte_TYPE
-
int_TYPE
-
long_TYPE
-
short_TYPE
-
double_TYPE
-
float_TYPE
-
Byte_TYPE
-
Short_TYPE
-
Integer_TYPE
-
Long_TYPE
-
Character_TYPE
-
Float_TYPE
-
Double_TYPE
-
Boolean_TYPE
-
BigInteger_TYPE
-
BigDecimal_TYPE
-
Number_TYPE
-
VOID_TYPE
-
void_WRAPPER_TYPE
-
METACLASS_TYPE
-
Iterator_TYPE
-
Annotation_TYPE
-
ELEMENT_TYPE_TYPE
-
AUTOCLOSEABLE_TYPE
-
CLONEABLE_TYPE
-
SERIALIZABLE_TYPE
-
SERIALIZEDLAMBDA_TYPE
-
SEALED_TYPE
-
OVERRIDE_TYPE
-
DEPRECATED_TYPE
-
MAP_TYPE
-
SET_TYPE
-
LIST_TYPE
-
Enum_Type
-
CLASS_Type
-
TUPLE_TYPE
-
STREAM_TYPE
-
ITERABLE_TYPE
-
REFERENCE_TYPE
-
COLLECTION_TYPE
-
COMPARABLE_TYPE
-
GROOVY_OBJECT_TYPE
-
GENERATED_LAMBDA_TYPE
-
GENERATED_CLOSURE_Type
-
GROOVY_INTERCEPTABLE_TYPE
-
GROOVY_OBJECT_SUPPORT_TYPE
-
DYNAMIC_TYPE
Deprecated. -
EMPTY_TYPE_ARRAY
-
OBJECT
- See Also:
-
-
Constructor Details
-
ClassHelper
public ClassHelper()
-
-
Method Details
-
dynamicType
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:
- a ClassNode representing the dynamic type (Object with metadata flag)
- See Also:
-
makeCached
Creates a cachedClassNodefor 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.- Parameters:
c- the Java class to create a ClassNode for- Returns:
- a cached ClassNode for the given class
-
make
Creates an array of ClassNodes using an array of Java classes, using caching where available. Each class is converted to a ClassNode viamake(Class).- Parameters:
classes- the Java classes to convert to ClassNodes- Returns:
- an array of ClassNodes corresponding to the input classes, preserving order
- See Also:
-
make
Creates 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 viamakeWithoutCaching(Class).- Parameters:
c- the Java class to create a ClassNode for- Returns:
- a ClassNode representing the given class
- See Also:
-
make
Creates a ClassNode for the given Java class, optionally including generic type parameters. Predefined types return cached instances; others are created uncached.- Parameters:
c- the Java class to create a ClassNode forincludeGenerics- whether to include generic type parameters from the class definition- Returns:
- a ClassNode representing the given class
-
makeWithoutCaching
Creates a new ClassNode for the given Java class without caching. Each call creates a unique ClassNode instance, suitable for cases requiring independent instances.- Parameters:
c- the Java class to create a ClassNode for- Returns:
- a new ClassNode representing the given class (not cached)
- See Also:
-
makeWithoutCaching
Creates a new ClassNode for the given Java class without caching, optionally including generic types. Always creates a unique instance, even for predefined types.- Parameters:
c- the Java class to create a ClassNode forincludeGenerics- whether to include generic type parameters from the class- Returns:
- a new ClassNode representing the given class (not cached)
-
makeWithoutCaching
Creates 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.- Parameters:
name- the fully-qualified or simple class name- Returns:
- a new ClassNode representing the named class (not cached)
- See Also:
-
make
Creates 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").- Parameters:
name- the fully-qualified or simple class name, or null/empty for dynamic type- Returns:
- a ClassNode for the named class, or the dynamic type if name is null or empty
- See Also:
-
getWrapper
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
- Parameters:
cn- the ClassNode containing a possible primitive type- See Also:
-
getUnwrapper
-
isPrimitiveType
Test to determine if a ClassNode is a primitive type. Note: this only works for ClassNodes created using a predefined ClassNode- Parameters:
cn- the ClassNode containing a possible primitive type- Returns:
- true if the ClassNode is a primitive type
- See Also:
-
isStaticConstantInitializerType
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>Note: this only works for ClassNodes created using a predefined ClassNode
- Parameters:
cn- the ClassNode to be tested- Returns:
- true if the ClassNode is of int, float, long, double or String type
- See Also:
-
isNumberType
-
makeReference
-
isCachedType
-
isDynamicTyped
-
isPrimitiveBoolean
-
isPrimitiveChar
-
isPrimitiveByte
-
isPrimitiveInt
-
isPrimitiveLong
-
isPrimitiveShort
-
isPrimitiveDouble
-
isPrimitiveFloat
-
isPrimitiveVoid
-
isWrapperBoolean
-
isWrapperCharacter
-
isWrapperByte
-
isWrapperInteger
-
isWrapperLong
-
isWrapperShort
-
isWrapperDouble
-
isWrapperFloat
-
isWrapperVoid
-
isBigIntegerType
-
isBigDecimalType
-
isStringType
-
isGStringType
-
isObjectType
-
isGroovyObjectType
-
isClassType
-
isSAMType
-
isFunctionalInterface
-
isGeneratedFunction
Checks if the type is a generated function, i.e. closure or lambda.- Since:
- 3.0.0
-
findSAM
Returns the single abstract method of a class node, if it is a SAM type, or null otherwise.- Parameters:
type- a type for which to search for a single abstract method- Returns:
- the method node if type is a SAM type, null otherwise
-
getNextSuperClass
Returns 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.
-