PropertyUtils { public static Object getPropertyValue(Object context, String property) { final String[] chain = property.split("\\."); final List<Object> accessors = new ArrayList<>(); Class<?> type = context.getClass(); final StringBuilder path = new StringBuilder(); for (String current : chain) { if (path.length() > 0) { path.append('.'); } path.append(current); final Method getter = ReflectionUtils.findMethod(type, "get" + StringUtils.capitalize(current)); if (getter != null) { accessors.add(getter); type = getter.getReturnType(); } else { final Field field = ReflectionUtils.findField(type, current); if (field != null) { field.setAccessible(true); accessors.add(field); type = field.getType(); } else { throw new IllegalStateException( "Cannot find property `" + path + "` on type `" + context.getClass() + "`"); } } } Object result = context; for (Object accessor : accessors) { try { if (accessor instanceof Method) { final Method method = (Method) accessor; result = method.invoke(result); } else { final Field field = (Field) accessor; result = field.get(result); } } catch (Exception e) { throw new IllegalStateException("Failed to access property `" + property + "`", e); } if (result == null) { return null; } } return result; } private  PropertyUtils(); static Object getPropertyValue(Object context, String property); static Object setPropertyValue(Object context, String property, Object value); static PropertyDescriptor getPropertyDescriptor(Class<?> domainType, String expression); static Class<?> getTypeOf(Class<?> type); static String getPropertyName(Method getter);  }
@Test public void testReadingPropertyValue() { final Person person = new Person(); person.setAddress(new Address()); person.getAddress().setCity("Tehran"); person.getAddress().setState(new State()); person.getAddress().getState().setName("Teheran"); person.getAddress().getState().setAbbreviation("TEH"); assertThat(PropertyUtils.getPropertyValue(person, "address"), Matchers.is(person.getAddress())); assertThat(PropertyUtils.getPropertyValue(person, "address.city"), Matchers.is(person.getAddress().getCity())); assertThat(PropertyUtils.getPropertyValue(person, "address.state.name"), Matchers.is(person.getAddress().getState().getName())); assertThat(PropertyUtils.getPropertyValue(person, "address.state.abbreviation"), Matchers.is(person.getAddress().getState().getAbbreviation())); assertThat(PropertyUtils.getPropertyValue(person, "firstName"), is(nullValue())); }
@Test public void testReadingPropertyValueThroughField() throws Exception { final ClassWithNoGetters object = new ClassWithNoGetters(); final Field id = Objects.requireNonNull(ReflectionUtils.findField(ClassWithNoGetters.class, "id")); id.setAccessible(true); id.set(object, "1234"); assertThat(PropertyUtils.getPropertyValue(object, "id"), is(id.get(object))); }
@Test(expectedExceptions = IllegalStateException.class) public void testReadingPropertyValueThroughErrorThrowingGetter() { final ClassWithErrorThrowingAccessors object = new ClassWithErrorThrowingAccessors(); PropertyUtils.getPropertyValue(object, "id"); }
@Test(expectedExceptions = IllegalStateException.class) public void testReadingNonExistentProperty() { final Person person = new Person(); PropertyUtils.getPropertyValue(person, "address.xyz"); }
@Test public void testReadingWhenMiddlePropertyIsNull() { assertThat(PropertyUtils.getPropertyValue(new Person().setAddress(new Address()), "address.zip.prefix"), is(nullValue())); }