X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=system%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fadapters%2FSetterConfiguration.java;h=77fd9720b4c66c9ac69a12bce27862a1458e5d41;hb=81fe8784a2182e25f92a7591ec5b0fba00afb828;hp=b2ab5f6d7044745182393717b2809d3a5b5c018b;hpb=f40bb58fb59463257e50e07efce97d3191d42b1e;p=utils diff --git a/system/general/src/main/java/org/wamblee/system/adapters/SetterConfiguration.java b/system/general/src/main/java/org/wamblee/system/adapters/SetterConfiguration.java index b2ab5f6d..77fd9720 100644 --- a/system/general/src/main/java/org/wamblee/system/adapters/SetterConfiguration.java +++ b/system/general/src/main/java/org/wamblee/system/adapters/SetterConfiguration.java @@ -47,40 +47,43 @@ import org.wamblee.system.core.SystemAssemblyException; public class SetterConfiguration { private Class _class; - private boolean _publicOnly; + private boolean publicOnly; - private Map _setters; + private Map setters; /** - * Constructs the setter configuration. By default all setters are added. + * Constructs the setter configuration. By default no setters are added. * * @param aClass * Class which is being configured. */ public SetterConfiguration(Class aClass) { _class = aClass; - _publicOnly = true; - initSetters(); + publicOnly = true; + setters = new HashMap(); } - private void initSetters() { - _setters = new HashMap(); - for (Method method: getAllSetters(_class, _publicOnly) ) { - _setters.put(method, createParameterValues(method)); + /** + * Makes sure that all available setters are used. + */ + public SetterConfiguration initAllSetters() { + setters.clear(); + for (Method method: getAllSetters(_class, publicOnly) ) { + setters.put(method, createParameterValues(method)); } + return this; } /** * Called to set whether non-public setters are also used. By default only - * public setters are used. This resets all changes made and selects all - * public setters if non-public is false and all setters if it is true. + * public setters are used. The currently selected setters remain chosen. * * @param aIsNonPublic * Non public flag. */ - public void setNonPublic(boolean aIsNonPublic) { - _publicOnly = !aIsNonPublic; - initSetters(); + public SetterConfiguration setNonPublic(boolean aIsNonPublic) { + publicOnly = !aIsNonPublic; + return this; } /** @@ -89,7 +92,7 @@ public class SetterConfiguration { * @return Reference to the current object to allow call chaining. */ public SetterConfiguration clear() { - _setters.clear(); + setters.clear(); return this; } @@ -97,59 +100,63 @@ public class SetterConfiguration { * Removes a setter from the set of methods. * * @param aName - * Name of the setter to remove (without the "set" prefix). + * Name of the setter to remove. * @return Reference to the current object to allow call chaining. */ public SetterConfiguration remove(String aName) { - final String name = createSetterName(aName); - Map setters = new HashMap(); - for (Method method : _setters.keySet()) { - if (method.getName().equals(name)) { - _setters.remove(method); + for (Method method : setters.keySet()) { + if (method.getName().equals(aName)) { + setters.remove(method); return this; } } throw new IllegalArgumentException( - "No setter configured by the name of '" + aName + "'"); + "No method configured by the name of '" + aName + "'"); } - + /** - * Creates the name of a setter based on the name of the setter without the - * "set" prefix. - * - * @param aName - * Setter name. - * @return Setter name. + * Removes the method from the set of methods. + * @param aMethod Method to remove. + * @return */ - private String createSetterName(String aName) { - return "set" + aName.substring(0, 1).toUpperCase() + aName.substring(1); + public SetterConfiguration remove(Method aMethod) { + if ( !aMethod.getDeclaringClass().isAssignableFrom(_class) ) { + throw new RuntimeException("Method " + aMethod + " not found in class " + _class + " or its superclasses"); + } + for (Method method : setters.keySet()) { + if (method.equals(aMethod)) { + setters.remove(method); + return this; + } + } + throw new IllegalArgumentException( + "Method '" + aMethod + "' was not configured. "); } - + /** * Adds a given setter name to the setters. * - * @param aName + * @param aName Name of a setter method. * @return Reference to the current object to allow call chaining. */ - public SetterConfiguration add(String aName) { - final String name = createSetterName(aName); - int oldlen = _setters.size(); + public SetterConfiguration add(final String aName) { + int oldlen = setters.size(); List methods = new ArrayList(); - CollectionFilter.filter(getAllSetters(_class, _publicOnly), methods, + CollectionFilter.filter(getAllSetters(_class, publicOnly), methods, new Condition() { @Override public boolean matches(Method aObject) { - return aObject.getName().equals(name); + return aObject.getName().equals(aName); } }); if (methods.size() == 0 ) { - throw new IllegalArgumentException("No setter found for '" + aName - + "' in " + _class.getName()); + throw new IllegalArgumentException("Method '" + aName + + "' not found in " + _class.getName()); } // TODO is it possible to get more than one setter here in case the subclass overrides // the baseclass method? - _setters.put(methods.get(0), createParameterValues(methods.get(0))); + setters.put(methods.get(0), createParameterValues(methods.get(0))); return this; } @@ -164,9 +171,9 @@ public class SetterConfiguration { * @throws IllegalArgumentException * In case no setter is found or multiple setters are found. */ - public SetterConfiguration add(final Class aType) { + public SetterConfiguration addSetter(final Class aType) { List result = new ArrayList(); - CollectionFilter.filter(getAllSetters(_class, _publicOnly), result, + CollectionFilter.filter(getAllSetters(_class, publicOnly), result, new Condition() { @Override public boolean matches(Method aObject) { @@ -192,7 +199,7 @@ public class SetterConfiguration { + setters); } Method method = result.get(0); - _setters.put(method, createParameterValues(method)); + setters.put(method, createParameterValues(method)); return this; } @@ -201,7 +208,7 @@ public class SetterConfiguration { * * @return List of all setters. */ - private static List getAllSetters(Class aClass, + public static List getAllSetters(Class aClass, boolean aPublicOnly) { List result = new ArrayList(); for (Method method : getAllMethods(aClass)) { @@ -216,10 +223,14 @@ public class SetterConfiguration { return result; } - private static ParameterValues createParameterValues(Method method) { - return new ParameterValues( - new String[] { getSetterName(method) }, new Class[] { method - .getParameterTypes()[0] }); + private static ParameterValues createParameterValues(Method aMethod) { + + Class[] paramTypes = aMethod.getParameterTypes(); + String[] paramNames = new String[paramTypes.length]; + for (int i = 0; i < paramTypes.length; i++) { + paramNames[i] = aMethod.getName() + "." + i; + } + return new ParameterValues(paramNames, paramTypes); } private static final List getAllMethods(Class aClass) { @@ -233,8 +244,8 @@ public class SetterConfiguration { */ public List getRequiredInterfaces() { List result = new ArrayList(); - for (Method method : _setters.keySet()) { - result.addAll(_setters.get(method).getRequiredInterfaces()); + for (Method method : setters.keySet()) { + result.addAll(setters.get(method).getRequiredInterfaces()); } return result; } @@ -252,8 +263,8 @@ public class SetterConfiguration { throw new IllegalArgumentException("Object '" + aObject + "' is not an instance of " + _class.getName()); } - for (Method method : _setters.keySet()) { - ParameterValues values = _setters.get(method); + for (Method method : setters.keySet()) { + ParameterValues values = setters.get(method); try { method.invoke(aObject, values.values(aScope)); @@ -277,31 +288,16 @@ public class SetterConfiguration { * @return Parameter values. */ public ParameterValues values(String aMethod) { - String name = createSetterName(aMethod); - for (Method method : _setters.keySet()) { - if (method.getName().equals(name)) { - return _setters.get(method); + for (Method method : setters.keySet()) { + if (method.getName().equals(aMethod)) { + return setters.get(method); } } - throw new IllegalArgumentException("No setter method '" + name + throw new IllegalArgumentException("No setter method '" + aMethod + "' found"); } - /** - * Gets the setter name for a given setter method. This is the name of the - * setter without the "set" prefix and with the first character converted to - * lowercase. - * - * @param aMethod - * Method. - * @return Setter name. - */ - private static String getSetterName(Method aMethod) { - String result = aMethod.getName().substring(3); - return result.substring(0, 1).toLowerCase() + result.substring(1); - } - public List getSetters() { - return new ArrayList(_setters.keySet()); + return new ArrayList(setters.keySet()); } }