/** * Wraps the service in a dynamic proxy that ensures all methods are executed with the object class's class loader * as the context class loader * * @param interfaces The interfaces to proxy * @param service The instance to proxy * @return A proxy that wraps the service */ private Object createContextClassLoaderSettingProxy(final Class<?>[] interfaces, final Object service) { final Function<Object, Object> transformer = service1 -> Proxy.newProxyInstance(DefaultComponentRegistrar.class.getClassLoader(), interfaces, new ContextClassLoaderSettingInvocationHandler(service1)); if (!(service instanceof ServiceFactory)) { return transformer.apply(service); } return new TransformingServiceFactory((ServiceFactory) service) { @Override protected Object transform(Bundle bundle, ServiceRegistration registration, Object service) { return transformer.apply(service); } }; }