- private List<Pair<Method, ParameterValues>> getAllSetters() {
- List<Pair<Method,ParameterValues>> result =
- new ArrayList<Pair<Method, ParameterValues>>();
- for (Method method : _class.getMethods()) {
- if (method.getName().startsWith("set")
- && method.getParameterTypes().length == 1) {
- String name = getSetterName(method);
- result.add(new Pair<Method,ParameterValues>(method,
- new ParameterValues(
- new String[] { name }, new Class[] { method.getParameterTypes()[0] })));
+ public SetterConfiguration addSetter(final Class aType) {
+ List<Method> result = new ArrayList<Method>();
+ CollectionFilter.filter(getAllSetters(clazz, publicOnly), result,
+ new Condition<Method>() {
+ @Override
+ public boolean matches(Method aObject) {
+ Class type = aObject.getParameterTypes()[0];
+
+ return type.equals(aType);
+ }
+ });
+
+ if (result.size() == 0) {
+ throw new IllegalArgumentException("No setter found in class '" +
+ clazz.getName() + "' that has a setter with argument type '" +
+ aType.getName() + "'");
+ }
+
+ if (result.size() > 1) {
+ StringBuffer settersString = new StringBuffer();
+
+ for (Method method : result) {
+ settersString.append((method.getName() + " "));
+ }
+
+ throw new IllegalArgumentException(
+ "Multiple setters found in class '" + clazz.getName() +
+ " that accept type '" + aType.getName() + "': " + settersString);
+ }
+
+ Method method = result.get(0);
+ setters.put(method, createParameterValues(method));
+
+ return this;
+ }
+
+ /**
+ * Gets all setters for the current class.
+ *
+ *
+ * @return List of all setters.
+ */
+ public static List<Method> getAllSetters(Class aClass, boolean aPublicOnly) {
+ List<Method> result = new ArrayList<Method>();
+
+ for (Method method : getAllMethods(aClass)) {
+ if (!aPublicOnly || Modifier.isPublic(method.getModifiers())) {
+ if (method.getName().startsWith("set") &&
+ (method.getParameterTypes().length == 1)) {
+ method.setAccessible(true);
+ result.add(method);
+ }