public SessionFactory proxy(SessionFactory sessionFactory, ServiceRegistry serviceRegistry) {
this.currentInstance = sessionFactory;
this.serviceRegistry = serviceRegistry;
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(SessionFactoryImpl.class);
factory.setInterfaces(new Class[]{SessionFactory.class});
MethodHandler handler = new MethodHandler() {
@Override
public Object invoke(Object self, Method overridden, Method forwarder,
Object[] args) throws Throwable {
return overridden.invoke(currentInstance, args);
}
};
Object instance;
try {
Constructor constructor = ReflectionFactory.getReflectionFactory().newConstructorForSerialization(factory.createClass(), Object.class.getDeclaredConstructor(new Class[0]));
instance = constructor.newInstance();
((Proxy) instance).setHandler(handler);
} catch (Exception e) {
throw new Error("Unable instantiate SessionFactory proxy", e);
}