01: package org.drools.objenesis.strategy;
02:
03: import org.drools.objenesis.instantiator.ObjectInstantiator;
04: import org.drools.objenesis.instantiator.gcj.GCJInstantiator;
05: import org.drools.objenesis.instantiator.jrockit.JRockit131Instantiator;
06: import org.drools.objenesis.instantiator.jrockit.JRockitLegacyInstantiator;
07: import org.drools.objenesis.instantiator.sun.Sun13Instantiator;
08: import org.drools.objenesis.instantiator.sun.SunReflectionFactoryInstantiator;
09:
10: /**
11: * Guess the best instantiator for a given class. The instantiator will instantiate the class
12: * without calling any constructor. Currently, the selection doesn't depend on the class. It relies
13: * on the
14: * <ul>
15: * <li>JVM version</li>
16: * <li>JVM vendor</li>
17: * <li>JVM vendor version</li>
18: * </ul>
19: * However, instantiators are stateful and so dedicated to their class.
20: *
21: * @see ObjectInstantiator
22: */
23: public class StdInstantiatorStrategy extends BaseInstantiatorStrategy {
24:
25: /**
26: * Return an {@link ObjectInstantiator} allowing to create instance without any constructor being
27: * called.
28: *
29: * @param type Class to instantiate
30: * @return The ObjectInstantiator for the class
31: */
32: public ObjectInstantiator newInstantiatorOf(final Class type) {
33:
34: if (JVM_NAME.startsWith(SUN)) {
35: if (VM_VERSION.startsWith("1.3")) {
36: return new Sun13Instantiator(type);
37: }
38: } else if (JVM_NAME.startsWith(JROCKIT)) {
39: if (VM_VERSION.startsWith("1.3")) {
40: return new JRockit131Instantiator(type);
41: } else if (VM_VERSION.startsWith("1.4")) {
42: // JRockit vendor version will be RXX where XX is the version
43: // Versions prior to 26 need special handling
44: // From R26 on, java.vm.version starts with R
45: if (!VENDOR_VERSION.startsWith("R")) {
46: // On R25.1 and R25.2, ReflectionFactory should work. Otherwise, we must use the
47: // Legacy instantiator.
48: if (VM_INFO == null || !VM_INFO.startsWith("R25.1")
49: || !VM_INFO.startsWith("R25.2")) {
50: return new JRockitLegacyInstantiator(type);
51: }
52: }
53: }
54: } else if (JVM_NAME.startsWith(GNU)) {
55: return new GCJInstantiator(type);
56: }
57:
58: // Fallback instantiator, should work with:
59: // - Java Hotspot version 1.4 and higher
60: // - JRockit 1.4-R26 and higher
61: // - IBM and Hitachi JVMs
62: // ... might works for others so we just give it a try
63: return new SunReflectionFactoryInstantiator(type);
64: }
65: }
|