Util { public static Method findSetter(Class<?> testClazz, Field field, Object propertyValue) { String propertyName = field.getName(); boolean isStatic = Modifier.isStatic(field.getModifiers()); String setterName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); List<Method> methods = new ArrayList<Method>(Arrays.asList(testClazz.getMethods())); methods.addAll(Arrays.asList(testClazz.getDeclaredMethods())); Method unpreferredValidMethod = null; for (Method clazzMethod : methods) { if (clazzMethod.getName().equals(setterName)) { if (clazzMethod.getParameterTypes().length == 0) { continue; } if (clazzMethod.getParameterTypes().length > 1) { continue; } if (clazzMethod.getReturnType() != Void.TYPE) { continue; } if (Modifier.isAbstract(clazzMethod.getModifiers())) { continue; } Class methodParameterType = clazzMethod.getParameterTypes()[0]; if (methodParameterType.isPrimitive() && propertyValue == null) { continue; } if (!isStatic && Modifier.isStatic(clazzMethod.getModifiers())) { continue; } if (!Modifier.isPublic(clazzMethod.getModifiers())) { clazzMethod.setAccessible(true); } if (!isInstance(methodParameterType, propertyValue)) { continue; } else if (methodParameterType != propertyValue.getClass()) { if (unpreferredValidMethod == null) { unpreferredValidMethod = clazzMethod; } else { unpreferredValidMethod = getMostSpecificMethod(unpreferredValidMethod, clazzMethod); } continue; } return clazzMethod; } } return unpreferredValidMethod; } private  Util(); static boolean isInstance(Class type, Object instance); static void addProperty(Hashtable env, String property); static Method findSetter(Class<?> testClazz, Field field, Object propertyValue); static Method getMostSpecificMethod(Method method1, Method method2);  }
@Test public void testFindSetterNoSetters() throws Exception { String fieldName = "noSetter"; String setterName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); Field field = getClass().getDeclaredField(fieldName); try { getClass().getDeclaredMethod(setterName, String.class); } catch (NoSuchMethodException e) { } Method method = Util.findSetter(getClass(), field, "ValueString"); assertNull("Setter found.", method); }
@Test public void testFindSetterAbstractSetter() throws Exception { String fieldName = "abstractSetter"; String setterName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); Field field = AbstractClass.class.getDeclaredField(fieldName); AbstractClass.class.getDeclaredMethod(setterName, String.class); Method method = Util.findSetter(AbstractClass.class, field, "ValueString"); assertNull("Abstract setter returned.", method); }
@Test public void testFindSetterDualValidSetters() throws Exception { String fieldName = "twoSetter"; Field field = getClass().getDeclaredField(fieldName); String setterName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); getClass().getDeclaredMethod(setterName, String.class, String.class); Method setterMethod = getClass().getDeclaredMethod(setterName, String.class); Method ret = Util.findSetter(getClass(), field, "ValueString"); assertNotNull("Couldn't find setter.", ret); assertEquals(setterMethod, ret); }
@Test public void testFindSetterBestMatchSetters() throws Exception { String fieldName = "twoAccessibleSetter"; Field field = getClass().getDeclaredField(fieldName); String setterName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); getClass().getDeclaredMethod(setterName, Object.class); Method setterMethod = getClass().getDeclaredMethod(setterName, String.class); Method ret = Util.findSetter(getClass(), field, "ValueString"); assertNotNull("Couldn't find setter.", ret); assertEquals(setterMethod, ret); }
@Test public void testFindSetterThreeAccessibleNoExactMatchSetter() throws Exception { String fieldName = "threeAccessibleNoExactMatchSetter"; Field field = getClass().getDeclaredField(fieldName); String setterName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); getClass().getDeclaredMethod(setterName, Object.class); getClass().getDeclaredMethod(setterName, Throwable.class); Method setterMethod = getClass().getDeclaredMethod(setterName, Exception.class); Method ret = Util.findSetter(getClass(), field, new NumberFormatException()); assertNotNull("Couldn't find setter.", ret); assertEquals(setterMethod, ret); }