in core/optaplanner-core-impl/src/main/java/org/optaplanner/core/config/util/ConfigUtils.java [109:172]
public static void applyCustomProperties(Object bean, String beanClassPropertyName,
Map<String, String> customProperties, String customPropertiesPropertyName) {
if (customProperties == null) {
return;
}
Class<?> beanClass = bean.getClass();
customProperties.forEach((propertyName, valueString) -> {
Method setterMethod = ReflectionHelper.getSetterMethod(beanClass, propertyName);
if (setterMethod == null) {
throw new IllegalStateException("The custom property " + propertyName + " (" + valueString
+ ") in the " + customPropertiesPropertyName
+ " cannot be set on the " + beanClassPropertyName + " (" + beanClass
+ ") because that class has no public setter for that property.\n"
+ "Maybe add a public setter for that custom property (" + propertyName
+ ") on that class (" + beanClass.getSimpleName() + ").\n"
+ "Maybe don't configure that custom property " + propertyName + " (" + valueString
+ ") in the " + customPropertiesPropertyName + ".");
}
Class<?> propertyType = setterMethod.getParameterTypes()[0];
Object typedValue;
try {
if (propertyType.equals(String.class)) {
typedValue = valueString;
} else if (propertyType.equals(Boolean.TYPE) || propertyType.equals(Boolean.class)) {
typedValue = Boolean.parseBoolean(valueString);
} else if (propertyType.equals(Integer.TYPE) || propertyType.equals(Integer.class)) {
typedValue = Integer.parseInt(valueString);
} else if (propertyType.equals(Long.TYPE) || propertyType.equals(Long.class)) {
typedValue = Long.parseLong(valueString);
} else if (propertyType.equals(Float.TYPE) || propertyType.equals(Float.class)) {
typedValue = Float.parseFloat(valueString);
} else if (propertyType.equals(Double.TYPE) || propertyType.equals(Double.class)) {
typedValue = Double.parseDouble(valueString);
} else if (propertyType.equals(BigDecimal.class)) {
typedValue = new BigDecimal(valueString);
} else if (propertyType.isEnum()) {
typedValue = Enum.valueOf((Class<? extends Enum>) propertyType, valueString);
} else {
throw new IllegalStateException("The custom property " + propertyName + " (" + valueString
+ ") in the " + customPropertiesPropertyName
+ " has an unsupported propertyType (" + propertyType + ") for value (" + valueString + ").");
}
} catch (NumberFormatException e) {
throw new IllegalStateException("The custom property " + propertyName + " (" + valueString
+ ") in the " + customPropertiesPropertyName
+ " cannot be parsed to the propertyType (" + propertyType
+ ") of the setterMethod (" + setterMethod + ").");
}
try {
setterMethod.invoke(bean, typedValue);
} catch (IllegalAccessException e) {
throw new IllegalStateException("The custom property " + propertyName + " (" + valueString
+ ") in the " + customPropertiesPropertyName
+ " has a setterMethod (" + setterMethod + ") on the beanClass (" + beanClass
+ ") that cannot be called for the typedValue (" + typedValue + ").", e);
} catch (InvocationTargetException e) {
throw new IllegalStateException("The custom property " + propertyName + " (" + valueString
+ ") in the " + customPropertiesPropertyName
+ " has a setterMethod (" + setterMethod + ") on the beanClass (" + beanClass
+ ") that throws an exception for the typedValue (" + typedValue + ").",
e.getCause());
}
});
}