@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()); }
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; }
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; } }
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; }  }
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); }
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);  }