Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 115   Methods: 2
NCLOC: 66   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultCloneHandler.java 85,7% 90,6% 100% 89,6%
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.impl;
 11   
 12    import java.lang.reflect.*;
 13    import org.jgap.*;
 14   
 15    /**
 16    * Default clone handler supporting IApplicationData as well as implementations
 17    * of Cloneable (for the latter: in case the clone-method is accessible via
 18    * reflection).
 19    *
 20    * @author Klaus Meffert
 21    * @since 2.6
 22    */
 23    public class DefaultCloneHandler
 24    implements ICloneHandler {
 25    /**
 26    * Handles all implementations of IApplicationData as well as all of
 27    * java.lang.Cloneable (for which the clone-method is accessible via
 28    * reflection. This is not the case for package protected classes, e.g.).
 29    *
 30    * @param a_obj the object to check for (maybe null)
 31    * @param a_clazz the class to check for whether it is handled (maybe null)
 32    * @return true in case class is clonable via this handler
 33    *
 34    * @author Klaus Meffert
 35    * @since 2.6
 36    */
 37  22 public boolean isHandlerFor(final Object a_obj, final Class a_clazz) {
 38  22 Class clazz;
 39  22 if (a_clazz == null) {
 40  7 if (a_obj == null) {
 41  4 return false;
 42    }
 43  3 clazz = a_obj.getClass();
 44    }
 45    else {
 46  15 clazz = a_clazz;
 47    }
 48  18 if (IApplicationData.class.isAssignableFrom(clazz)) {
 49  3 return true;
 50    }
 51  15 if (Cloneable.class.isAssignableFrom(clazz)) {
 52    // Ensure access via reflection is possible.
 53    // Thank you Java for providing only a marker interface and not
 54    // something convenient :-(
 55    // ------------------------------------------------------------
 56  4 boolean result;
 57  4 try {
 58  4 Method m = clazz.getMethod("clone", new Class[] {});
 59  3 boolean modified = false;
 60  3 if (!m.isAccessible()) {
 61  3 m.setAccessible(true);
 62  3 modified = true;
 63    }
 64  3 result = m.isAccessible();
 65  3 if (modified) {
 66  3 m.setAccessible(false);
 67    }
 68    }
 69    catch (Exception ex) {
 70  1 return false;
 71    }
 72  3 return result;
 73    }
 74    else {
 75  11 return false;
 76    }
 77    }
 78   
 79    /**
 80    *
 81    * @param a_objToClone the object to clone
 82    * @param a_class not considered here
 83    * @param a_params not considered here
 84    * @return Object
 85    *
 86    * @author Klaus Meffert
 87    * @since 2.6
 88    */
 89  4 public Object perform(final Object a_objToClone, final Class a_class,
 90    final Object a_params) {
 91  4 if (IApplicationData.class.isAssignableFrom(a_objToClone.getClass())) {
 92  1 try {
 93  1 return ( (IApplicationData) a_objToClone).clone();
 94    }
 95    catch (CloneNotSupportedException cex) {
 96  0 throw new IllegalStateException(cex.getMessage());
 97    }
 98    }
 99    // Support Cloneable interface by looking for clone() method
 100    // via introspection.
 101    // ---------------------------------------------------------
 102  3 try {
 103  3 Method cloneMethod = a_objToClone.getClass().getMethod(
 104    "clone", new Class[] {});
 105  3 cloneMethod.setAccessible(true);
 106  3 return cloneMethod.invoke(a_objToClone, new Object[] {});
 107    }
 108    catch (InvocationTargetException iex) {
 109  0 throw new IllegalStateException(iex.getTargetException().getMessage());
 110    }
 111    catch (Throwable ex) {
 112  0 throw new IllegalStateException(ex.getMessage());
 113    }
 114    }
 115    }