BeanUtil { public static BeanProperty findBeanProperty(Class type, String propertyName, boolean caseSensitive) throws IllegalAccessException { BeanProperty beanProperty = null; Class classType = type; while (classType != null && !classType.equals(Object.class)) { for (Field f : classType.getDeclaredFields()) { String fieldName = f.getName(); if ((!caseSensitive && fieldName.equalsIgnoreCase(propertyName)) || (caseSensitive && fieldName.equals(propertyName))) { Class fieldType = f.getType(); f.setAccessible(true); beanProperty = new BeanProperty(propertyName, fieldType, f); break; } } classType = classType.getSuperclass(); } String capitalizedName = StringUtil.capitalize(propertyName); String getMethodName = "get" + capitalizedName; String setMethodName = "set" + capitalizedName; String addMethodName = "add" + capitalizedName; if (beanProperty == null) { classType = type; while (classType != null && !classType.equals(Object.class)) { for (Method m : classType.getDeclaredMethods()) { String methodName = m.getName(); if ((!caseSensitive && methodName.equalsIgnoreCase(getMethodName)) || (caseSensitive && methodName.equals(getMethodName))) { if (m.getParameterTypes().length == 0) { beanProperty = new BeanProperty(propertyName, m.getReturnType(), null); m.setAccessible(true); beanProperty.getMethod = m; break; } } else if ((!caseSensitive && methodName.equalsIgnoreCase(setMethodName)) || (caseSensitive && methodName.equals(setMethodName))) { if (m.getParameterTypes().length == 1) { beanProperty = new BeanProperty(propertyName, m.getParameterTypes()[0], null); m.setAccessible(true); beanProperty.setMethod = m; break; } } else if ((!caseSensitive && methodName.equalsIgnoreCase(addMethodName)) || (caseSensitive && methodName.equals(addMethodName))) { if (m.getParameterTypes().length == 1) { beanProperty = new BeanProperty(propertyName, m.getParameterTypes()[0], null); m.setAccessible(true); beanProperty.addMethod = m; break; } } } classType = classType.getSuperclass(); } } if (beanProperty != null) { if (beanProperty.getMethod == null) { try { Method getMethod = ClassUtil.getMethod(type, getMethodName, beanProperty.getType(), null, caseSensitive); getMethod.setAccessible(true); beanProperty.getMethod = getMethod; } catch (NoSuchMethodException e) { } } if (beanProperty.setMethod == null) { try { Method setMethod = ClassUtil.getMethod(type, setMethodName, null, beanProperty.getType(), caseSensitive); setMethod.setAccessible(true); beanProperty.setMethod = setMethod; } catch (NoSuchMethodException e) { } } if (beanProperty.addMethod == null) { try { Method addMethod = ClassUtil.getMethod(type, addMethodName, null, beanProperty.getType(), caseSensitive); addMethod.setAccessible(true); beanProperty.addMethod = addMethod; } catch (NoSuchMethodException e) { } } } return beanProperty; }  static BeanProperty findBeanProperty(Class type, String propertyName, boolean caseSensitive);  }
@Test public void getBeanProperty() throws Exception { InternetPerson person = new InternetPerson(); Assert.assertNull(person.getFirstName()); Assert.assertNull(person.getLastName()); Assert.assertEquals(-1, person.getId()); BeanProperty beanProperty = BeanUtil.findBeanProperty(InternetPerson.class, "firstName", true); Assert.assertEquals(Person.class.getDeclaredField("firstName"), beanProperty.getField()); Assert.assertEquals(String.class, beanProperty.getType()); Assert.assertEquals("firstName", beanProperty.getName()); beanProperty.set(person, "Joe"); Assert.assertEquals("Joe", beanProperty.get(person)); Assert.assertEquals("Joe", person.getFirstName()); BeanProperty beanProperty2 = BeanUtil.findBeanProperty(InternetPerson.class, "extraInfo", true); Assert.assertEquals(null, beanProperty2.get(person)); beanProperty2.set(person, "MoreInfoHere"); Assert.assertEquals("MoreInfoHere", beanProperty2.get(person)); ServerConfig config = new ServerConfig(); BeanProperty beanProperty3 = BeanUtil.findBeanProperty(config.getClass(), "port", true); try { beanProperty3.set(config, "80"); Assert.fail("String arg should have thrown exception"); } catch (IllegalArgumentException e) { } try { beanProperty3.set(config, -80); Assert.fail("negative port arg should have thrown exception"); } catch (InvocationTargetException e) { Assert.assertEquals(ServerConfigException.class, e.getCause().getClass()); } beanProperty3.set(config, 80); Assert.assertEquals(80, beanProperty3.get(config)); }
@Test public void getBeanPropertyOnlyGetter() throws Exception { TestBeanA bean = new TestBeanA(); BeanProperty property = BeanUtil.findBeanProperty(bean.getClass(), "firstName", true); Assert.assertEquals(String.class, property.getType()); Assert.assertEquals("firstName", property.getName()); Assert.assertEquals(true, property.canGet()); Assert.assertEquals(false, property.canSet()); Assert.assertEquals(false, property.canAdd()); Assert.assertEquals(null, property.getField()); Assert.assertEquals(TestBeanA.class.getDeclaredMethod("getFirstName"), property.getGetMethod()); Assert.assertEquals(null, property.getSetMethod()); }
@Test public void getBeanPropertyOnlySetter() throws Exception { TestBeanB bean = new TestBeanB(); BeanProperty property = BeanUtil.findBeanProperty(bean.getClass(), "firstName", true); Assert.assertEquals(String.class, property.getType()); Assert.assertEquals("firstName", property.getName()); Assert.assertEquals(false, property.canGet()); Assert.assertEquals(true, property.canSet()); Assert.assertEquals(false, property.canAdd()); Assert.assertEquals(null, property.getField()); Assert.assertEquals(TestBeanB.class.getDeclaredMethod("setFirstName", String.class), property.getSetMethod()); Assert.assertEquals(null, property.getGetMethod()); }
@Test public void getBeanPropertyOnlyGetterAndSetter() throws Exception { TestBeanC bean = new TestBeanC(); BeanProperty property = BeanUtil.findBeanProperty(bean.getClass(), "firstName", true); Assert.assertEquals(String.class, property.getType()); Assert.assertEquals("firstName", property.getName()); Assert.assertEquals(true, property.canGet()); Assert.assertEquals(true, property.canSet()); Assert.assertEquals(false, property.canAdd()); Assert.assertEquals(null, property.getField()); Assert.assertEquals(TestBeanC.class.getDeclaredMethod("setFirstName", String.class), property.getSetMethod()); Assert.assertEquals(TestBeanC.class.getDeclaredMethod("getFirstName"), property.getGetMethod()); }
@Test public void getBeanPropertyOnlyAdder() throws Exception { TestBeanD bean = new TestBeanD(); BeanProperty property = BeanUtil.findBeanProperty(bean.getClass(), "host", true); Assert.assertEquals(String.class, property.getType()); Assert.assertEquals("host", property.getName()); Assert.assertEquals(false, property.canGet()); Assert.assertEquals(false, property.canSet()); Assert.assertEquals(true, property.canAdd()); Assert.assertEquals(null, property.getField()); Assert.assertEquals(TestBeanD.class.getDeclaredMethod("addHost", String.class), property.getAddMethod()); Assert.assertEquals(null, property.getSetMethod()); Assert.assertEquals(null, property.getGetMethod()); }
@Test public void getBeanPropertyAddOrSet() throws Exception { TestBeanE bean = new TestBeanE(); BeanProperty property = BeanUtil.findBeanProperty(bean.getClass(), "host", true); Assert.assertEquals(String.class, property.getType()); Assert.assertEquals("host", property.getName()); Assert.assertEquals(false, property.canGet()); Assert.assertEquals(true, property.canSet()); Assert.assertEquals(true, property.canAdd()); Assert.assertEquals(true, property.canAddOrSet()); Assert.assertEquals(null, property.getField()); Assert.assertEquals(TestBeanE.class.getDeclaredMethod("addHost", String.class), property.getAddMethod()); Assert.assertEquals(TestBeanE.class.getDeclaredMethod("setHost", String.class), property.getSetMethod()); Assert.assertEquals(null, property.getGetMethod()); property.addOrSet(bean, "www.google.com"); Assert.assertEquals("www.google.com", bean.addhost); Assert.assertEquals(null, bean.sethost); }