01: package org.drools.objenesis.instantiator.gcj;
02:
03: import java.io.IOException;
04: import java.io.ObjectInputStream;
05: import java.lang.reflect.Method;
06:
07: import org.drools.objenesis.ObjenesisException;
08: import org.drools.objenesis.instantiator.ObjectInstantiator;
09:
10: /**
11: * Base class for GCJ-based instantiators. It initializes reflection access to method
12: * ObjectInputStream.newObject, as well as creating a dummy ObjectInputStream to be used as the
13: * "this" argument for the method.
14: *
15: * @author Leonardo Mesquita
16: */
17: public abstract class GCJInstantiatorBase implements ObjectInstantiator {
18: protected static Method newObjectMethod = null;
19: protected static ObjectInputStream dummyStream;
20:
21: private static class DummyStream extends ObjectInputStream {
22: public DummyStream() throws IOException {
23: super ();
24: }
25: }
26:
27: private static void initialize() {
28: if (newObjectMethod == null) {
29: try {
30: newObjectMethod = ObjectInputStream.class
31: .getDeclaredMethod("newObject", new Class[] {
32: Class.class, Class.class });
33: newObjectMethod.setAccessible(true);
34: dummyStream = new DummyStream();
35: } catch (final Exception e) {
36: throw new ObjenesisException(e);
37: }
38: }
39: }
40:
41: protected final Class type;
42:
43: public GCJInstantiatorBase(final Class type) {
44: this .type = type;
45: initialize();
46: }
47:
48: public abstract Object newInstance();
49: }
|