01: package org.drools.objenesis.strategy;
02:
03: import java.io.NotSerializableException;
04: import java.io.Serializable;
05:
06: import org.drools.objenesis.ObjenesisException;
07: import org.drools.objenesis.instantiator.ObjectInstantiator;
08: import org.drools.objenesis.instantiator.basic.ObjectStreamClassInstantiator;
09: import org.drools.objenesis.instantiator.gcj.GCJSerializationInstantiator;
10: import org.drools.objenesis.instantiator.sun.Sun13SerializationInstantiator;
11:
12: /**
13: * Guess the best serializing instantiator for a given class. The returned instantiator will
14: * instantiate classes like the genuine java serialization framework (the constructor of the first
15: * not serializable class will be called). Currently, the selection doesn't depend on the class. It
16: * relies on the
17: * <ul>
18: * <li>JVM version</li>
19: * <li>JVM vendor</li>
20: * <li>JVM vendor version</li>
21: * </ul>
22: * However, instantiators are stateful and so dedicated to their class.
23: *
24: * @see ObjectInstantiator
25: */
26: public class SerializingInstantiatorStrategy extends
27: BaseInstantiatorStrategy {
28:
29: /**
30: * Return an {@link ObjectInstantiator} allowing to create instance following the java
31: * serialization framework specifications.
32: *
33: * @param type Class to instantiate
34: * @return The ObjectInstantiator for the class
35: */
36: public ObjectInstantiator newInstantiatorOf(final Class type) {
37: if (!Serializable.class.isAssignableFrom(type)) {
38: throw new ObjenesisException(new NotSerializableException(
39: type + " not serializable"));
40: }
41: if (JVM_NAME.startsWith(SUN)) {
42: if (VM_VERSION.startsWith("1.3")) {
43: return new Sun13SerializationInstantiator(type);
44: }
45: } else if (JVM_NAME.startsWith(GNU)) {
46: return new GCJSerializationInstantiator(type);
47: }
48: return new ObjectStreamClassInstantiator(type);
49: }
50:
51: }
|