Class ShortTypeHandling
Provides fast-path type conversions for common cases. These methods are designed to avoid dependency on the Groovy runtime and should be used directly only when the type constraints documented in each method are met.
For general-purpose type coercion, use DefaultTypeTransformation instead.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic CharactercastToChar(Object object) Casts an object to a char value (Character wrapper).static ClasscastToClass(Object object) Casts an object to a Class object.static EnumcastToEnum(Object object, Class<? extends Enum> type) Casts an object to an Enum value of the specified type.static StringcastToString(Object object) Casts an object to a String representation.
-
Constructor Details
-
ShortTypeHandling
public ShortTypeHandling()
-
-
Method Details
-
castToClass
Casts an object to a Class object.Accepts Class objects directly, or a string representation of a class name which is loaded via
Class.forName.- Parameters:
object- a Class object or string class name- Returns:
- the Class object, or null if input is null
- Throws:
GroovyCastException- if the object is not a Class or valid class name
-
castToString
Casts an object to a String representation.Arrays are converted using
Arrays.toStringformat. Primitive arrays receive special handling to show element values. Other objects use their standardtoString()method.- Parameters:
object- any object (may be null)- Returns:
- string representation, or null if input is null
-
castToEnum
Casts an object to an Enum value of the specified type.Note: This method requires that the supplied enum is not fitting a Collection case for casting.
Accepts Enum objects already of the correct type, or a String/GString representation of the enum constant name.
- Parameters:
object- an enum value, or a string enum constant nametype- the target enum class- Returns:
- the Enum value, or null if input is null
- Throws:
GroovyCastException- if the object cannot be converted to the enum type
-
castToChar
Casts an object to a char value (Character wrapper).Accepts Character objects, Numbers (converted via intValue), or Strings of length 1.
- Parameters:
object- a Character, Number, or single-character String- Returns:
- a Character with the coerced value, or null if input is null
- Throws:
GroovyCastException- if the object cannot be converted to char
-