Source:
private static void setProperty(final Object target, final String propName, final Object propValue, final List<Method> methods) { String methodName = "set" + propName.substring(0, 1).toUpperCase(Locale.ENGLISH) + propName.substring(1); Method writeMethod = methods.stream().filter(m -> m.getName().equals(methodName) && m.getParameterCount() == 1).findFirst().orElse(null); if (writeMethod == null) { String methodName2 = "set" + propName.toUpperCase(Locale.ENGLISH); writeMethod = methods.stream().filter(m -> m.getName().equals(methodName2) && m.getParameterCount() == 1).findFirst().orElse(null); } if (writeMethod == null) { LOGGER.error("Property does not exist on target {}", propName, target.getClass()); throw new RuntimeException(String.format("Property %s does not exist on target %s", propName, target.getClass())); } try { Class<?> paramClass = writeMethod.getParameterTypes()[0]; if (paramClass == int.class) { writeMethod.invoke(target, Integer.parseInt(propValue.toString())); } else if (paramClass == long.class) { writeMethod.invoke(target, Long.parseLong(propValue.toString())); } else if (paramClass == boolean.class || paramClass == Boolean.class) { writeMethod.invoke(target, Boolean.parseBoolean(propValue.toString())); } else if (paramClass == String.class) { writeMethod.invoke(target, propValue.toString()); } else { try { LOGGER.info("Try to create a new instance of \"{}\"", propValue.toString()); writeMethod.invoke(target, Class.forName(propValue.toString()).newInstance()); }catch(InstantiationException | ClassNotFoundException e){ LOGGER.info("Class \"{}\" not found or could not instantiate it (Default constructor)", propValue.toString()); writeMethod.invoke(target, propValue); } } } catch (Exception e) { LOGGER.error("Failed to set property on target {}", propName, target.getClass(), e); throw new RuntimeException(e); } }

Target:
private static void setProperty(final Object target, final String propName, final Object propValue, final List<Method> methods) { String methodName = "set" + propName.substring(0, 1).toUpperCase(Locale.ENGLISH) + propName.substring(1); Method writeMethod = methods.stream().filter(m -> m.getName().equals(methodName) && m.getParameterCount() == 1).findFirst().orElse(null); if (writeMethod == null) { String methodName2 = "set" + propName.toUpperCase(Locale.ENGLISH); writeMethod = methods.stream().filter(m -> m.getName().equals(methodName2) && m.getParameterCount() == 1).findFirst().orElse(null); } if (writeMethod == null) { LOGGER.error("Property does not exist on target {}", propName, target.getClass()); throw new RuntimeException(String.format("Property %s does not exist on target %s", propName, target.getClass())); } try { Class<?> paramClass = writeMethod.getParameterTypes()[0]; if (paramClass == int.class) { writeMethod.invoke(target, Integer.parseInt(propValue.toString())); } else if (paramClass == long.class) { writeMethod.invoke(target, Long.parseLong(propValue.toString())); } else if (paramClass == boolean.class || paramClass == Boolean.class) { writeMethod.invoke(target, Boolean.parseBoolean(propValue.toString())); } else if (paramClass == String.class) { writeMethod.invoke(target, propValue.toString()); } else { try { LOGGER.debug("Try to create a new instance of \"{}\"", propValue.toString()); writeMethod.invoke(target, Class.forName(propValue.toString()).newInstance()); } catch (InstantiationException | ClassNotFoundException e) { LOGGER.debug("Class \"{}\" not found or could not instantiate it (Default constructor)", propValue.toString()); writeMethod.invoke(target, propValue); } } } catch (Exception e) { LOGGER.error("Failed to set property on target {}", propName, target.getClass(), e); throw new RuntimeException(e); } }