private <T> T createProxyInstance(com.mongodb.session.ClientSession session, T target, Class<T> targetType) { ProxyFactory factory = new ProxyFactory(); factory.setTarget(target); factory.setInterfaces(targetType); factory.setOpaque(true); factory.addAdvice(new SessionAwareMethodInterceptor<>(session, target, ClientSession.class, MongoDatabase.class, this::proxyDatabase, MongoCollection.class, this::proxyCollection)); return targetType.cast(factory.getProxy()); } }
private <T> T createProxyInstance(com.mongodb.session.ClientSession session, T target, Class<T> targetType) { ProxyFactory factory = new ProxyFactory(); factory.setTarget(target); factory.setInterfaces(targetType); factory.setOpaque(true); factory.addAdvice(new SessionAwareMethodInterceptor<>(session, target, ClientSession.class, MongoDatabase.class, this::proxyDatabase, MongoCollection.class, this::proxyCollection)); return targetType.cast(factory.getProxy()); } }
/** * Get a proxy for the given service object, implementing the specified * service interface. * <p>Used to export a proxy that does not expose any internals but just * a specific interface intended for remote access. Furthermore, a * {@link RemoteInvocationTraceInterceptor} will be registered (by default). * @return the proxy * @see #setServiceInterface * @see #setRegisterTraceInterceptor * @see RemoteInvocationTraceInterceptor */ protected Object getProxyForService() { checkService(); checkServiceInterface(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addInterface(getServiceInterface()); if (this.registerTraceInterceptor != null ? this.registerTraceInterceptor : this.interceptors == null) { proxyFactory.addAdvice(new RemoteInvocationTraceInterceptor(getExporterName())); } if (this.interceptors != null) { AdvisorAdapterRegistry adapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); for (Object interceptor : this.interceptors) { proxyFactory.addAdvisor(adapterRegistry.wrap(interceptor)); } } proxyFactory.setTarget(getService()); proxyFactory.setOpaque(true); return proxyFactory.getProxy(getBeanClassLoader()); }
/** * Get a proxy for the given service object, implementing the specified * service interface. * <p>Used to export a proxy that does not expose any internals but just * a specific interface intended for remote access. Furthermore, a * {@link RemoteInvocationTraceInterceptor} will be registered (by default). * @return the proxy * @see #setServiceInterface * @see #setRegisterTraceInterceptor * @see RemoteInvocationTraceInterceptor */ protected Object getProxyForService() { checkService(); checkServiceInterface(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addInterface(getServiceInterface()); if (this.registerTraceInterceptor != null ? this.registerTraceInterceptor : this.interceptors == null) { proxyFactory.addAdvice(new RemoteInvocationTraceInterceptor(getExporterName())); } if (this.interceptors != null) { AdvisorAdapterRegistry adapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); for (Object interceptor : this.interceptors) { proxyFactory.addAdvisor(adapterRegistry.wrap(interceptor)); } } proxyFactory.setTarget(getService()); proxyFactory.setOpaque(true); return proxyFactory.getProxy(getBeanClassLoader()); }
@Test public void testAddAdviceAtRuntime() { TestBean bean = new TestBean(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); ProxyFactory pf = new ProxyFactory(); pf.setTarget(bean); pf.setFrozen(false); pf.setOpaque(false); pf.setProxyTargetClass(true); TestBean proxy = (TestBean) pf.getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); proxy.getAge(); assertEquals(0, cba.getCalls()); ((Advised) proxy).addAdvice(cba); proxy.getAge(); assertEquals(1, cba.getCalls()); }
@Test public void testCanPreventCastToAdvisedUsingOpaque() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(ITestBean.class); pc.addAdvice(new NopInterceptor()); CountingBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba); pc.addAdvisor(advisor); assertFalse("Opaque defaults to false", pc.isOpaque()); pc.setOpaque(true); assertTrue("Opaque now true for this config", pc.isOpaque()); ITestBean proxied = (ITestBean) createProxy(pc); proxied.setAge(10); assertEquals(10, proxied.getAge()); assertEquals(1, mba.getCalls()); assertFalse("Cannot be cast to Advised", proxied instanceof Advised); }
private <T> T createProxyInstance(com.mongodb.session.ClientSession session, T target, Class<T> targetType) { ProxyFactory factory = new ProxyFactory(); factory.setTarget(target); factory.setInterfaces(targetType); factory.setOpaque(true); factory.addAdvice(new SessionAwareMethodInterceptor<>(session, target, ClientSession.class, MongoDatabase.class, this::proxyDatabase, MongoCollection.class, this::proxyCollection)); return targetType.cast(factory.getProxy()); } }
private <T> T createProxyInstance(com.mongodb.session.ClientSession session, T target, Class<T> targetType) { ProxyFactory factory = new ProxyFactory(); factory.setTarget(target); factory.setInterfaces(targetType); factory.setOpaque(true); factory.addAdvice(new SessionAwareMethodInterceptor<>(session, target, ClientSession.class, MongoDatabase.class, this::proxyDatabase, MongoCollection.class, this::proxyCollection)); return targetType.cast(factory.getProxy()); } }
@Override @SuppressWarnings("unchecked") public <T> T createProjection(Class<T> projectionType, Object source) { Assert.notNull(projectionType, "Projection type must not be null!"); Assert.notNull(source, "Source must not be null!"); Assert.isTrue(projectionType.isInterface(), "Projection type must be an interface!"); if (projectionType.isInstance(source)) { return (T) source; } ProxyFactory factory = new ProxyFactory(); factory.setTarget(source); factory.setOpaque(true); factory.setInterfaces(projectionType, TargetAware.class); factory.addAdvice(new DefaultMethodInvokingMethodInterceptor()); factory.addAdvice(new TargetAwareMethodInterceptor(source.getClass())); factory.addAdvice(getMethodInterceptor(source, projectionType)); return (T) factory.getProxy(classLoader == null ? ClassUtils.getDefaultClassLoader() : classLoader); }
factory.setOpaque(true); boolean isSecurityOn = (System.getSecurityManager() != null); try {
/** */ public HibernateBeanSerializer(T object,String... excludesProperties) { if(object == null) { this.proxy = null; }else { ProxyFactory pf = new ProxyFactory(); pf.setTargetClass(object.getClass()); pf.setOptimize(true); pf.setTarget(object); pf.setProxyTargetClass(true); pf.setOpaque(true); pf.setExposeProxy(true); pf.setPreFiltered(true); pf.setInterfaces(new Class[]{HibernateBeanSerializerProxy.class}); HibernateBeanSerializerAdvice beanSerializerAdvice = new HibernateBeanSerializerAdvice(); beanSerializerAdvice.setExcludesProperties(excludesProperties); pf.addAdvice(beanSerializerAdvice); this.proxy = (T)pf.getProxy(); } }
/** * Get a proxy for the given service object, implementing the specified * service interface. * <p>Used to export a proxy that does not expose any internals but just * a specific interface intended for remote access. Furthermore, a * {@link RemoteInvocationTraceInterceptor} will be registered (by default). * @return the proxy * @see #setServiceInterface * @see #setRegisterTraceInterceptor * @see RemoteInvocationTraceInterceptor */ protected Object getProxyForService() { checkService(); checkServiceInterface(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addInterface(getServiceInterface()); if (this.registerTraceInterceptor != null ? this.registerTraceInterceptor : this.interceptors == null) { proxyFactory.addAdvice(new RemoteInvocationTraceInterceptor(getExporterName())); } if (this.interceptors != null) { AdvisorAdapterRegistry adapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); for (Object interceptor : this.interceptors) { proxyFactory.addAdvisor(adapterRegistry.wrap(interceptor)); } } proxyFactory.setTarget(getService()); proxyFactory.setOpaque(true); return proxyFactory.getProxy(getBeanClassLoader()); }