01: package org.drools.objenesis;
02:
03: import java.io.Serializable;
04:
05: import org.drools.objenesis.instantiator.ObjectInstantiator;
06:
07: /**
08: * Use Objenesis in a static way. <strong>It is strongly not recommended to use this class.</strong>
09: *
10: * @author Henri Tremblay
11: */
12: public final class ObjenesisHelper {
13:
14: private static final Objenesis OBJENESIS_STD = new ObjenesisStd();
15:
16: private static final Objenesis OBJENESIS_SERIALIZER = new ObjenesisSerializer();
17:
18: private ObjenesisHelper() {
19: }
20:
21: /**
22: * Will create a new object without any constructor being called
23: *
24: * @param clazz Class to instantiate
25: * @return New instance of clazz
26: */
27: public static final Object newInstance(final Class clazz) {
28: return OBJENESIS_STD.newInstance(clazz);
29: }
30:
31: /**
32: * Will create an object just like it's done by ObjectInputStream.readObject (the default
33: * constructor of the first non serializable class will be called)
34: *
35: * @param clazz Class to instantiate
36: * @return New instance of clazz
37: */
38: public static final Serializable newSerializableInstance(
39: final Class clazz) {
40: return (Serializable) OBJENESIS_SERIALIZER.newInstance(clazz);
41: }
42:
43: /**
44: * Will pick the best instantiator for the provided class. If you need to create a lot of
45: * instances from the same class, it is way more efficient to create them from the same
46: * ObjectInstantiator than calling {@link #newInstance(Class)}.
47: *
48: * @param clazz Class to instantiate
49: * @return Instantiator dedicated to the class
50: */
51: public static final ObjectInstantiator getInstantiatorOf(
52: final Class clazz) {
53: return OBJENESIS_STD.getInstantiatorOf(clazz);
54: }
55:
56: /**
57: * Same as {@link #getInstantiatorOf(Class)} but providing an instantiator emulating
58: * ObjectInputStream.readObject behavior.
59: *
60: * @see #newSerializableInstance(Class)
61: * @param clazz Class to instantiate
62: * @return Instantiator dedicated to the class
63: */
64: public static final ObjectInstantiator getSerializableObjectInstantiatorOf(
65: final Class clazz) {
66: return OBJENESIS_SERIALIZER.getInstantiatorOf(clazz);
67: }
68: }
|