01: /**
02: * COPYRIGHT & LICENSE
03: *
04: * This code is Copyright (c) 2006 BEA Systems, inc. It is provided free, as-is and without any warranties for the purpose of
05: * inclusion in Objenesis or any other open source project with a FSF approved license, as long as this notice is not
06: * removed. There are no limitations on modifying or repackaging the code apart from this.
07: *
08: * BEA does not guarantee that the code works, and provides no support for it. Use at your own risk.
09: *
10: * Originally developed by Leonardo Mesquita. Copyright notice added by Henrik Sthl, BEA JRockit Product Manager.
11: *
12: */package org.drools.objenesis.instantiator.jrockit;
13:
14: import java.lang.reflect.Method;
15:
16: import org.drools.objenesis.ObjenesisException;
17: import org.drools.objenesis.instantiator.ObjectInstantiator;
18:
19: /**
20: * Instantiates a class by making a call to internal JRockit private methods. It is only supposed to
21: * work on JRockit 1.4.2 JVMs prior to release R25.1. From release R25.1 on, JRockit supports
22: * sun.reflect.ReflectionFactory, making this "trick" unnecessary. This instantiator will not call
23: * any constructors.
24: *
25: * @author Leonardo Mesquita
26: * @see org.drools.objenesis.instantiator.ObjectInstantiator
27: * @see org.drools.objenesis.instantiator.sun.SunReflectionFactoryInstantiator
28: */
29: public class JRockitLegacyInstantiator implements ObjectInstantiator {
30: private static Method safeAllocObjectMethod = null;
31:
32: private static void initialize() {
33: if (safeAllocObjectMethod == null) {
34: Class memSystem;
35: try {
36: memSystem = Class.forName("jrockit.vm.MemSystem");
37: safeAllocObjectMethod = memSystem.getDeclaredMethod(
38: "safeAllocObject", new Class[] { Class.class });
39: safeAllocObjectMethod.setAccessible(true);
40: } catch (final Exception e) {
41: throw new ObjenesisException(e);
42: }
43: }
44: }
45:
46: private Class type;
47:
48: public JRockitLegacyInstantiator(final Class type) {
49: initialize();
50: this .type = type;
51: }
52:
53: public Object newInstance() {
54: try {
55: return safeAllocObjectMethod.invoke(null,
56: new Object[] { this .type });
57: } catch (final Exception e) {
58: throw new ObjenesisException(e);
59: }
60: }
61: }
|