/** * Create a new advisor for this DeclareParents field. * @param interfaceType static field defining the introduction * @param typePattern type pattern the introduction is restricted to * @param delegateRef the delegate implementation object */ public DeclareParentsAdvisor(Class<?> interfaceType, String typePattern, Object delegateRef) { this(interfaceType, typePattern, new DelegatingIntroductionInterceptor(delegateRef)); }
/** * Return an IntroductionAdvisor that providing a mixin * exposing statistics about the pool maintained by this object. */ public DefaultIntroductionAdvisor getPoolingConfigMixin() { DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this); return new DefaultIntroductionAdvisor(dii, PoolingConfig.class); }
/** * Return an introduction advisor mixin that allows the AOP proxy to be * cast to ThreadLocalInvokerStats. */ public IntroductionAdvisor getStatsMixin() { DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this); return new DefaultIntroductionAdvisor(dii, ThreadLocalTargetSourceStats.class); }
@Test(expected = IllegalArgumentException.class) public void testNullTarget() throws Exception { // Shouldn't accept null target new DelegatingIntroductionInterceptor(null); }
/** * Wrap each concrete endpoint instance with an AOP proxy, * exposing the message listener's interfaces as well as the * endpoint SPI through an AOP introduction. */ @Override public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException { GenericMessageEndpoint endpoint = (GenericMessageEndpoint) super.createEndpoint(xaResource); ProxyFactory proxyFactory = new ProxyFactory(getMessageListener()); DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(endpoint); introduction.suppressInterface(MethodInterceptor.class); proxyFactory.addAdvice(introduction); return (MessageEndpoint) proxyFactory.getProxy(); }
/** * @param dii */ public TimestampIntroductionAdvisor() { super(new DelegatingIntroductionInterceptor(new TimestampIntroductionInterceptor())); }
@Override public void setBeanFactory(BeanFactory beanFactory) { if (!(beanFactory instanceof ConfigurableBeanFactory)) { throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory); } ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory; this.scopedTargetSource.setBeanFactory(beanFactory); ProxyFactory pf = new ProxyFactory(); pf.copyFrom(this); pf.setTargetSource(this.scopedTargetSource); Assert.notNull(this.targetBeanName, "Property 'targetBeanName' is required"); Class<?> beanType = beanFactory.getType(this.targetBeanName); if (beanType == null) { throw new IllegalStateException("Cannot create scoped proxy for bean '" + this.targetBeanName + "': Target type could not be determined at the time of proxy creation."); } if (!isProxyTargetClass() || beanType.isInterface() || Modifier.isPrivate(beanType.getModifiers())) { pf.setInterfaces(ClassUtils.getAllInterfacesForClass(beanType, cbf.getBeanClassLoader())); } // Add an introduction that implements only the methods on ScopedObject. ScopedObject scopedObject = new DefaultScopedObject(cbf, this.scopedTargetSource.getTargetBeanName()); pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject)); // Add the AopInfrastructureBean marker to indicate that the scoped proxy // itself is not subject to auto-proxying! Only its target bean is. pf.addInterface(AopInfrastructureBean.class); this.proxy = pf.getProxy(cbf.getBeanClassLoader()); }
/** * Create a refreshable proxy for the given AOP TargetSource. * @param ts the refreshable TargetSource * @param interfaces the proxy interfaces (may be {@code null} to * indicate proxying of all interfaces implemented by the target class) * @return the generated proxy * @see RefreshableScriptTargetSource */ protected Object createRefreshableProxy(TargetSource ts, @Nullable Class<?>[] interfaces, boolean proxyTargetClass) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetSource(ts); ClassLoader classLoader = this.beanClassLoader; if (interfaces != null) { proxyFactory.setInterfaces(interfaces); } else { Class<?> targetClass = ts.getTargetClass(); if (targetClass != null) { proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.beanClassLoader)); } } if (proxyTargetClass) { classLoader = null; // force use of Class.getClassLoader() proxyFactory.setProxyTargetClass(true); } DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts); introduction.suppressInterface(TargetSource.class); proxyFactory.addAdvice(introduction); return proxyFactory.getProxy(classLoader); }
/** * Wrap each concrete endpoint instance with an AOP proxy, * exposing the message listener's interfaces as well as the * endpoint SPI through an AOP introduction. */ @Override public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException { GenericMessageEndpoint endpoint = (GenericMessageEndpoint) super.createEndpoint(xaResource); ProxyFactory proxyFactory = new ProxyFactory(getMessageListener()); DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(endpoint); introduction.suppressInterface(MethodInterceptor.class); proxyFactory.addAdvice(introduction); return (MessageEndpoint) proxyFactory.getProxy(); }
/** * Create a refreshable proxy for the given AOP TargetSource. * @param ts the refreshable TargetSource * @param interfaces the proxy interfaces (may be {@code null} to * indicate proxying of all interfaces implemented by the target class) * @return the generated proxy * @see RefreshableScriptTargetSource */ protected Object createRefreshableProxy(TargetSource ts, @Nullable Class<?>[] interfaces, boolean proxyTargetClass) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetSource(ts); ClassLoader classLoader = this.beanClassLoader; if (interfaces != null) { proxyFactory.setInterfaces(interfaces); } else { Class<?> targetClass = ts.getTargetClass(); if (targetClass != null) { proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.beanClassLoader)); } } if (proxyTargetClass) { classLoader = null; // force use of Class.getClassLoader() proxyFactory.setProxyTargetClass(true); } DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts); introduction.suppressInterface(TargetSource.class); proxyFactory.addAdvice(introduction); return proxyFactory.getProxy(classLoader); }
@Test public void testIntroductionInterceptorWithInterfaceHierarchy() throws Exception { TestBean raw = new TestBean(); assertTrue(! (raw instanceof SubTimeStamped)); ProxyFactory factory = new ProxyFactory(raw); TimeStamped ts = mock(SubTimeStamped.class); long timestamp = 111L; given(ts.getTimeStamp()).willReturn(timestamp); factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), SubTimeStamped.class)); SubTimeStamped tsp = (SubTimeStamped) factory.getProxy(); assertTrue(tsp.getTimeStamp() == timestamp); }
@Test public void testIntroductionInterceptorWithDelegation() throws Exception { TestBean raw = new TestBean(); assertTrue(! (raw instanceof TimeStamped)); ProxyFactory factory = new ProxyFactory(raw); TimeStamped ts = mock(TimeStamped.class); long timestamp = 111L; given(ts.getTimeStamp()).willReturn(timestamp); factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts))); TimeStamped tsp = (TimeStamped) factory.getProxy(); assertTrue(tsp.getTimeStamp() == timestamp); }
@Test public void testIntroductionInterceptorWithSuperInterface() throws Exception { TestBean raw = new TestBean(); assertTrue(! (raw instanceof TimeStamped)); ProxyFactory factory = new ProxyFactory(raw); TimeStamped ts = mock(SubTimeStamped.class); long timestamp = 111L; given(ts.getTimeStamp()).willReturn(timestamp); factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), TimeStamped.class)); TimeStamped tsp = (TimeStamped) factory.getProxy(); assertTrue(!(tsp instanceof SubTimeStamped)); assertTrue(tsp.getTimeStamp() == timestamp); }
@Test public void testAutomaticInterfaceRecognitionInDelegate() throws Exception { final long t = 1001L; class Tester implements TimeStamped, ITester { @Override public void foo() throws Exception { } @Override public long getTimeStamp() { return t; } } DelegatingIntroductionInterceptor ii = new DelegatingIntroductionInterceptor(new Tester()); TestBean target = new TestBean(); ProxyFactory pf = new ProxyFactory(target); pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii)); //assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1); TimeStamped ts = (TimeStamped) pf.getProxy(); assertTrue(ts.getTimeStamp() == t); ((ITester) ts).foo(); ((ITestBean) ts).getAge(); }
@Test public void testSerializableDelegatingIntroductionInterceptorSerializable() throws Exception { SerializablePerson serializableTarget = new SerializablePerson(); String name = "Tony"; serializableTarget.setName("Tony"); ProxyFactory factory = new ProxyFactory(serializableTarget); factory.addInterface(Person.class); long time = 1000; TimeStamped ts = new SerializableTimeStamped(time); factory.addAdvisor(new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts))); factory.addAdvice(new SerializableNopInterceptor()); Person p = (Person) factory.getProxy(); assertEquals(name, p.getName()); assertEquals(time, ((TimeStamped) p).getTimeStamp()); Person p1 = (Person) SerializationTestUtils.serializeAndDeserialize(p); assertEquals(name, p1.getName()); assertEquals(time, ((TimeStamped) p1).getTimeStamp()); }
@Test public void testDelegateReturnsThisIsMassagedToReturnProxy() { NestedTestBean target = new NestedTestBean(); String company = "Interface21"; target.setCompany(company); TestBean delegate = new TestBean() { @Override public ITestBean getSpouse() { return this; } }; ProxyFactory pf = new ProxyFactory(target); pf.addAdvice(new DelegatingIntroductionInterceptor(delegate)); INestedTestBean proxy = (INestedTestBean) pf.getProxy(); assertEquals(company, proxy.getCompany()); ITestBean introduction = (ITestBean) proxy; assertSame("Introduced method returning delegate returns proxy", introduction, introduction.getSpouse()); assertTrue("Introduced method returning delegate returns proxy", AopUtils.isAopProxy(introduction.getSpouse())); }
@Test public void testAdviceImplementsIntroductionInfo() throws Throwable { TestBean tb = new TestBean(); String name = "tony"; tb.setName(name); ProxyFactory pc = new ProxyFactory(tb); NopInterceptor di = new NopInterceptor(); pc.addAdvice(di); final long ts = 37; pc.addAdvice(new DelegatingIntroductionInterceptor(new TimeStamped() { @Override public long getTimeStamp() { return ts; } })); ITestBean proxied = (ITestBean) createProxy(pc); assertEquals(name, proxied.getName()); TimeStamped intro = (TimeStamped) proxied; assertEquals(ts, intro.getTimeStamp()); }
/** * Create a new advisor for this DeclareParents field. * @param interfaceType static field defining the introduction * @param typePattern type pattern the introduction is restricted to * @param delegateRef the delegate implementation object */ public DeclareParentsAdvisor(Class<?> interfaceType, String typePattern, Object delegateRef) { this(interfaceType, typePattern, new DelegatingIntroductionInterceptor(delegateRef)); }
/** * Return an introduction advisor mixin that allows the AOP proxy to be * cast to ThreadLocalInvokerStats. */ public IntroductionAdvisor getStatsMixin() { DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this); return new DefaultIntroductionAdvisor(dii, ThreadLocalTargetSourceStats.class); }
/** * Return an introduction advisor mixin that allows the AOP proxy to be * cast to ThreadLocalInvokerStats. */ public IntroductionAdvisor getStatsMixin() { DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this); return new DefaultIntroductionAdvisor(dii, ThreadLocalTargetSourceStats.class); }