(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / adapters / SetterConfiguration.java
index 374edae6cb12a3afb767070985dbc444f54bcd7a..581c5b5913e7513fd5c7d59e37ceeb5c1b1c2429 100644 (file)
@@ -18,7 +18,9 @@ package org.wamblee.system.adapters;
 import java.awt.CompositeContext;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import org.wamblee.collections.CollectionFilter;
@@ -40,172 +42,216 @@ import org.wamblee.system.core.SystemAssemblyException;
  */
 public class SetterConfiguration {
 
-    private Class _class;
+       private Class _class;
+       private boolean _publicOnly;
 
-    private List<Pair<Method, ParameterValues>> _setters;
+       private List<Pair<Method, ParameterValues>> _setters;
 
-    /**
-     * Constructs the setter configuration. By default all setters are added.
-     * 
-     * @param aClass
-     *            Class which is being configured.
-     */
-    public SetterConfiguration(Class aClass) {
-        _class = aClass;
-        _setters = getAllSetters();
-    }
+       /**
+        * Constructs the setter configuration. By default all setters are added.
+        * 
+        * @param aClass
+        *            Class which is being configured.
+        */
+       public SetterConfiguration(Class aClass) {
+               _class = aClass;
+               _publicOnly = true;
+               _setters = getAllSetters(_class, _publicOnly);
+       }
 
-    /**
-     * Removes all setters.
-     * 
-     * @return Reference to the current object to allow call chaining.
-     */
-    public SetterConfiguration clear() {
-        _setters.clear();
-        return this;
-    }
+       /**
+        * Called to set whether non-public setters are also used. By default only
+        * public setters are used.
+        * 
+        * @param aIsNonPublic
+        *            Non public flag.
+        */
+       public void setNonPublic(boolean aIsNonPublic) {
+               _publicOnly = !aIsNonPublic;
+               _setters = getAllSetters(_class, _publicOnly);
+       }
 
-    /**
-     * Removes a setter from the set of methods.
-     * 
-     * @param aName
-     *            Name of the setter to remove (without the "set" prefix).
-     * @return Reference to the current object to allow call chaining.
-     */
-    public SetterConfiguration remove(String aName) {
-        final String name = createSetterName(aName);
-        List<Pair<Method,ParameterValues>> setters = 
-            new ArrayList<Pair<Method,ParameterValues>>();
-        CollectionFilter.filter(_setters, setters, new Condition<Pair<Method,ParameterValues>>() {
-            @Override
-            public boolean matches(Pair<Method,ParameterValues> aObject) {
-                return !aObject.getFirst().getName().equals(name);
-            }
-
-        });
-        if ( _setters.size() == setters.size()) { 
-            throw new IllegalArgumentException("No setter configured by the name of '" + aName + "'");
-        }
-        _setters = setters;
-        return this;
-    }
+       /**
+        * Removes all setters.
+        * 
+        * @return Reference to the current object to allow call chaining.
+        */
+       public SetterConfiguration clear() {
+               _setters.clear();
+               return this;
+       }
 
-    /**
-     * Creates the name of a setter based on the name of the setter without
-     * the "set" prefix. 
-     * @param aName Setter name. 
-     * @return Setter name. 
-     */
-    private String createSetterName(String aName) {
-        return "set" + aName.substring(0, 1).toUpperCase()
-                + aName.substring(1);
-    }
+       /**
+        * Removes a setter from the set of methods.
+        * 
+        * @param aName
+        *            Name of the setter to remove (without the "set" prefix).
+        * @return Reference to the current object to allow call chaining.
+        */
+       public SetterConfiguration remove(String aName) {
+               final String name = createSetterName(aName);
+               List<Pair<Method, ParameterValues>> setters = new ArrayList<Pair<Method, ParameterValues>>();
+               CollectionFilter.filter(_setters, setters,
+                               new Condition<Pair<Method, ParameterValues>>() {
+                                       @Override
+                                       public boolean matches(Pair<Method, ParameterValues> aObject) {
+                                               return !aObject.getFirst().getName().equals(name);
+                                       }
 
-    /**
-     * Adds a given setter name to the setters. 
-     * @param aName
-     * @return Reference to the current object to allow call chaining. 
-     */
-    public SetterConfiguration add(String aName) {
-        final String name = createSetterName(aName);
-        int oldlen = _setters.size(); 
-        CollectionFilter.filter(getAllSetters(), _setters,
-                new Condition<Pair<Method,ParameterValues>>() {
-                    @Override
-                    public boolean matches(Pair<Method,ParameterValues> aObject) {
-                        return aObject.getFirst().getName().equals(name);
-                    }
-
-                });
-        if ( _setters.size() == oldlen) { 
-            throw new IllegalArgumentException("No setter found for '" + aName + "' in " + _class.getName());
-        }
-        return this;
-    }
+                               });
+               if (_setters.size() == setters.size()) {
+                       throw new IllegalArgumentException(
+                                       "No setter configured by the name of '" + aName + "'");
+               }
+               _setters = setters;
+               return this;
+       }
 
-    /**
-     * Gets all setters for the current class. 
-     * @return List of all setters. 
-     */
-    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] })));
-            }
-        }
-        return result;
-    }
-    
-    /**
-     * Gets the required interfaces based on the configured setteres.
-     * @return List of required interfaces. 
-     */
-    public List<RequiredInterface> getRequiredInterfaces() { 
-        List<RequiredInterface> result = new ArrayList<RequiredInterface>();
-        for (Pair<Method,ParameterValues> method: _setters) {
-            result.addAll(method.getSecond().getRequiredInterfaces());
-        }
-        return result; 
-    }
-    
-    
-    /**
-     * Invokes all configured setters with the appropriate values. 
-     * @param aScope Scope within which invocation takes place. 
-     * @param aObject Object on which the invocation takes place. 
-     */
-    public void inject(Scope aScope, Object aObject) {
-        if ( !_class.isInstance(aObject)) { 
-            throw new IllegalArgumentException("Object '" + aObject + "' is not an instance of " 
-                    + _class.getName());
-        }
-        for (Pair<Method,ParameterValues> setter: _setters) {
-            Method method = setter.getFirst();
-            ParameterValues values = setter.getSecond(); 
-            
-            try {
-                method.invoke(aObject, values.values(aScope));
-            } catch (IllegalAccessException e) {
-                throw new SystemAssemblyException("Problem invoking " + method + " with " + values, e);
-            } catch (InvocationTargetException e) { 
-                throw new SystemAssemblyException("Problem invoking " + method + " with " + values, e);
-            }
-        }
-    }
-    
-    /**
-     * Returns the parameter values for allowing detailed configuration of how
-     * parameter values are set.
-     * @param aSetter Setter name without the "set" prefix with the first
-     *  character converted to lower case.  
-     * @return Parameter values. 
-     */
-    public ParameterValues values(String aMethod) { 
-        String name = createSetterName(aMethod); 
-        for (Pair<Method,ParameterValues> method: _setters) { 
-            if ( method.getFirst().getName().equals(name) ) { 
-                return method.getSecond();
-            }
-        }
-        throw new IllegalArgumentException("No setter method '" + name + "' found");
-    }
+       /**
+        * Creates the name of a setter based on the name of the setter without the
+        * "set" prefix.
+        * 
+        * @param aName
+        *            Setter name.
+        * @return Setter name.
+        */
+       private String createSetterName(String aName) {
+               return "set" + aName.substring(0, 1).toUpperCase() + aName.substring(1);
+       }
 
