@Test public void testBadSetterMethod() { BeanProperty<Truck> painter = new BeanProperty<>(Truck.class, "towedVehicle.color", true, true); Truck truck = new Truck(2); truck.setTowedVehicle(new Automobile(true)); try { painter.set(truck, "this should break"); } catch (IllegalArgumentException e) { assertEquals("Automobile.setColor(Color) cannot be called with an instance of String", e.getMessage()); } }
public Object set(T member, Object newValue) { if(!isWritable()) throw new IllegalStateException("Property " + propertyName + " of " + beanClass + " not writable"); Method setterMethod = null; try { Object currentMember = member; for(int i = 0, n = setterChain.size() - 1; i < n; i++) { Method currentMethod = setterChain.get(i); currentMember = currentMethod.invoke(currentMember, EMPTY_ARGUMENTS); if(currentMember == null) return null; } setterMethod = setterChain.get(setterChain.size() - 1); return setterMethod.invoke(currentMember, new Object[] { newValue }); } catch (IllegalArgumentException e) { String message = e.getMessage(); if ("argument type mismatch".equals(message) && setterMethod != null) message = getSimpleName(setterMethod.getDeclaringClass()) + "." + setterMethod.getName() + "(" + getSimpleName(setterMethod.getParameterTypes()[0]) + ") cannot be called with an instance of " + getSimpleName(newValue.getClass()); throw new IllegalArgumentException(message); } catch(IllegalAccessException e) { SecurityException se = new SecurityException(); se.initCause(e); throw se; } catch(InvocationTargetException e) { throw new UndeclaredThrowableException(e.getCause()); } catch(RuntimeException e) { throw new RuntimeException("Failed to set property \"" + propertyName + "\" of " + beanClass + " to " + (newValue == null ? "null" : "instance of " + newValue.getClass()), e); } }
BeanProperty { public Object set(T member, Object newValue) { if(!isWritable()) throw new IllegalStateException("Property " + propertyName + " of " + beanClass + " not writable"); Method setterMethod = null; try { Object currentMember = member; for(int i = 0, n = setterChain.size() - 1; i < n; i++) { Method currentMethod = setterChain.get(i); currentMember = currentMethod.invoke(currentMember, EMPTY_ARGUMENTS); if(currentMember == null) return null; } setterMethod = setterChain.get(setterChain.size() - 1); return setterMethod.invoke(currentMember, new Object[] { newValue }); } catch (IllegalArgumentException e) { String message = e.getMessage(); if ("argument type mismatch".equals(message) && setterMethod != null) message = getSimpleName(setterMethod.getDeclaringClass()) + "." + setterMethod.getName() + "(" + getSimpleName(setterMethod.getParameterTypes()[0]) + ") cannot be called with an instance of " + getSimpleName(newValue.getClass()); throw new IllegalArgumentException(message); } catch(IllegalAccessException e) { SecurityException se = new SecurityException(); se.initCause(e); throw se; } catch(InvocationTargetException e) { throw new UndeclaredThrowableException(e.getCause()); } catch(RuntimeException e) { throw new RuntimeException("Failed to set property \"" + propertyName + "\" of " + beanClass + " to " + (newValue == null ? "null" : "instance of " + newValue.getClass()), e); } } }
BeanProperty { public Object set(T member, Object newValue) { if(!isWritable()) throw new IllegalStateException("Property " + propertyName + " of " + beanClass + " not writable"); Method setterMethod = null; try { Object currentMember = member; for(int i = 0, n = setterChain.size() - 1; i < n; i++) { Method currentMethod = setterChain.get(i); currentMember = currentMethod.invoke(currentMember, EMPTY_ARGUMENTS); if(currentMember == null) return null; } setterMethod = setterChain.get(setterChain.size() - 1); return setterMethod.invoke(currentMember, new Object[] { newValue }); } catch (IllegalArgumentException e) { String message = e.getMessage(); if ("argument type mismatch".equals(message) && setterMethod != null) message = getSimpleName(setterMethod.getDeclaringClass()) + "." + setterMethod.getName() + "(" + getSimpleName(setterMethod.getParameterTypes()[0]) + ") cannot be called with an instance of " + getSimpleName(newValue.getClass()); throw new IllegalArgumentException(message); } catch(IllegalAccessException e) { SecurityException se = new SecurityException(); se.initCause(e); throw se; } catch(InvocationTargetException e) { throw new UndeclaredThrowableException(e.getCause()); } catch(RuntimeException e) { throw new RuntimeException("Failed to set property \"" + propertyName + "\" of " + beanClass + " to " + (newValue == null ? "null" : "instance of " + newValue.getClass()), e); } } BeanProperty(Class<T> beanClass, String propertyName, boolean readable, boolean writable); }
BeanProperty { public Object set(T member, Object newValue) { if(!isWritable()) throw new IllegalStateException("Property " + propertyName + " of " + beanClass + " not writable"); Method setterMethod = null; try { Object currentMember = member; for(int i = 0, n = setterChain.size() - 1; i < n; i++) { Method currentMethod = setterChain.get(i); currentMember = currentMethod.invoke(currentMember, EMPTY_ARGUMENTS); if(currentMember == null) return null; } setterMethod = setterChain.get(setterChain.size() - 1); return setterMethod.invoke(currentMember, new Object[] { newValue }); } catch (IllegalArgumentException e) { String message = e.getMessage(); if ("argument type mismatch".equals(message) && setterMethod != null) message = getSimpleName(setterMethod.getDeclaringClass()) + "." + setterMethod.getName() + "(" + getSimpleName(setterMethod.getParameterTypes()[0]) + ") cannot be called with an instance of " + getSimpleName(newValue.getClass()); throw new IllegalArgumentException(message); } catch(IllegalAccessException e) { SecurityException se = new SecurityException(); se.initCause(e); throw se; } catch(InvocationTargetException e) { throw new UndeclaredThrowableException(e.getCause()); } catch(RuntimeException e) { throw new RuntimeException("Failed to set property \"" + propertyName + "\" of " + beanClass + " to " + (newValue == null ? "null" : "instance of " + newValue.getClass()), e); } } BeanProperty(Class<T> beanClass, String propertyName, boolean readable, boolean writable); Class<T> getBeanClass(); String getPropertyName(); Class getValueClass(); boolean isReadable(); boolean isWritable(); Object get(T member); Object set(T member, Object newValue); @Override boolean equals(Object o); @Override int hashCode(); }
BeanProperty { public Object set(T member, Object newValue) { if(!isWritable()) throw new IllegalStateException("Property " + propertyName + " of " + beanClass + " not writable"); Method setterMethod = null; try { Object currentMember = member; for(int i = 0, n = setterChain.size() - 1; i < n; i++) { Method currentMethod = setterChain.get(i); currentMember = currentMethod.invoke(currentMember, EMPTY_ARGUMENTS); if(currentMember == null) return null; } setterMethod = setterChain.get(setterChain.size() - 1); return setterMethod.invoke(currentMember, new Object[] { newValue }); } catch (IllegalArgumentException e) { String message = e.getMessage(); if ("argument type mismatch".equals(message) && setterMethod != null) message = getSimpleName(setterMethod.getDeclaringClass()) + "." + setterMethod.getName() + "(" + getSimpleName(setterMethod.getParameterTypes()[0]) + ") cannot be called with an instance of " + getSimpleName(newValue.getClass()); throw new IllegalArgumentException(message); } catch(IllegalAccessException e) { SecurityException se = new SecurityException(); se.initCause(e); throw se; } catch(InvocationTargetException e) { throw new UndeclaredThrowableException(e.getCause()); } catch(RuntimeException e) { throw new RuntimeException("Failed to set property \"" + propertyName + "\" of " + beanClass + " to " + (newValue == null ? "null" : "instance of " + newValue.getClass()), e); } } BeanProperty(Class<T> beanClass, String propertyName, boolean readable, boolean writable); Class<T> getBeanClass(); String getPropertyName(); Class getValueClass(); boolean isReadable(); boolean isWritable(); Object get(T member); Object set(T member, Object newValue); @Override boolean equals(Object o); @Override int hashCode();  }