01: package org.drools.objenesis.instantiator.basic;
02:
03: import java.lang.reflect.Constructor;
04:
05: import org.drools.objenesis.ObjenesisException;
06: import org.drools.objenesis.instantiator.ObjectInstantiator;
07:
08: /**
09: * Instantiates a class by grabbing the no args constructor and calling Constructor.newInstance().
10: * This can deal with default public constructors, but that's about it.
11: *
12: * @see ObjectInstantiator
13: */
14: public class ConstructorInstantiator implements ObjectInstantiator {
15:
16: protected Constructor constructor;
17:
18: public ConstructorInstantiator(final Class type) {
19: try {
20: this .constructor = type
21: .getDeclaredConstructor((Class[]) null);
22: } catch (final Exception e) {
23: throw new ObjenesisException(e);
24: }
25: }
26:
27: public Object newInstance() {
28: try {
29: return this .constructor.newInstance((Object[]) null);
30: } catch (final Exception e) {
31: throw new ObjenesisException(e);
32: }
33: }
34:
35: }
|