-    /**
-     * 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 String getSetterName(Method aMethod) {
-        String result = aMethod.getName().substring(3);
-        return result.substring(0,1).toLowerCase() +  result.substring(1);
+       /**
+        * Adds a given setter name to the setters.
+        * 
+        * @param aName
+        * @return Reference to the current object to allow call chaining.
+        */
+       public SetterConfiguration add(String aName) {
+               final String name = createSetterName(aName);
+               int oldlen = _setters.size();
+               CollectionFilter.filter(getAllSetters(_class, false), _setters,
+                               new Condition<Pair<Method, ParameterValues>>() {
+                                       @Override
+                                       public boolean matches(Pair<Method, ParameterValues> aObject) {
+                                               return aObject.getFirst().getName().equals(name);
+                                       }
+
+                               });
+               if (_setters.size() == oldlen) {
+                       throw new IllegalArgumentException("No setter found for '" + aName
+                                       + "' in " + _class.getName());
+               }
+               return this;
+       }
+
+       /**
+        * Gets all setters for the current class.
+        * 
+        * @return List of all setters.
+        */
+       private static List<Pair<Method, ParameterValues>> getAllSetters(
+                       Class aClass, boolean aPublicOnly) {
+               List<Pair<Method, ParameterValues>> result = new ArrayList<Pair<Method, ParameterValues>>();
+               for (Method method : getAllMethods(aClass)) {
+                       if (!aPublicOnly || Modifier.isPublic(method.getModifiers())) {
+                               if (method.getName().startsWith("set")
+                                               && method.getParameterTypes().length == 1) {
+                                       method.setAccessible(true);
+                                       String name = getSetterName(method);
+                                       result
+                                                       .add(new Pair<Method, ParameterValues>(method,
+                                                                       new ParameterValues(new String[] { name },
+                                                                                       new Class[] { method
+                                                                                                       .getParameterTypes()[0] })));
+                               }
+                       }
+               }
+               return result;
+       }
+
+       private static final List<Method> getAllMethods(Class aClass) {
+       List<Method> result = new ArrayList<Method>();
+       result.addAll(Arrays.asList(aClass.getDeclaredMethods()));
+       Class superClass = aClass.getSuperclass();
+       if ( superClass != null ) { 
+               result.addAll(getAllMethods(superClass));
+       }
+       return result; 
     }
