@Test public void testGetProperty() throws Exception { TestBean config = new TestBean(); config.setAlias("1111aaaa"); config.setHeartbeat(2000); config.setRegister(false); Assert.assertEquals(BeanUtils.getProperty(config, "alias", String.class), "1111aaaa"); Assert.assertTrue(BeanUtils.getProperty(config, "heartbeat", int.class) == 2000); Assert.assertTrue((Integer) BeanUtils.getProperty(config, "heartbeat", null) == 2000); Assert.assertFalse(BeanUtils.getProperty(config, "register", boolean.class)); boolean error = false; try { BeanUtils.getProperty(config, "xxx", String.class); } catch (Exception e) { error = true; } Assert.assertTrue(error); }
public static <T> T getProperty(Object bean, String name, Class<T> clazz) throws Exception { Method method = ReflectUtils.getPropertyGetterMethod(bean.getClass(), name); if (method.isAccessible()) { return (T) method.invoke(bean); } else { try { method.setAccessible(true); return (T) method.invoke(bean); } finally { method.setAccessible(false); } } }
BeanUtils { public static <T> T getProperty(Object bean, String name, Class<T> clazz) throws Exception { Method method = ReflectUtils.getPropertyGetterMethod(bean.getClass(), name); if (method.isAccessible()) { return (T) method.invoke(bean); } else { try { method.setAccessible(true); return (T) method.invoke(bean); } finally { method.setAccessible(false); } } } }
BeanUtils { public static <T> T getProperty(Object bean, String name, Class<T> clazz) throws Exception { Method method = ReflectUtils.getPropertyGetterMethod(bean.getClass(), name); if (method.isAccessible()) { return (T) method.invoke(bean); } else { try { method.setAccessible(true); return (T) method.invoke(bean); } finally { method.setAccessible(false); } } }  }
BeanUtils { public static <T> T getProperty(Object bean, String name, Class<T> clazz) throws Exception { Method method = ReflectUtils.getPropertyGetterMethod(bean.getClass(), name); if (method.isAccessible()) { return (T) method.invoke(bean); } else { try { method.setAccessible(true); return (T) method.invoke(bean); } finally { method.setAccessible(false); } } }  static void setProperty(Object bean, String name, Class<T> clazz, T value); static T getProperty(Object bean, String name, Class<T> clazz); static void copyPropertiesToMap(Object bean, String prefix, Map<String, Object> map); static void copyProperties(Object src, Object dst, String... ignoreFields); static List<String> getModifiedFields(T src, T dst, String... ignoreFields); }
BeanUtils { public static <T> T getProperty(Object bean, String name, Class<T> clazz) throws Exception { Method method = ReflectUtils.getPropertyGetterMethod(bean.getClass(), name); if (method.isAccessible()) { return (T) method.invoke(bean); } else { try { method.setAccessible(true); return (T) method.invoke(bean); } finally { method.setAccessible(false); } } }  static void setProperty(Object bean, String name, Class<T> clazz, T value); static T getProperty(Object bean, String name, Class<T> clazz); static void copyPropertiesToMap(Object bean, String prefix, Map<String, Object> map); static void copyProperties(Object src, Object dst, String... ignoreFields); static List<String> getModifiedFields(T src, T dst, String... ignoreFields);  }