FieldUtils { public static Object getValue(Object object, Field field) throws LoaderException { if (Modifier.isPublic(field.getModifiers())) { return getByField(object, field); } else { return getByGetter(object, field); } } private  FieldUtils(); static Object getValue(Object object, Field field); static void setValue(Object object, Field field, Object value);  }
@Test public void getPublic() throws Exception { TestClass entity = new TestClass(); entity.publicValue = "value"; Field field = TestClass.class.getDeclaredField("publicValue"); String actualValue = (String)FieldUtils.getValue(entity, field); Assert.assertEquals(entity.publicValue, actualValue); }
@Test(expected = LoaderException.class) public void getFailed() throws Exception { TestClass entity = new TestClass(); Field field = TestClass.class.getDeclaredField("inaccessible"); FieldUtils.getValue(entity, field); }