Package com.tnsai.actions
Class TypeConverter
java.lang.Object
com.tnsai.actions.TypeConverter
Utility class for converting values between types in action parameter handling.
TypeConverter is used by ActionExecutor to automatically convert
action parameters from their JSON-parsed types to the expected method parameter types.
Supported Conversions
| Source Type | Target Types |
|---|---|
| String | int, long, double, float, boolean |
| Number | int, long, double, float |
| Map<String, Object> | Any POJO (via Jackson) |
Usage Examples
Basic Type Conversion
// String to int
Object result = TypeConverter.convert("42", int.class);
assert result.equals(42);
// Number to int (handles JSON number parsing)
Object result = TypeConverter.convert(42L, int.class);
assert result.equals(42);
// Same type (no conversion)
Object result = TypeConverter.convert("hello", String.class);
assert result.equals("hello");
Map to POJO Conversion
// Define a record or POJO
public record UserDto(String name, int age) {}
// Convert map to POJO
Map<String, Object> params = Map.of("name", "Alice", "age", 30);
UserDto user = TypeConverter.convertMapToPojo(params, UserDto.class);
System.out.println(user.name()); // "Alice"
System.out.println(user.age()); // 30
Type Checking
// Check if type is primitive or wrapper
TypeConverter.isPrimitiveOrWrapper(int.class); // true
TypeConverter.isPrimitiveOrWrapper(Integer.class); // true
TypeConverter.isPrimitiveOrWrapper(String.class); // false
// Check if type is "simple" (doesn't need POJO conversion)
TypeConverter.isSimpleType(String.class); // true
TypeConverter.isSimpleType(Map.class); // true
TypeConverter.isSimpleType(UserDto.class); // false
Null Handling
All methods handle null values gracefully:
convert(null, type)returns nullconvertMapToPojo(null, type)throws IllegalArgumentException
Error Handling
Invalid conversions are handled as follows:
- String to number: Returns original string on NumberFormatException
- Map to POJO: Throws IllegalArgumentException with details
- See Also:
-
Method Summary
-
Method Details
-
convert
Converts a value to the target type if needed.- Parameters:
value- the value to converttargetType- the target type- Returns:
- the converted value, or null if conversion fails
-