updated coding rules.
[utils] / system / general / src / main / java / org / wamblee / system / adapters / ConstructorConfiguration.java
index 2725d0338fcae9026cf8f02754da8c2e5453f6f8..9050b9557fcb4a5428a836f98a227b6fb2e56e3c 100644 (file)
@@ -16,7 +16,6 @@
 package org.wamblee.system.adapters;
 
 import java.lang.reflect.Constructor;
-import java.lang.reflect.Member;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -28,34 +27,49 @@ import org.wamblee.system.core.RequiredInterface;
 import org.wamblee.system.core.Scope;
 import org.wamblee.system.core.SystemAssemblyException;
 
+/**
+ * Class that allows configuration of the constructor to use. 
+ * 
+ * In particular, it provides:
+ * <ul>
+ *   <li> Selection of a constructor using explicit selection 
+ *      {@link #select(Class...)} or using the most greedy constructor 
+ *      {@link #greedy()}.
+ *   </li>
+ *   <li>
+ *      Selection of methods to invoke to inject other objects into the object.  
+ *   </li>
+ *   <li> Selection of fields to set. 
+ *   </li>
+ * </ul>
+ */
 public class ConstructorConfiguration {
-       private Class _class
-       private Constructor<?> _constructor;
-       private ParameterValues _values;
-       private boolean _publicOnly; 
+       private Class clazz
+       private Constructor<?> constructor;
+       private ParameterValues values;
+       private boolean publicOnly; 
 
        /**
-        * Constructs the configuration. By default no constructor is selected and 
-        * one of {@link #select(Class...)} or 
-        * {@link #greedy()} must be called.  
+        * Constructs the configuration. By default the public constructor with the 
+        * most arguments will be used.   
         * @param aClass Class to construct. 
         */
        public ConstructorConfiguration(Class aClass) {
-               _class = aClass; 
-               _constructor = null;  
-               _publicOnly = true; 
+               clazz = aClass; 
+               constructor = null;  
+               publicOnly = true; 
        }
        
        /**
         * Sets whether or no non public constructors are also considered.
-        * Reset the choice of a constructor. 
+        * Reset the choice of a constructor to its default.  
         * @param aNonPublic
         * @return
         */
        public ConstructorConfiguration setNonPublic(boolean aNonPublic) { 
-               _publicOnly = !aNonPublic;
-               _constructor = null;
-               _values = null;
+               publicOnly = !aNonPublic;
+               constructor = null;
+               values = null;
                return this; 
        }
        
@@ -66,7 +80,7 @@ public class ConstructorConfiguration {
         */
        public ConstructorConfiguration select(Class... aTypes) {
                try {
-                       _constructor = _class.getDeclaredConstructor(aTypes); 
+                       constructor = clazz.getDeclaredConstructor(aTypes); 
                } catch (Exception e) {
                        throw new SystemAssemblyException(e.getMessage(), e);
                }
@@ -81,9 +95,9 @@ public class ConstructorConfiguration {
         *    identified.  
         */
        public ConstructorConfiguration greedy() { 
-               Constructor<?>[] declared = _class.getDeclaredConstructors();
+               Constructor<?>[] declared = clazz.getDeclaredConstructors();
                if (declared.length == 0) {
-                       throw new SystemAssemblyException("Class '" + _class
+                       throw new SystemAssemblyException("Class '" + clazz
                                        + " is an interface, primitive type, or array");
                }
                int max = -1;
@@ -92,7 +106,7 @@ public class ConstructorConfiguration {
                        new Condition<Constructor<?>>() { 
                        @Override
                        public boolean matches(Constructor<?> aObject) {
-                               if ( !_publicOnly ) { 
+                               if ( !publicOnly ) { 
                                        return true; 
                                } else { 
                                        return Modifier.isPublic(aObject.getModifiers());
@@ -118,22 +132,22 @@ public class ConstructorConfiguration {
                        throw new SystemAssemblyException(
                                        "Greediest constructor cannot be uniquely determined");
                }
-           _constructor = longest.get(0);
+           constructor = longest.get(0);
            resetValues();
            return this; 
        }
        
        public ParameterValues getParameters() {
                getConstructor(); // initialize constructor if needed.
-               return _values; 
+               return values; 
        }
 
        /**
         * Resets the values. 
         */
        private void resetValues() {
-               _constructor.setAccessible(true);
-               _values = new ParameterValues(_constructor.getParameterTypes());        
+               constructor.setAccessible(true);
+               values = new ParameterValues(constructor.getParameterTypes());  
        }
 
        /**
@@ -142,24 +156,24 @@ public class ConstructorConfiguration {
         * @return object. 
         */
        public Object create(Scope aScope) {
-               Object[] values = _values.values(aScope);
+               Object[] valueArray = values.values(aScope);
                try {
-                       return getConstructor().newInstance(values);
+                       return getConstructor().newInstance(valueArray);
                } catch (Exception e) {
                        throw new SystemAssemblyException("Could not construct object "
-                                       + getConstructor() + " " + Arrays.asList(values), e);
+                                       + getConstructor() + " " + Arrays.asList(valueArray), e);
                }
        }
        
        public List<RequiredInterface> getRequiredInterfaces() {
                getConstructor(); // initialize constructor if needed.
-               return _values.getRequiredInterfaces(); 
+               return values.getRequiredInterfaces(); 
        }
        
        private Constructor getConstructor() { 
-               if (_constructor == null ) { 
+               if (constructor == null ) { 
                        greedy(); 
                }
-               return _constructor; 
+               return constructor; 
        }
 }