/** * Create a proxy for the specified {@code TargetSource} that extends * the target class of the {@code TargetSource}. * @param targetSource the TargetSource that the proxy should invoke * @return the proxy object */ public static Object getProxy(TargetSource targetSource) { if (targetSource.getTargetClass() == null) { throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class"); } ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetSource(targetSource); proxyFactory.setProxyTargetClass(true); return proxyFactory.getProxy(); }
@Override protected ProxyFactory prepareProxyFactory(Object bean, String beanName) { if (this.beanFactory != null) { AutoProxyUtils.exposeTargetClass(this.beanFactory, beanName, bean.getClass()); } ProxyFactory proxyFactory = super.prepareProxyFactory(bean, beanName); if (!proxyFactory.isProxyTargetClass() && this.beanFactory != null && AutoProxyUtils.shouldProxyTargetClass(this.beanFactory, beanName)) { proxyFactory.setProxyTargetClass(true); } return proxyFactory; }
static SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean nestedProxy() { ProxyFactory pf1 = new ProxyFactory(new SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean()); pf1.setProxyTargetClass(true); ProxyFactory pf2 = new ProxyFactory(pf1.getProxy()); pf2.setProxyTargetClass(true); return (SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean) pf2.getProxy(); } }
@Test public void testTargetClassProxiesCanBeOrderedThroughAnnotations() { ProxyFactory pf1 = new ProxyFactory(new A()); pf1.setProxyTargetClass(true); ProxyFactory pf2 = new ProxyFactory(new B()); pf2.setProxyTargetClass(true); Object proxy1 = pf1.getProxy(); Object proxy2 = pf2.getProxy(); List<Object> list = new ArrayList<>(2); list.add(proxy1); list.add(proxy2); AnnotationAwareOrderComparator.sort(list); assertSame(proxy2, list.get(0)); assertSame(proxy1, list.get(1)); }
private Foo cglibProxy(Foo foo) { ProxyFactory pf = new ProxyFactory(); pf.setTarget(foo); pf.setProxyTargetClass(true); Foo proxy = (Foo) pf.getProxy(); assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy)); assertThat(proxy, instanceOf(FooImpl.class)); return proxy; }
protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) { ProxyFactory pf = new ProxyFactory(target); if (interfaces.length > 1 || interfaces[0].isInterface()) { pf.setInterfaces(interfaces); } else { pf.setProxyTargetClass(true); } // Required everywhere we use AspectJ proxies pf.addAdvice(ExposeInvocationInterceptor.INSTANCE); pf.addAdvisors(advisors); pf.setExposeProxy(true); return pf.getProxy(); }
@Test public void getShortNameForCglibClass() { TestBean tb = new TestBean(); ProxyFactory pf = new ProxyFactory(); pf.setTarget(tb); pf.setProxyTargetClass(true); TestBean proxy = (TestBean) pf.getProxy(); String className = ClassUtils.getShortName(proxy.getClass()); assertEquals("Class name did not match", "TestBean", className); } }
@Test public void testAnnotationOnDynamicProxyMethod() throws Exception { String expression = "@annotation(test.annotation.transaction.Tx)"; AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); ajexp.setExpression(expression); ProxyFactory factory = new ProxyFactory(new BeanA()); factory.setProxyTargetClass(false); IBeanA proxy = (IBeanA) factory.getProxy(); assertTrue(ajexp.matches(IBeanA.class.getMethod("getAge"), proxy.getClass())); }
@Test public void setFieldAndGetFieldViaCglibProxy() throws Exception { ProxyFactory pf = new ProxyFactory(this.person); pf.setProxyTargetClass(true); Person proxy = (Person) pf.getProxy(); assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy)); assertSetFieldAndGetFieldBehaviorForProxy(proxy, this.person); }
@Test public void testAnnotationOnCglibProxyMethod() throws Exception { String expression = "@annotation(test.annotation.transaction.Tx)"; AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); ajexp.setExpression(expression); ProxyFactory factory = new ProxyFactory(new BeanA()); factory.setProxyTargetClass(true); BeanA proxy = (BeanA) factory.getProxy(); assertTrue(ajexp.matches(BeanA.class.getMethod("getAge"), proxy.getClass())); }
private ITestBean getIntroductionAdvisorProxy(TestBean target) { ProxyFactory pf = new ProxyFactory(ITestBean.class); pf.setProxyTargetClass(true); pf.addAdvisor(new LockMixinAdvisor()); pf.setTarget(target); pf.setFrozen(true); pf.setExposeProxy(false); return (ITestBean) pf.getProxy(); }
/** * Test the important case where the invocation is on a proxied interface method * but the attribute is defined on the target class. */ @Test public void transactionAttributeDeclaredOnCglibClassMethod() throws Exception { Method classMethod = ITestBean1.class.getMethod("getAge"); TestBean1 tb = new TestBean1(); ProxyFactory pf = new ProxyFactory(tb); pf.setProxyTargetClass(true); Object proxy = pf.getProxy(); AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); TransactionAttribute actual = atas.getTransactionAttribute(classMethod, proxy.getClass()); RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute(); rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class)); assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules()); }
@Test public void parameterAnnotationWithCglibProxy() throws JMSException { ProxyFactory pf = new ProxyFactory(sample); pf.setProxyTargetClass(true); listener = (JmsEndpointSampleBean) pf.getProxy(); containerFactory.setMessageConverter(new UpperCaseMessageConverter()); MethodJmsListenerEndpoint endpoint = createDefaultMethodJmsEndpoint( JmsEndpointSampleBean.class, "handleIt", String.class, String.class); Message message = new StubTextMessage("foo-bar"); message.setStringProperty("my-header", "my-value"); invokeListener(endpoint, message); assertListenerMethodInvocation("handleIt"); }
@Test public void testAopCglibProxy() throws Exception { ModelMap map = new ModelMap(); ProxyFactory factory = new ProxyFactory(); SomeInnerClass val = new SomeInnerClass(); factory.setTarget(val); factory.setProxyTargetClass(true); map.addAttribute(factory.getProxy()); assertTrue(map.containsKey("someInnerClass")); assertEquals(val, map.get("someInnerClass")); }
@Test public void testProxyProtectedMethod() { CountingBeforeAdvice advice = new CountingBeforeAdvice(); ProxyFactory proxyFactory = new ProxyFactory(new MyBean()); proxyFactory.addAdvice(advice); proxyFactory.setProxyTargetClass(true); MyBean proxy = (MyBean) proxyFactory.getProxy(); assertEquals(4, proxy.add(1, 3)); assertEquals(1, advice.getCalls("add")); }
@Test public void testProxyTargetClassWithConcreteClassAsTarget() { ProxyFactory pf = new ProxyFactory(); pf.setTargetClass(TestBean.class); Object proxy = pf.getProxy(); assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy)); assertTrue(proxy instanceof TestBean); assertEquals(TestBean.class, AopProxyUtils.ultimateTargetClass(proxy)); ProxyFactory pf2 = new ProxyFactory(proxy); pf2.setProxyTargetClass(true); Object proxy2 = pf2.getProxy(); assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy2)); assertTrue(proxy2 instanceof TestBean); assertEquals(TestBean.class, AopProxyUtils.ultimateTargetClass(proxy2)); }
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message, boolean proxyTargetClass) throws Exception { logAdvice.reset(); ProxyFactory factory = new ProxyFactory(target); factory.setProxyTargetClass(proxyTargetClass); factory.addAdvisor(advisor); TestService bean = (TestService) factory.getProxy(); assertEquals(0, logAdvice.getCountThrows()); try { bean.sayHello(); fail("Expected exception"); } catch (TestException ex) { assertEquals(message, ex.getMessage()); } assertEquals(1, logAdvice.getCountThrows()); }
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message, boolean proxyTargetClass) throws Exception { logAdvice.reset(); ProxyFactory factory = new ProxyFactory(target); factory.setProxyTargetClass(proxyTargetClass); factory.addAdvisor(advisor); TestService bean = (TestService) factory.getProxy(); assertEquals(0, logAdvice.getCountThrows()); try { bean.sayHello(); fail("Expected exception"); } catch (TestException ex) { assertEquals(message, ex.getMessage()); } assertEquals(1, logAdvice.getCountThrows()); }
@Test public void withInterfaceOnTargetCglibProxy() { ProxyFactory targetFactory = new ProxyFactory(); targetFactory.setTarget(new TestWithInterfaceImpl()); targetFactory.setProxyTargetClass(true); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTarget(targetFactory.getProxy()); proxyFactory.addInterface(TestWithInterface.class); proxyFactory.addAdvice(this.ti); TestWithInterface proxy = (TestWithInterface) proxyFactory.getProxy(); proxy.doSomething(); assertGetTransactionAndCommitCount(1); proxy.doSomethingElse(); assertGetTransactionAndCommitCount(2); proxy.doSomethingElse(); assertGetTransactionAndCommitCount(3); proxy.doSomething(); assertGetTransactionAndCommitCount(4); proxy.doSomethingDefault(); assertGetTransactionAndCommitCount(5); }
@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()); }