This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
66
src/main/java/be/seeseepuff/pcinv/meta/Converters.java
Normal file
66
src/main/java/be/seeseepuff/pcinv/meta/Converters.java
Normal file
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user