private static Object callGetter(Object target, String propertyName) throws PropertyException { Method getter = getGetter(target, propertyName); try { return getter.invoke(target); } catch (IllegalAccessException ex) { throw new PropertyException("property '" + propertyName + "' is not accessible", ex); } catch (InvocationTargetException ex) { throw new PropertyException("property '" + propertyName + "' access threw an exception", ex); } }
public void testSetProperties() throws Exception { Destination destination = new Destination(); PropertyUtils.setProperty(destination, "props.key", "value"); assertEquals("value", destination.getProps().getProperty("key")); PropertyUtils.setProperty(destination, "subDestination.props.key", "value"); assertEquals("value", destination.getSubDestination().getProps().getProperty("key")); PropertyUtils.setProperty(destination, "anInteger", "10"); assertEquals(10, destination.getAnInteger()); PropertyUtils.setProperty(destination, "subDestination.anInteger", "20"); assertEquals(20, destination.getSubDestination().getAnInteger()); PropertyUtils.setProperty(destination, "aBoolean", "true"); assertEquals(true, destination.isABoolean()); PropertyUtils.setProperty(destination, "aWriteOnlyInt", "20"); PrivateDestination privateDestination = new PrivateDestination(); try { PropertyUtils.setProperty(privateDestination, "subDestination.props.key", "value"); fail("it is not possible to set the 'subDestination' property, PropertyException should have been thrown"); } catch (PropertyException ex) { assertEquals("cannot set property 'subDestination.props.key' - 'subDestination' is null and cannot be auto-filled", ex.getMessage()); } }
public void testSetProperties() throws Exception { Destination destination = new Destination(); PropertyUtils.setProperty(destination, "props.key", "value"); assertEquals("value", destination.getProps().getProperty("key")); PropertyUtils.setProperty(destination, "subDestination.props.key", "value"); assertEquals("value", destination.getSubDestination().getProps().getProperty("key")); PropertyUtils.setProperty(destination, "anInteger", "10"); assertEquals(10, destination.getAnInteger()); PropertyUtils.setProperty(destination, "subDestination.anInteger", "20"); assertEquals(20, destination.getSubDestination().getAnInteger()); PropertyUtils.setProperty(destination, "aBoolean", "true"); assertEquals(true, destination.isABoolean()); PropertyUtils.setProperty(destination, "aWriteOnlyInt", "20"); PrivateDestination privateDestination = new PrivateDestination(); try { PropertyUtils.setProperty(privateDestination, "subDestination.props.key", "value"); fail("it is not possible to set the 'subDestination' property, PropertyException should have been thrown"); } catch (PropertyException ex) { assertEquals("cannot set property 'subDestination.props.key' - 'subDestination' is null and cannot be auto-filled", ex.getMessage()); } }
private static void callSetter(Object target, String propertyName, Object parameter) throws PropertyException { Method setter = getSetter(target, propertyName); try { setter.invoke(target, parameter); } catch (IllegalAccessException ex) { throw new PropertyException("property '" + propertyName + "' is not accessible", ex); } catch (InvocationTargetException ex) { throw new PropertyException("property '" + propertyName + "' access threw an exception", ex); } }
private static Object callGetter(Object target, String propertyName) throws PropertyException { Method getter = getGetter(target, propertyName); try { return getter.invoke(target); } catch (IllegalAccessException ex) { throw new PropertyException("property '" + propertyName + "' is not accessible", ex); } catch (InvocationTargetException ex) { throw new PropertyException("property '" + propertyName + "' access threw an exception", ex); } }
private static void callSetter(Object target, String propertyName, Object parameter) throws PropertyException { Method setter = getSetter(target, propertyName); try { setter.invoke(target, new Object[] {parameter}); } catch (IllegalAccessException ex) { throw new PropertyException("property '" + propertyName + "' is not accessible", ex); } catch (InvocationTargetException ex) { throw new PropertyException("property '" + propertyName + "' access threw an exception", ex); } }
private static Object callGetter(Object target, String propertyName) throws PropertyException { Method getter = getGetter(target, propertyName); try { return getter.invoke(target, (Object[]) null); // casting to (Object[]) b/c of javac 1.5 warning } catch (IllegalAccessException ex) { throw new PropertyException("property '" + propertyName + "' is not accessible", ex); } catch (InvocationTargetException ex) { throw new PropertyException("property '" + propertyName + "' access threw an exception", ex); } }
private static void callSetter(Object target, String propertyName, Object parameter) throws PropertyException { Method setter = getSetter(target, propertyName); try { setter.invoke(target, parameter); } catch (IllegalAccessException ex) { throw new PropertyException("property '" + propertyName + "' is not accessible", ex); } catch (InvocationTargetException ex) { throw new PropertyException("property '" + propertyName + "' access threw an exception", ex); } }
private static void callSetter(Object target, String propertyName, Object parameter) throws PropertyException { Method setter = getSetter(target, propertyName); try { setter.invoke(target, new Object[] {parameter}); } catch (IllegalAccessException ex) { throw new PropertyException("property '" + propertyName + "' is not accessible", ex); } catch (InvocationTargetException ex) { throw new PropertyException("property '" + propertyName + "' access threw an exception", ex); } }
private static Object callGetter(Object target, String propertyName) throws PropertyException { Method getter = getGetter(target, propertyName); try { return getter.invoke(target, (Object[]) null); // casting to (Object[]) b/c of javac 1.5 warning } catch (IllegalAccessException ex) { throw new PropertyException("property '" + propertyName + "' is not accessible", ex); } catch (InvocationTargetException ex) { throw new PropertyException("property '" + propertyName + "' access threw an exception", ex); } }
private static Method getSetter(Object target, String propertyName) { if (propertyName == null || "".equals(propertyName)) { throw new PropertyException("encountered invalid null or empty property name"); } String setterName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (Method method : methods) { if (method.getName().equals(setterName) && method.getReturnType().equals(void.class) && method.getParameterTypes().length == 1) { return method; } } throw new PropertyException("no writeable property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }
private static Method getSetter(Object target, String propertyName) { if (propertyName == null || "".equals(propertyName)) throw new PropertyException("encountered invalid null or empty property name"); String setterName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().equals(setterName) && method.getReturnType().equals(void.class) && method.getParameterTypes().length == 1) { return method; } } throw new PropertyException("no writeable property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }
private static Method getSetter(Object target, String propertyName) { if (propertyName == null || "".equals(propertyName)) { throw new PropertyException("encountered invalid null or empty property name"); } String setterName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (Method method : methods) { if (method.getName().equals(setterName) && method.getReturnType().equals(void.class) && method.getParameterTypes().length == 1) { return method; } } throw new PropertyException("no writeable property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }
private static Method getSetter(Object target, String propertyName) { if (propertyName == null || "".equals(propertyName)) throw new PropertyException("encountered invalid null or empty property name"); String setterName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().equals(setterName) && method.getReturnType().equals(void.class) && method.getParameterTypes().length == 1) { return method; } } throw new PropertyException("no writeable property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }
private static Method getGetter(Object target, String propertyName) { String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); String getterIsName = "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (Method method : methods) { if ((method.getName().equals(getterName) || method.getName().equals(getterIsName)) && !method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0) { return method; } } throw new PropertyException("no readable property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }
private static Method getGetter(Object target, String propertyName) { String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); String getterIsName = "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if ((method.getName().equals(getterName) || method.getName().equals(getterIsName)) && !method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0) { return method; } } throw new PropertyException("no readable property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }
private static Class getPropertyType(Object target, String propertyName) { String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); String getterIsName = "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if ((method.getName().equals(getterName) || method.getName().equals(getterIsName)) && !method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0) { return method.getReturnType(); } } throw new PropertyException("no property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }
private static Class<?> getPropertyType(Object target, String propertyName) { String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); String getterIsName = "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (Method method : methods) { if ((method.getName().equals(getterName) || method.getName().equals(getterIsName)) && !method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0) { return method.getReturnType(); } } throw new PropertyException("no property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }
private static Class<?> getPropertyType(Object target, String propertyName) { String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); String getterIsName = "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (Method method : methods) { if ((method.getName().equals(getterName) || method.getName().equals(getterIsName)) && !method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0) { return method.getReturnType(); } } throw new PropertyException("no property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }
private static Class getPropertyType(Object target, String propertyName) { String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); String getterIsName = "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = target.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if ((method.getName().equals(getterName) || method.getName().equals(getterIsName)) && !method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0) { return method.getReturnType(); } } throw new PropertyException("no property '" + propertyName + "' in class '" + target.getClass().getName() + "'"); }