1 10 package org.jgap.impl; 11 12 import java.io.*; 13 import java.lang.reflect.*; 14 import org.jgap.util.*; 15 import org.jgap.*; 16 17 25 public class DefaultCloneHandler 26 implements ICloneHandler, ICloneable, Serializable, Comparable { 27 39 public boolean isHandlerFor(final Object a_obj, final Class a_clazz) { 40 Class clazz; 41 if (a_clazz == null) { 42 if (a_obj == null) { 43 return false; 44 } 45 clazz = a_obj.getClass(); 46 } 47 else { 48 clazz = a_clazz; 49 } 50 if (IApplicationData.class.isAssignableFrom(clazz)) { 51 return true; 52 } 53 if (ICloneable.class.isAssignableFrom(clazz)) { 54 return true; 55 } 56 if (Cloneable .class.isAssignableFrom(clazz)) { 57 boolean result; 62 try { 63 Method m = clazz.getMethod("clone", new Class [] {}); 64 boolean modified = false; 65 if (!m.isAccessible()) { 66 m.setAccessible(true); 67 modified = true; 68 } 69 result = m.isAccessible(); 70 if (modified) { 71 m.setAccessible(false); 72 } 73 } 74 catch (Exception ex) { 75 return false; 76 } 77 return result; 78 } 79 else { 80 return false; 81 } 82 } 83 84 95 public Object perform(final Object a_objToClone, final Class a_class, 96 final Object a_params) { 97 Class clazz; 98 if (a_class == null) { 99 if (a_objToClone == null) { 100 return null; 101 } 102 clazz = a_objToClone.getClass(); 103 } 104 else { 105 clazz = a_class; 106 } 107 if (ICloneable.class.isAssignableFrom(clazz)) { 108 try { 109 return ( (ICloneable) a_objToClone).clone(); 110 } 111 catch (CloneException cex) { 112 throw new IllegalStateException (cex.getMessage()); 113 } 114 } 115 if (IApplicationData.class.isAssignableFrom(a_objToClone.getClass())) { 116 try { 117 return ( (IApplicationData) a_objToClone).clone(); 118 } 119 catch (CloneNotSupportedException cex) { 120 throw new IllegalStateException (cex.getMessage()); 121 } 122 } 123 try { 127 Method cloneMethod = a_objToClone.getClass().getMethod( 128 "clone", new Class [] {}); 129 cloneMethod.setAccessible(true); 130 return cloneMethod.invoke(a_objToClone, new Object [] {}); 131 } 132 catch (InvocationTargetException iex) { 133 throw new IllegalStateException (iex.getTargetException().getMessage()); 134 } 135 catch (Throwable ex) { 136 throw new IllegalStateException (ex.getMessage()); 137 } 138 } 139 140 146 public Object clone() { 147 return new DefaultCloneHandler(); 148 } 149 150 157 public int compareTo(Object a_other) { 158 if (a_other.getClass().equals(getClass())) { 159 return 0; 160 } 161 else { 162 return getClass().getName().compareTo(a_other.getClass().getName()); 163 } 164 } 165 } 166 | Popular Tags |