-    
+
+       /**
+        * Gets the required interfaces based on the configured setteres.
+        * 
+        * @return List of required interfaces.
+        */
+       public List<RequiredInterface> getRequiredInterfaces() {
+               List<RequiredInterface> result = new ArrayList<RequiredInterface>();
+               for (Pair<Method, ParameterValues> method : _setters) {
+                       result.addAll(method.getSecond().getRequiredInterfaces());
+               }
+               return result;
+       }
+
+       /**
+        * Invokes all configured setters with the appropriate values.
+        * 
+        * @param aScope
+        *            Scope within which invocation takes place.
+        * @param aObject
+        *            Object on which the invocation takes place.
+        */
+       public void inject(Scope aScope, Object aObject) {
+               if (!_class.isInstance(aObject)) {
+                       throw new IllegalArgumentException("Object '" + aObject
+                                       + "' is not an instance of " + _class.getName());
+               }
+               for (Pair<Method, ParameterValues> setter : _setters) {
+                       Method method = setter.getFirst();
+                       ParameterValues values = setter.getSecond();
+
+                       try {
+                               method.invoke(aObject, values.values(aScope));
+                       } catch (IllegalAccessException e) {
+                               throw new SystemAssemblyException("Problem invoking " + method
+                                               + " with " + values, e);
+                       } catch (InvocationTargetException e) {
+                               throw new SystemAssemblyException("Problem invoking " + method
+                                               + " with " + values, e);
+                       }
+               }
+       }
+
+       /**
+        * Returns the parameter values for allowing detailed configuration of how
+        * parameter values are set.
+        * 
+        * @param aSetter
+        *            Setter name without the "set" prefix with the first character
+        *            converted to lower case.
+        * @return Parameter values.
+        */
+       public ParameterValues values(String aMethod) {
+               String name = createSetterName(aMethod);
+               for (Pair<Method, ParameterValues> method : _setters) {
+                       if (method.getFirst().getName().equals(name)) {
+                               return method.getSecond();
+                       }
+               }
+               throw new IllegalArgumentException("No setter method '" + name
+                               + "' 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);
+       }
+
 }