1
18
19 package io.undertow.servlet.util;
20
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23
24 import io.undertow.servlet.api.InstanceFactory;
25 import io.undertow.servlet.api.InstanceHandle;
26
27
30 public class ConstructorInstanceFactory<T> implements InstanceFactory<T> {
31
32 private final Constructor<T> constructor;
33
34 public ConstructorInstanceFactory(final Constructor<T> constructor) {
35 constructor.setAccessible(true);
36 this.constructor = constructor;
37 }
38
39 @Override
40 public InstanceHandle<T> createInstance() throws InstantiationException {
41 try {
42 final T instance = constructor.newInstance();
43 return new ImmediateInstanceHandle<>(instance);
44 } catch (IllegalAccessException e) {
45 InstantiationException ite = new InstantiationException();
46 ite.initCause(e);
47 throw ite;
48 } catch (InvocationTargetException e) {
49 InstantiationException ite = new InstantiationException();
50 ite.initCause(e);
51 throw ite;
52 }
53 }
54 }
55