From 698f6a39030f858d8d1bd9215bca2e5d3d7009c6 Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Tue, 10 Jun 2025 13:30:36 +0200 Subject: [PATCH] Fix some conversion problems --- .../seeseepuff/pcinv/meta/AssetProperty.java | 2 +- .../be/seeseepuff/pcinv/meta/Converters.java | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/main/java/be/seeseepuff/pcinv/meta/Converters.java diff --git a/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java b/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java index 3ec9cff..1415eb5 100644 --- a/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java +++ b/src/main/java/be/seeseepuff/pcinv/meta/AssetProperty.java @@ -99,7 +99,7 @@ public class AssetProperty { .setter((obj, value) -> { try { property.setAccessible(true); - property.set(obj, value); + property.set(obj, Converters.convert(value, property.getType())); } catch (IllegalAccessException e) { throw new RuntimeException(e); } diff --git a/src/main/java/be/seeseepuff/pcinv/meta/Converters.java b/src/main/java/be/seeseepuff/pcinv/meta/Converters.java new file mode 100644 index 0000000..7115720 --- /dev/null +++ b/src/main/java/be/seeseepuff/pcinv/meta/Converters.java @@ -0,0 +1,66 @@ +package be.seeseepuff.pcinv.meta; + +import lombok.experimental.UtilityClass; + +/** + * Utility class for converting values to different types. + */ +@UtilityClass +public class Converters { + /** + * Converts a value to the specified target type. + * + * @param value The value to convert. + * @param target The target class to convert the value to. + * @return The converted value, or null if conversion is not possible. + */ + public static Object convert(Object value, Class target) { + if (value == null) { + return null; + } + if (target.isInstance(value)) { + return value; // No conversion needed + } + if (value instanceof Long longValue) { + return convertLong(longValue, target); + } + if (value instanceof Integer intValue) { + return convertInteger(intValue, target); + } + throw new ClassCastException("Cannot convert " + value.getClass().getName() + " to " + target.getName()); + } + + /** + * Converts a Long value to the specified target type. + * + * @param value The Long value to convert. + * @param target The target class to convert the value to. + * @return The converted value, or throws ClassCastException if conversion is not possible. + */ + private static Object convertLong(Long value, Class target) { + if (target == Long.class || target == long.class) { + return value; // No conversion needed + } + if (target == Integer.class || target == int.class) { + return value.intValue(); + } + throw new ClassCastException("Cannot convert " + value.getClass().getName() + " to " + target.getName()); + } + + /** + * Converts an Integer value to the specified target type. + * + * @param value The Integer value to convert. + * @param target The target class to convert the value to. + * @return The converted value, or throws ClassCastException if conversion is not possible. + */ + private static Object convertInteger(Integer value, Class target) { + if (target == Integer.class || target == int.class) { + return value; // No conversion needed + } + if (target == Long.class || target == long.class) { + return value.longValue(); + } + throw new ClassCastException("Cannot convert " + value.getClass().getName() + " to " + target.getName()); + } +}