Search in sources :

Example 1 with ProxyFactory

use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.

the class MethodInvocationProceedingJoinPointTests method testCanGetMethodSignatureFromJoinPoint.

@Test
public void testCanGetMethodSignatureFromJoinPoint() {
    final Object raw = new TestBean();
    // Will be set by advice during a method call
    final int newAge = 23;
    ProxyFactory pf = new ProxyFactory(raw);
    pf.setExposeProxy(true);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice(new MethodBeforeAdvice() {

        private int depth;

        @Override
        public void before(Method method, Object[] args, Object target) throws Throwable {
            JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
            assertTrue("Method named in toString", jp.toString().contains(method.getName()));
            // Ensure that these don't cause problems
            jp.toShortString();
            jp.toLongString();
            assertSame(target, AbstractAspectJAdvice.currentJoinPoint().getTarget());
            assertFalse(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget()));
            ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
            assertTrue(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis()));
            assertNotSame(target, thisProxy);
            // Check getting again doesn't cause a problem
            assertSame(thisProxy, AbstractAspectJAdvice.currentJoinPoint().getThis());
            // Be sure to increment depth to avoid infinite recursion
            if (depth++ == 0) {
                // Check that toString doesn't cause a problem
                thisProxy.toString();
                // Change age, so this will be returned by invocation
                thisProxy.setAge(newAge);
                assertEquals(newAge, thisProxy.getAge());
            }
            assertSame(AopContext.currentProxy(), thisProxy);
            assertSame(target, raw);
            assertSame(method.getName(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getName());
            assertEquals(method.getModifiers(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers());
            MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
            assertSame("Return same MethodSignature repeatedly", msig, AbstractAspectJAdvice.currentJoinPoint().getSignature());
            assertSame("Return same JoinPoint repeatedly", AbstractAspectJAdvice.currentJoinPoint(), AbstractAspectJAdvice.currentJoinPoint());
            assertEquals(method.getDeclaringClass(), msig.getDeclaringType());
            assertTrue(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes()));
            assertEquals(method.getReturnType(), msig.getReturnType());
            assertTrue(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes()));
            msig.toLongString();
            msig.toShortString();
        }
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    assertEquals("Advice reentrantly set age", newAge, itb.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) Method(java.lang.reflect.Method) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.Test)

Example 2 with ProxyFactory

use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.

the class MethodInvocationProceedingJoinPointTests method testCanGetStaticPartFromJoinPoint.

@Test
public void testCanGetStaticPartFromJoinPoint() {
    final Object raw = new TestBean();
    ProxyFactory pf = new ProxyFactory(raw);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice(new MethodBeforeAdvice() {

        @Override
        public void before(Method method, Object[] args, Object target) throws Throwable {
            StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart();
            assertEquals("Same static part must be returned on subsequent requests", staticPart, AbstractAspectJAdvice.currentJoinPoint().getStaticPart());
            assertEquals(ProceedingJoinPoint.METHOD_EXECUTION, staticPart.getKind());
            assertSame(AbstractAspectJAdvice.currentJoinPoint().getSignature(), staticPart.getSignature());
            assertEquals(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(), staticPart.getSourceLocation());
        }
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    itb.getAge();
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) Method(java.lang.reflect.Method) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) Test(org.junit.Test)

Example 3 with ProxyFactory

use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.

the class MethodInvocationProceedingJoinPointTests method testCanGetSourceLocationFromJoinPoint.

@Test
public void testCanGetSourceLocationFromJoinPoint() {
    final Object raw = new TestBean();
    ProxyFactory pf = new ProxyFactory(raw);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice(new MethodBeforeAdvice() {

        @Override
        public void before(Method method, Object[] args, Object target) throws Throwable {
            SourceLocation sloc = AbstractAspectJAdvice.currentJoinPoint().getSourceLocation();
            assertEquals("Same source location must be returned on subsequent requests", sloc, AbstractAspectJAdvice.currentJoinPoint().getSourceLocation());
            assertEquals(TestBean.class, sloc.getWithinType());
            try {
                sloc.getLine();
                fail("Can't get line number");
            } catch (UnsupportedOperationException ex) {
            // Expected
            }
            try {
                sloc.getFileName();
                fail("Can't get file name");
            } catch (UnsupportedOperationException ex) {
            // Expected
            }
        }
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    itb.getAge();
}
Also used : SourceLocation(org.aspectj.lang.reflect.SourceLocation) ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 4 with ProxyFactory

use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.

the class ScopedProxyFactoryBean method setBeanFactory.

@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);
    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());
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Example 5 with ProxyFactory

use of org.springframework.aop.framework.ProxyFactory in project spring-framework by spring-projects.

the class ContextAnnotationAutowireCandidateResolver method buildLazyResolutionProxy.

protected Object buildLazyResolutionProxy(final DependencyDescriptor descriptor, final String beanName) {
    Assert.state(getBeanFactory() instanceof DefaultListableBeanFactory, "BeanFactory needs to be a DefaultListableBeanFactory");
    final DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) getBeanFactory();
    TargetSource ts = new TargetSource() {

        @Override
        public Class<?> getTargetClass() {
            return descriptor.getDependencyType();
        }

        @Override
        public boolean isStatic() {
            return false;
        }

        @Override
        public Object getTarget() {
            Object target = beanFactory.doResolveDependency(descriptor, beanName, null, null);
            if (target == null) {
                throw new NoSuchBeanDefinitionException(descriptor.getResolvableType(), "Optional dependency not present for lazy injection point");
            }
            return target;
        }

        @Override
        public void releaseTarget(Object target) {
        }
    };
    ProxyFactory pf = new ProxyFactory();
    pf.setTargetSource(ts);
    Class<?> dependencyType = descriptor.getDependencyType();
    if (dependencyType.isInterface()) {
        pf.addInterface(dependencyType);
    }
    return pf.getProxy(beanFactory.getBeanClassLoader());
}
Also used : TargetSource(org.springframework.aop.TargetSource) ProxyFactory(org.springframework.aop.framework.ProxyFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Aggregations

ProxyFactory (org.springframework.aop.framework.ProxyFactory)78 Test (org.junit.Test)48 ITestBean (org.springframework.tests.sample.beans.ITestBean)20 TestBean (org.springframework.tests.sample.beans.TestBean)20 TimeStamped (org.springframework.tests.TimeStamped)8 INestedTestBean (org.springframework.tests.sample.beans.INestedTestBean)8 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)8 Method (java.lang.reflect.Method)6 HashMap (java.util.HashMap)5 MethodBeforeAdvice (org.springframework.aop.MethodBeforeAdvice)4 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)4 Context (javax.naming.Context)3 TargetSource (org.springframework.aop.TargetSource)3 Advised (org.springframework.aop.framework.Advised)3 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)3 IOException (java.io.IOException)2 EJBLocalObject (javax.ejb.EJBLocalObject)2 Message (javax.jms.Message)2 TextMessage (javax.jms.TextMessage)2 ObjectName (javax.management.ObjectName)2