Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 230   Methods: 8
NCLOC: 107   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RootConfigurationHandler.java 53,6% 72,2% 100% 68,9%
coverage coverage
 1    /*
 2    * This file is part of JGAP.
 3    *
 4    * JGAP offers a dual license model containing the LGPL as well as the MPL.
 5    *
 6    * For licencing information please see the file license.txt included with JGAP
 7    * or have a look at the top of class org.jgap.Chromosome which representatively
 8    * includes the JGAP license policy applicable for any file delivered with JGAP.
 9    */
 10    package org.jgap.data.config;
 11   
 12    import java.util.*;
 13    import org.jgap.*;
 14    import java.lang.reflect.*;
 15   
 16    /**
 17    * The ConfigurationHandler for the Configuration class itself. This is the
 18    * entry point for a Configuration.
 19    * In other words this is for dynamically building up a Configuration.
 20    *
 21    * @author Siddhartha Azad
 22    * @author Klaus Meffert
 23    * @since 2.3
 24    * */
 25    public class RootConfigurationHandler
 26    implements ConfigurationHandler {
 27    /** String containing the CVS revision. Read out via reflection!*/
 28    private final static String CVS_REVISION = "$Revision: 1.10 $";
 29   
 30    // Namespace
 31    private final static String CONFIG_NAMESPACE = "org.jgap.Configuration";
 32   
 33    // constants to indicate various properties
 34    // ----------------------------------------
 35    private final static String GENETIC_OPS = "GeneticOperators";
 36   
 37    private final static String NATURAL_SELS = "NaturalSelectors";
 38   
 39    private Configurable m_configurable;
 40   
 41    /**
 42    * @return Name of this Configuration Object (name of what you are
 43    * configuring) to be used in the properties file.
 44    *
 45    * @since 2.3
 46    * */
 47  4 public String getName() {
 48  4 return "Configuration";
 49    }
 50   
 51    /**
 52    * Return the information to generate the GUI for configuring this class.
 53    * @return a list of ConfigProperty objects
 54    * @since 2.3
 55    * */
 56  1 public List getConfigProperties() {
 57  1 return null;
 58    }
 59   
 60    /**
 61    * Get the namespace to be used in the config file for the Configurable
 62    * this ConfigurationHandler belongs to.
 63    * @return the namespace of the Configurable
 64    *
 65    * @author Siddhartha Azad
 66    * @since 2.3
 67    * */
 68  1 public String getNS() {
 69  1 return CONFIG_NAMESPACE;
 70    }
 71   
 72    /**
 73    * Method that will populate an Configurable with the properties in the
 74    * config file.
 75    *
 76    * @throws ConfigException
 77    * @throws InvalidConfigurationException
 78    *
 79    * @author Siddhartha Azad
 80    * @since 2.3
 81    * */
 82  1 public void readConfig()
 83    throws ConfigException, InvalidConfigurationException {
 84    // TODO adapt to new concept of configuration via reflection and marker
 85    // interface
 86    // set the namespace to get the properties from
 87  1 ConfigFileReader.instance().setNS(CONFIG_NAMESPACE);
 88  1 String value = ConfigFileReader.instance().getValue("m_populationSize");
 89  1 try {
 90  1 if (value != null) {
 91  1 setConfigProperty(m_configurable, "m_populationSize", value);
 92    }
 93    } catch (IllegalAccessException ex) {
 94  0 ex.printStackTrace();
 95  0 throw new InvalidConfigurationException(ex.getMessage());
 96    }
 97    // go through all genetic operators and configure them
 98  1 configureClass(GENETIC_OPS);
 99    // go through all natural selectors and configure them
 100  1 configureClass(NATURAL_SELS);
 101    }
 102   
 103    /**
 104    * Set the Configurable to which this ConfigurationHandler belongs.
 105    * @param a_configurable the Configurable to which this ConfigurationHandler
 106    * belongs
 107    *
 108    * @author Siddhartha Azad
 109    * @since 2.3
 110    * */
 111  3681 public void setConfigurable(Configurable a_configurable) {
 112  3681 m_configurable = a_configurable;
 113    }
 114   
 115    /**
 116    * Sets the property of a configurable to a given value. Uses reflection to
 117    * do so. Queries the method getConfigVarName for the name of the field
 118    * hosting the configurable properties.
 119    *
 120    * @param a_configurable the configurable to use
 121    * @param a_propertyName the property to set
 122    * @param a_value the value to assign to the property
 123    * @throws Exception
 124    *
 125    * @author Klaus Meffert
 126    * @since 2.6
 127    */
 128  1 public void setConfigProperty(Object a_configurable, String a_propertyName,
 129    String a_value)
 130    throws IllegalAccessException {
 131    // Use following in case variable name "m_config" should be determined
 132    // dynamically.
 133    // Method m = null;
 134    // try {
 135    // m = a_configurable.getClass().getDeclaredMethod("getConfigVarName",
 136    // new Class[0]);
 137    // } catch (NoSuchMethodException nex) {
 138    // // nothing to set here
 139    // return;
 140    // }
 141  1 String configVarName = "m_config"; //(String)m.invoke(a_configurable, new Object[0]);
 142  1 Field configVar = getPrivateField(a_configurable, configVarName);
 143  1 configVar.setAccessible(true);
 144  1 Object configObj = configVar.get(a_configurable);
 145  1 Field propertyVar = getPrivateField(configObj, a_propertyName);
 146  1 propertyVar.setAccessible(true);
 147  1 Class type = propertyVar.getType();
 148  1 if (type.equals(boolean.class)) {
 149  0 propertyVar.setBoolean(configObj, Boolean.valueOf(a_value).booleanValue());
 150    }
 151  1 else if (type.equals(byte.class)) {
 152  0 propertyVar.setByte(configObj, Byte.valueOf(a_value).byteValue());
 153    }
 154  1 else if (type.equals(char.class)) {
 155  0 propertyVar.setChar(configObj, a_value.charAt(0));
 156    }
 157  1 else if (type.equals(double.class)) {
 158  0 propertyVar.setDouble(configObj, Double.valueOf(a_value).doubleValue());
 159    }
 160  1 else if (type.equals(float.class)) {
 161  0 propertyVar.setFloat(configObj, Float.valueOf(a_value).floatValue());
 162    }
 163  1 else if (type.equals(int.class)) {
 164  1 propertyVar.setInt(configObj, Integer.valueOf(a_value).intValue());
 165    }
 166  0 else if (type.equals(long.class)) {
 167  0 propertyVar.setLong(configObj, Long.valueOf(a_value).longValue());
 168    }
 169  0 else if (type.equals(short.class)) {
 170  0 propertyVar.setShort(configObj, Short.valueOf(a_value).shortValue());
 171    }
 172  0 else if (type.equals(String.class)) {
 173  0 propertyVar.set(configObj, a_value);
 174    }
 175    else {
 176  0 throw new RuntimeException("Unknown field type: " + type.getName());
 177    }
 178    }
 179   
 180    /**
 181    * Helper method: Read a private field.
 182    * @param a_instance the instance the field is contained with
 183    * @param a_fieldName the name of the field to read
 184    * @return the Field object or null, if none found
 185    *
 186    * @author Klaus Meffert
 187    * @since 2.6
 188    */
 189  5 public static Field getPrivateField(Object a_instance, String a_fieldName) {
 190  5 final Field fields[] = a_instance.getClass().getDeclaredFields();
 191  5 for (int i = 0; i < fields.length; ++i) {
 192  41 if (a_fieldName.equals(fields[i].getName())) {
 193  3 fields[i].setAccessible(true);
 194  3 return fields[i];
 195    }
 196    }
 197  2 return null;
 198    }
 199   
 200    /**
 201    * Retrieve all instances of a certain property from the config file reader
 202    * and configure each of these.
 203    * @param className the name of the property to configure
 204    * @throws ConfigException
 205    *
 206    * @author Siddhartha Azad
 207    * @since 2.4
 208    * */
 209  2 public static void configureClass(String className)
 210    throws ConfigException {
 211  2 List values = ConfigFileReader.instance().getValues(className);
 212  2 if (values != null && values.size() > 0) {
 213  1 String cName = "";
 214    // iterate through all instances of this property and create Configurables
 215    // for them, then configure these
 216  1 for (Iterator iter = values.iterator(); iter.hasNext(); ) {
 217  1 try {
 218  1 cName = (String) iter.next();
 219  1 Class genClass = Class.forName(cName);
 220  1 Configurable conObj = (Configurable) genClass.newInstance();
 221    //TODO ConfigurationHandler cHandler = conObj.getConfigurationHandler();
 222    //TODO cHandler.readConfig();
 223    } catch (Exception ex) {
 224  0 throw new ConfigException("Error while configuring " + className +
 225    "." + cName);
 226    }
 227    }
 228    }
 229    }
 230    }