PropertyBinding implements Binding<T> { @Override public void setValue(T value) { try { PropertyUtils.setSimpleProperty(boundObject, property, value); } catch (Exception e) { throw new IllegalStateException(String.format( "Binding error! Setting property '%s' on bound object of class %s failed!", property, boundObject.getClass()), e); } } PropertyBinding(Object boundObject, String property); @Override void setValue(T value); @Override @SuppressWarnings("unchecked") T getValue();  }
@Test public void test() { Person person = new Person(); Binding<String> nameBinding = new PropertyBinding<String>(person, "name"); Binding<Integer> ageBinding = new PropertyBinding<Integer>(person, "age"); nameBinding.setValue("Tom"); ageBinding.setValue(30); Assert.assertEquals("Tom", person.getName()); Assert.assertEquals(30, person.getAge()); }
@Test(expected = IllegalStateException.class) public void testWrongType() { Person person = new Person(); Binding<String> nameBinding = new PropertyBinding<String>(person, "age"); nameBinding.setValue("Tom"); }