KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > DefaultCloneHandler


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.io.*;
13 import java.lang.reflect.*;
14 import org.jgap.util.*;
15 import org.jgap.*;
16
17 /**
18  * Default clone handler supporting IApplicationData as well as implementations
19  * of Cloneable (for the latter: in case the clone-method is accessible via
20  * reflection).
21  *
22  * @author Klaus Meffert
23  * @since 2.6
24  */

25 public class DefaultCloneHandler
26     implements ICloneHandler, ICloneable, Serializable, Comparable JavaDoc {
27   /**
28    * Handles all implementations of IApplicationData as well as all of
29    * java.lang.Cloneable (for which the clone-method is accessible via
30    * reflection. This is not the case for package protected classes, e.g.).
31    *
32    * @param a_obj the object to check for (maybe null)
33    * @param a_clazz the class to check for whether it is handled (maybe null)
34    * @return true in case class is clonable via this handler
35    *
36    * @author Klaus Meffert
37    * @since 2.6
38    */

39   public boolean isHandlerFor(final Object JavaDoc a_obj, final Class JavaDoc a_clazz) {
40     Class JavaDoc 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 JavaDoc.class.isAssignableFrom(clazz)) {
57       // Ensure access via reflection is possible.
58
// Thank you Java for providing only a marker interface and not
59
// something convenient :-(
60
// ------------------------------------------------------------
61
boolean result;
62       try {
63         Method m = clazz.getMethod("clone", new Class JavaDoc[] {});
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 JavaDoc ex) {
75         return false;
76       }
77       return result;
78     }
79     else {
80       return false;
81     }
82   }
83
84   /**
85    * Performs the clone operation.
86    *
87    * @param a_objToClone the object to clone
88    * @param a_class not considered here
89    * @param a_params not considered here
90    * @return Object
91    *
92    * @author Klaus Meffert
93    * @since 2.6
94    */

95   public Object JavaDoc perform(final Object JavaDoc a_objToClone, final Class JavaDoc a_class,
96                         final Object JavaDoc a_params) {
97     Class JavaDoc 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 JavaDoc(cex.getMessage());
113       }
114     }
115     if (IApplicationData.class.isAssignableFrom(a_objToClone.getClass())) {
116       try {
117         return ( (IApplicationData) a_objToClone).clone();
118       }
119       catch (CloneNotSupportedException JavaDoc cex) {
120         throw new IllegalStateException JavaDoc(cex.getMessage());
121       }
122     }
123     // Support Cloneable interface by looking for clone() method
124
// via introspection.
125
// ---------------------------------------------------------
126
try {
127       Method cloneMethod = a_objToClone.getClass().getMethod(
128           "clone", new Class JavaDoc[] {});
129       cloneMethod.setAccessible(true);
130       return cloneMethod.invoke(a_objToClone, new Object JavaDoc[] {});
131     }
132     catch (InvocationTargetException iex) {
133       throw new IllegalStateException JavaDoc(iex.getTargetException().getMessage());
134     }
135     catch (Throwable JavaDoc ex) {
136       throw new IllegalStateException JavaDoc(ex.getMessage());
137     }
138   }
139
140   /**
141    * @return deep clone of this instance
142    *
143    * @author Klaus Meffert
144    * @since 3.2
145    */

146   public Object JavaDoc clone() {
147     return new DefaultCloneHandler();
148   }
149
150   /**
151    * @param a_other sic
152    * @return as always
153    *
154    * @author Klaus Meffert
155    * @since 3.2
156    */

157   public int compareTo(Object JavaDoc 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