/** * Return the proxy object to expose. * <p>The default implementation uses a {@code getProxy} call with * the factory's bean class loader. Can be overridden to specify a * custom class loader. * @param aopProxy the prepared AopProxy instance to get the proxy from * @return the proxy object to expose * @see AopProxy#getProxy(ClassLoader) */ protected Object getProxy(AopProxy aopProxy) { return aopProxy.getProxy(this.proxyClassLoader); }
/** * Create a new proxy according to the settings in this factory. * <p>Can be called repeatedly. Effect will vary if we've added * or removed interfaces. Can add and remove interceptors. * <p>Uses the given class loader (if necessary for proxy creation). * @param classLoader the class loader to create the proxy with * @return the new proxy */ @SuppressWarnings("unchecked") public <T> T getProxy(ClassLoader classLoader) { return (T) createAopProxy().getProxy(classLoader); }
/** * Create a new proxy according to the settings in this factory. * <p>Can be called repeatedly. Effect will vary if we've added * or removed interfaces. Can add and remove interceptors. * <p>Uses a default class loader: Usually, the thread context class loader * (if necessary for proxy creation). * @return the proxy object */ public Object getProxy() { return createAopProxy().getProxy(); }
/** * Create a new proxy according to the settings in this factory. * <p>Can be called repeatedly. Effect will vary if we've added * or removed interfaces. Can add and remove interceptors. * <p>Uses a default class loader: Usually, the thread context class loader * (if necessary for proxy creation). * @return the new proxy */ @SuppressWarnings("unchecked") public <T> T getProxy() { return (T) createAopProxy().getProxy(); }
/** * Create a new proxy according to the settings in this factory. * <p>Can be called repeatedly. Effect will vary if we've added * or removed interfaces. Can add and remove interceptors. * <p>Uses the given class loader (if necessary for proxy creation). * @param classLoader the class loader to create the proxy with * (or {@code null} for the low-level proxy facility's default) * @return the proxy object */ public Object getProxy(@Nullable ClassLoader classLoader) { return createAopProxy().getProxy(classLoader); }
@Test(expected = AopConfigException.class) public void testNoInterceptorsAndNoTarget() { AdvisedSupport pc = new AdvisedSupport(ITestBean.class); // Add no interceptors AopProxy aop = createAopProxy(pc); aop.getProxy(); }
@Override protected Object createProxy(ProxyCreatorSupport as) { assertFalse("Not forcible CGLIB", as.isProxyTargetClass()); Object proxy = as.createAopProxy().getProxy(); assertTrue("Should be a JDK proxy: " + proxy.getClass(), AopUtils.isJdkDynamicProxy(proxy)); return proxy; }
@Override protected Object createProxy(ProxyCreatorSupport as) { as.setProxyTargetClass(true); Object proxy = as.createAopProxy().getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); return proxy; }
@Test(expected = AopConfigException.class) public void testNoTarget() { AdvisedSupport pc = new AdvisedSupport(ITestBean.class); pc.addAdvice(new NopInterceptor()); AopProxy aop = createAopProxy(pc); aop.getProxy(); }
@Test public void testProtectedMethodInvocation() { ProtectedMethodTestBean bean = new ProtectedMethodTestBean(); bean.value = "foo"; mockTargetSource.setTarget(bean); AdvisedSupport as = new AdvisedSupport(); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); AopProxy aop = new CglibAopProxy(as); ProtectedMethodTestBean proxy = (ProtectedMethodTestBean) aop.getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); assertEquals(proxy.getClass().getClassLoader(), bean.getClass().getClassLoader()); assertEquals("foo", proxy.getString()); }
@Test public void testPackageMethodInvocation() { PackageMethodTestBean bean = new PackageMethodTestBean(); bean.value = "foo"; mockTargetSource.setTarget(bean); AdvisedSupport as = new AdvisedSupport(); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); AopProxy aop = new CglibAopProxy(as); PackageMethodTestBean proxy = (PackageMethodTestBean) aop.getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); assertEquals(proxy.getClass().getClassLoader(), bean.getClass().getClassLoader()); assertEquals("foo", proxy.getString()); }
@Test public void testInterceptorIsInvokedWithNoTarget() { // Test return value final int age = 25; MethodInterceptor mi = (invocation -> age); AdvisedSupport pc = new AdvisedSupport(ITestBean.class); pc.addAdvice(mi); AopProxy aop = createAopProxy(pc); ITestBean tb = (ITestBean) aop.getProxy(); assertEquals("correct return value", age, tb.getAge()); }
@Before public void setup() { converter = new Jaxb2RootElementHttpMessageConverter(); rootElement = new RootElement(); DefaultAopProxyFactory proxyFactory = new DefaultAopProxyFactory(); AdvisedSupport advisedSupport = new AdvisedSupport(); advisedSupport.setTarget(rootElement); advisedSupport.setProxyTargetClass(true); AopProxy proxy = proxyFactory.createAopProxy(advisedSupport); rootElementCglib = (RootElement) proxy.getProxy(); }
/** * Check that although a method is eligible for advice chain optimization and * direct reflective invocation, it doesn't happen if we've asked to see the proxy, * so as to guarantee a consistent programming model. * @throws Throwable */ @Test public void testTargetCanGetInvocationEvenIfNoAdviceChain() throws Throwable { NeedsToSeeProxy target = new NeedsToSeeProxy(); AdvisedSupport pc = new AdvisedSupport(INeedsToSeeProxy.class); pc.setTarget(target); pc.setExposeProxy(true); // Now let's try it with the special target AopProxy aop = createAopProxy(pc); INeedsToSeeProxy proxied = (INeedsToSeeProxy) aop.getProxy(); // It will complain if it can't get the proxy proxied.incrementViaProxy(); }
@Test public void testMethodInvocationDuringConstructor() { CglibTestBean bean = new CglibTestBean(); bean.setName("Rob Harrop"); AdvisedSupport as = new AdvisedSupport(); as.setTarget(bean); as.addAdvice(new NopInterceptor()); AopProxy aop = new CglibAopProxy(as); CglibTestBean proxy = (CglibTestBean) aop.getProxy(); assertEquals("The name property has been overwritten by the constructor", "Rob Harrop", proxy.getName()); }
@Test public void testSerializationAdviceAndTargetNotSerializable() throws Exception { TestBean tb = new TestBean(); assertFalse(SerializationTestUtils.isSerializable(tb)); ProxyFactory pf = new ProxyFactory(tb); pf.addAdvice(new NopInterceptor()); ITestBean proxy = (ITestBean) createAopProxy(pf).getProxy(); assertFalse(SerializationTestUtils.isSerializable(proxy)); }
@Test public void testToStringInvocation() { PrivateCglibTestBean bean = new PrivateCglibTestBean(); bean.setName("Rob Harrop"); AdvisedSupport as = new AdvisedSupport(); as.setTarget(bean); as.addAdvice(new NopInterceptor()); AopProxy aop = new CglibAopProxy(as); PrivateCglibTestBean proxy = (PrivateCglibTestBean) aop.getProxy(); assertEquals("The name property has been overwritten by the constructor", "Rob Harrop", proxy.toString()); }
private void testManyProxies(int howMany) { int age1 = 33; TestBean target1 = new TestBean(); target1.setAge(age1); ProxyFactory pf1 = new ProxyFactory(target1); pf1.addAdvice(new NopInterceptor()); pf1.addAdvice(new NopInterceptor()); ITestBean[] proxies = new ITestBean[howMany]; for (int i = 0; i < howMany; i++) { proxies[i] = (ITestBean) createAopProxy(pf1).getProxy(); assertEquals(age1, proxies[i].getAge()); } }
@Test public void testSerializationAdviceNotSerializable() throws Exception { SerializablePerson sp = new SerializablePerson(); assertTrue(SerializationTestUtils.isSerializable(sp)); ProxyFactory pf = new ProxyFactory(sp); // This isn't serializable Advice i = new NopInterceptor(); pf.addAdvice(i); assertFalse(SerializationTestUtils.isSerializable(i)); Object proxy = createAopProxy(pf).getProxy(); assertFalse(SerializationTestUtils.isSerializable(proxy)); }
@Test public void testProxyCanBeClassNotInterface() { TestBean raw = new TestBean(); raw.setAge(32); mockTargetSource.setTarget(raw); AdvisedSupport pc = new AdvisedSupport(); pc.setTargetSource(mockTargetSource); AopProxy aop = new CglibAopProxy(pc); Object proxy = aop.getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); assertTrue(proxy instanceof ITestBean); assertTrue(proxy instanceof TestBean); TestBean tb = (TestBean) proxy; assertEquals(32, tb.getAge()); }