@Test public void testFindSetterAbstractSetter() throws Exception { final String fieldName = "abstractSetter"; final String setterName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); final Field field = AbstractClass.class.getDeclaredField(fieldName); AbstractClass.class.getDeclaredMethod(setterName, String.class); final Method method = Util.findSetter(AbstractClass.class, field, "ValueString"); assertNull("Abstract setter returned.", method); }
public static Method findSetter(final Class<?> testClazz, final Field field, final Object propertyValue) { final String propertyName = field.getName(); final boolean isStatic = Modifier.isStatic(field.getModifiers()); final String setterName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); final List<Method> methods = new ArrayList<Method>(Arrays.asList(testClazz.getMethods())); methods.addAll(Arrays.asList(testClazz.getDeclaredMethods())); Method unpreferredValidMethod = null; for (final 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; } final 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; }
Util { public static Method findSetter(final Class<?> testClazz, final Field field, final Object propertyValue) { final String propertyName = field.getName(); final boolean isStatic = Modifier.isStatic(field.getModifiers()); final String setterName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); final List<Method> methods = new ArrayList<Method>(Arrays.asList(testClazz.getMethods())); methods.addAll(Arrays.asList(testClazz.getDeclaredMethods())); Method unpreferredValidMethod = null; for (final 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; } final 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; } }
Util { public static Method findSetter(final Class<?> testClazz, final Field field, final Object propertyValue) { final String propertyName = field.getName(); final boolean isStatic = Modifier.isStatic(field.getModifiers()); final String setterName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); final List<Method> methods = new ArrayList<Method>(Arrays.asList(testClazz.getMethods())); methods.addAll(Arrays.asList(testClazz.getDeclaredMethods())); Method unpreferredValidMethod = null; for (final 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; } final 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(); }
Util { public static Method findSetter(final Class<?> testClazz, final Field field, final Object propertyValue) { final String propertyName = field.getName(); final boolean isStatic = Modifier.isStatic(field.getModifiers()); final String setterName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); final List<Method> methods = new ArrayList<Method>(Arrays.asList(testClazz.getMethods())); methods.addAll(Arrays.asList(testClazz.getDeclaredMethods())); Method unpreferredValidMethod = null; for (final 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; } final 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(final Class type, final Object instance); static void addProperty(final Hashtable env, String property); static Method findSetter(final Class<?> testClazz, final Field field, final Object propertyValue); static Method getMostSpecificMethod(final Method method1, final Method method2); }
Util { public static Method findSetter(final Class<?> testClazz, final Field field, final Object propertyValue) { final String propertyName = field.getName(); final boolean isStatic = Modifier.isStatic(field.getModifiers()); final String setterName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); final List<Method> methods = new ArrayList<Method>(Arrays.asList(testClazz.getMethods())); methods.addAll(Arrays.asList(testClazz.getDeclaredMethods())); Method unpreferredValidMethod = null; for (final 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; } final 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(final Class type, final Object instance); static void addProperty(final Hashtable env, String property); static Method findSetter(final Class<?> testClazz, final Field field, final Object propertyValue); static Method getMostSpecificMethod(final Method method1, final Method method2);  }