@Test public void testSetPropertyToNull() throws Exception { Options configObject = new Options(); configObject.setFirstName("foo"); assertTrue(PropertyUtil.setProperty(configObject, "firstName", null)); assertNull(configObject.getFirstName()); }
public static boolean setProperty(Object target, String name, Object value) { try { int dotPos = name.indexOf("."); while (dotPos >= 0) { String getterName = name.substring(0, dotPos); target = getProperty(target, getterName); name = name.substring(dotPos + 1); dotPos = name.indexOf("."); } Class<? extends Object> clazz = target.getClass(); List<Method> setters = findSetterMethod(clazz, name); if (setters == null || setters.isEmpty()) { return false; } Throwable failure = null; for (Method setter : setters) { if (value == null || value.getClass() == setter.getParameterTypes()[0]) { try { setter.invoke(target, new Object[] { value }); failure = null; break; } catch (Throwable error) { failure = error; } } else { try { setter.invoke(target, new Object[] { convert(value, setter.getParameterTypes()[0]) }); failure = null; break; } catch (Throwable error) { failure = error; } } } return failure == null; } catch (Throwable ignore) { return false; } }
PropertyUtil { public static boolean setProperty(Object target, String name, Object value) { try { int dotPos = name.indexOf("."); while (dotPos >= 0) { String getterName = name.substring(0, dotPos); target = getProperty(target, getterName); name = name.substring(dotPos + 1); dotPos = name.indexOf("."); } Class<? extends Object> clazz = target.getClass(); List<Method> setters = findSetterMethod(clazz, name); if (setters == null || setters.isEmpty()) { return false; } Throwable failure = null; for (Method setter : setters) { if (value == null || value.getClass() == setter.getParameterTypes()[0]) { try { setter.invoke(target, new Object[] { value }); failure = null; break; } catch (Throwable error) { failure = error; } } else { try { setter.invoke(target, new Object[] { convert(value, setter.getParameterTypes()[0]) }); failure = null; break; } catch (Throwable error) { failure = error; } } } return failure == null; } catch (Throwable ignore) { return false; } } }
PropertyUtil { public static boolean setProperty(Object target, String name, Object value) { try { int dotPos = name.indexOf("."); while (dotPos >= 0) { String getterName = name.substring(0, dotPos); target = getProperty(target, getterName); name = name.substring(dotPos + 1); dotPos = name.indexOf("."); } Class<? extends Object> clazz = target.getClass(); List<Method> setters = findSetterMethod(clazz, name); if (setters == null || setters.isEmpty()) { return false; } Throwable failure = null; for (Method setter : setters) { if (value == null || value.getClass() == setter.getParameterTypes()[0]) { try { setter.invoke(target, new Object[] { value }); failure = null; break; } catch (Throwable error) { failure = error; } } else { try { setter.invoke(target, new Object[] { convert(value, setter.getParameterTypes()[0]) }); failure = null; break; } catch (Throwable error) { failure = error; } } } return failure == null; } catch (Throwable ignore) { return false; } }  }
PropertyUtil { public static boolean setProperty(Object target, String name, Object value) { try { int dotPos = name.indexOf("."); while (dotPos >= 0) { String getterName = name.substring(0, dotPos); target = getProperty(target, getterName); name = name.substring(dotPos + 1); dotPos = name.indexOf("."); } Class<? extends Object> clazz = target.getClass(); List<Method> setters = findSetterMethod(clazz, name); if (setters == null || setters.isEmpty()) { return false; } Throwable failure = null; for (Method setter : setters) { if (value == null || value.getClass() == setter.getParameterTypes()[0]) { try { setter.invoke(target, new Object[] { value }); failure = null; break; } catch (Throwable error) { failure = error; } } else { try { setter.invoke(target, new Object[] { convert(value, setter.getParameterTypes()[0]) }); failure = null; break; } catch (Throwable error) { failure = error; } } } return failure == null; } catch (Throwable ignore) { return false; } }  static URI replaceQuery(URI originalURI, Map<String, String> properties); static URI replaceQuery(URI uri, String query); static URI eraseQuery(URI uri); static String createQueryString(Map<String, ? extends Object> options); static Map<String, String> parseQuery(URI uri); static Map<String, String> parseQuery(String queryString); static Map<String, String> filterProperties(Map<String, String> properties, String optionPrefix); static Map<String, String> setProperties(Object target, Map<String, String> properties); static Map<String, Object> setProperties(Object target, Properties properties); static Map<String, String> getProperties(Object object); static Object getProperty(Object object, String name); static boolean setProperty(Object target, String name, Object value); static String stripPrefix(String value, String prefix); static String stripUpto(String value, char c); static String stripBefore(String value, char c); }
PropertyUtil { public static boolean setProperty(Object target, String name, Object value) { try { int dotPos = name.indexOf("."); while (dotPos >= 0) { String getterName = name.substring(0, dotPos); target = getProperty(target, getterName); name = name.substring(dotPos + 1); dotPos = name.indexOf("."); } Class<? extends Object> clazz = target.getClass(); List<Method> setters = findSetterMethod(clazz, name); if (setters == null || setters.isEmpty()) { return false; } Throwable failure = null; for (Method setter : setters) { if (value == null || value.getClass() == setter.getParameterTypes()[0]) { try { setter.invoke(target, new Object[] { value }); failure = null; break; } catch (Throwable error) { failure = error; } } else { try { setter.invoke(target, new Object[] { convert(value, setter.getParameterTypes()[0]) }); failure = null; break; } catch (Throwable error) { failure = error; } } } return failure == null; } catch (Throwable ignore) { return false; } }  static URI replaceQuery(URI originalURI, Map<String, String> properties); static URI replaceQuery(URI uri, String query); static URI eraseQuery(URI uri); static String createQueryString(Map<String, ? extends Object> options); static Map<String, String> parseQuery(URI uri); static Map<String, String> parseQuery(String queryString); static Map<String, String> filterProperties(Map<String, String> properties, String optionPrefix); static Map<String, String> setProperties(Object target, Map<String, String> properties); static Map<String, Object> setProperties(Object target, Properties properties); static Map<String, String> getProperties(Object object); static Object getProperty(Object object, String name); static boolean setProperty(Object target, String name, Object value); static String stripPrefix(String value, String prefix); static String stripUpto(String value, char c); static String stripBefore(String value, char c);  }