Search in sources :

Example 66 with Advisor

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

the class PerThisAspect method aspectMethodThrowsExceptionIllegalOnSignature.

// TODO document this behaviour.
// Is it different AspectJ behaviour, at least for checked exceptions?
@Test
void aspectMethodThrowsExceptionIllegalOnSignature() {
    TestBean target = new TestBean();
    RemoteException expectedException = new RemoteException();
    List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new ExceptionThrowingAspect(expectedException), "someBean"));
    assertThat(advisors.size()).as("One advice method was found").isEqualTo(1);
    ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);
    assertThatExceptionOfType(UndeclaredThrowableException.class).isThrownBy(itb::getAge).withCause(expectedException);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Advisor(org.springframework.aop.Advisor) RemoteException(java.rmi.RemoteException) Test(org.junit.jupiter.api.Test)

Example 67 with Advisor

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

the class AbstractAutoProxyCreator method createProxy.

/**
 * Create an AOP proxy for the given bean.
 * @param beanClass the class of the bean
 * @param beanName the name of the bean
 * @param specificInterceptors the set of interceptors that is
 * specific to this bean (may be empty, but not null)
 * @param targetSource the TargetSource for the proxy,
 * already pre-configured to access the bean
 * @return the AOP proxy for the bean
 * @see #buildAdvisors
 */
protected Object createProxy(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, TargetSource targetSource) {
    if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
        AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
    }
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.copyFrom(this);
    if (proxyFactory.isProxyTargetClass()) {
        // Explicit handling of JDK proxy targets (for introduction advice scenarios)
        if (Proxy.isProxyClass(beanClass)) {
            // Must allow for introductions; can't just set interfaces to the proxy's interfaces only.
            for (Class<?> ifc : beanClass.getInterfaces()) {
                proxyFactory.addInterface(ifc);
            }
        }
    } else {
        // No proxyTargetClass flag enforced, let's apply our default checks...
        if (shouldProxyTargetClass(beanClass, beanName)) {
            proxyFactory.setProxyTargetClass(true);
        } else {
            evaluateProxyInterfaces(beanClass, proxyFactory);
        }
    }
    Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
    proxyFactory.addAdvisors(advisors);
    proxyFactory.setTargetSource(targetSource);
    customizeProxyFactory(proxyFactory);
    proxyFactory.setFrozen(this.freezeProxy);
    if (advisorsPreFiltered()) {
        proxyFactory.setPreFiltered(true);
    }
    // Use original ClassLoader if bean class not locally loaded in overriding class loader
    ClassLoader classLoader = getProxyClassLoader();
    if (classLoader instanceof SmartClassLoader && classLoader != beanClass.getClassLoader()) {
        classLoader = ((SmartClassLoader) classLoader).getOriginalClassLoader();
    }
    return proxyFactory.getProxy(classLoader);
}
Also used : ProxyFactory(org.springframework.aop.framework.ProxyFactory) SmartClassLoader(org.springframework.core.SmartClassLoader) Advisor(org.springframework.aop.Advisor) SmartClassLoader(org.springframework.core.SmartClassLoader) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 68 with Advisor

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

the class PerThisAspect method introductionWithArgumentBinding.

// TODO: Why does this test fail? It hasn't been run before, so it maybe never actually passed...
@Test
@Disabled
void introductionWithArgumentBinding() {
    TestBean target = new TestBean();
    List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeITestBeanModifiable(), "someBean"));
    advisors.addAll(getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")));
    Modifiable modifiable = (Modifiable) createProxy(target, advisors, ITestBean.class);
    assertThat(modifiable).isInstanceOf(Modifiable.class);
    Lockable lockable = (Lockable) modifiable;
    assertThat(lockable.locked()).isFalse();
    ITestBean itb = (ITestBean) modifiable;
    assertThat(modifiable.isModified()).isFalse();
    int oldAge = itb.getAge();
    itb.setAge(oldAge + 1);
    assertThat(modifiable.isModified()).isTrue();
    modifiable.acceptChanges();
    assertThat(modifiable.isModified()).isFalse();
    itb.setAge(itb.getAge());
    assertThat(modifiable.isModified()).as("Setting same value does not modify").isFalse();
    itb.setName("And now for something completely different");
    assertThat(modifiable.isModified()).isTrue();
    lockable.lock();
    assertThat(lockable.locked()).isTrue();
    assertThatIllegalStateException().as("Should be locked").isThrownBy(() -> itb.setName("Else"));
    lockable.unlock();
    itb.setName("Tony");
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Advisor(org.springframework.aop.Advisor) Lockable(test.aop.Lockable) DefaultLockable(test.aop.DefaultLockable) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 69 with Advisor

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

the class TestNamespaceHandler method testChainedDecorators.

@Test
public void testChainedDecorators() throws Exception {
    ITestBean bean = (ITestBean) this.beanFactory.getBean("chainedTestBean");
    assertTestBean(bean);
    assertThat(AopUtils.isAopProxy(bean)).isTrue();
    Advisor[] advisors = ((Advised) bean).getAdvisors();
    assertThat(advisors.length).as("Incorrect number of advisors").isEqualTo(2);
    assertThat(advisors[0].getAdvice().getClass()).as("Incorrect advice class").isEqualTo(DebugInterceptor.class);
    assertThat(advisors[1].getAdvice().getClass()).as("Incorrect advice class").isEqualTo(NopInterceptor.class);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 70 with Advisor

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

the class AspectJAwareAdvisorAutoProxyCreator method sortAdvisors.

/**
 * Sort the supplied {@link Advisor} instances according to AspectJ precedence.
 * <p>If two pieces of advice come from the same aspect, they will have the same
 * order. Advice from the same aspect is then further ordered according to the
 * following rules:
 * <ul>
 * <li>If either of the pair is <em>after</em> advice, then the advice declared
 * last gets highest precedence (i.e., runs last).</li>
 * <li>Otherwise the advice declared first gets highest precedence (i.e., runs
 * first).</li>
 * </ul>
 * <p><b>Important:</b> Advisors are sorted in precedence order, from highest
 * precedence to lowest. "On the way in" to a join point, the highest precedence
 * advisor should run first. "On the way out" of a join point, the highest
 * precedence advisor should run last.
 */
@Override
protected List<Advisor> sortAdvisors(List<Advisor> advisors) {
    List<PartiallyComparableAdvisorHolder> partiallyComparableAdvisors = new ArrayList<>(advisors.size());
    for (Advisor advisor : advisors) {
        partiallyComparableAdvisors.add(new PartiallyComparableAdvisorHolder(advisor, DEFAULT_PRECEDENCE_COMPARATOR));
    }
    List<PartiallyComparableAdvisorHolder> sorted = PartialOrder.sort(partiallyComparableAdvisors);
    if (sorted != null) {
        List<Advisor> result = new ArrayList<>(advisors.size());
        for (PartiallyComparableAdvisorHolder pcAdvisor : sorted) {
            result.add(pcAdvisor.getAdvisor());
        }
        return result;
    } else {
        return super.sortAdvisors(advisors);
    }
}
Also used : ArrayList(java.util.ArrayList) AspectJPointcutAdvisor(org.springframework.aop.aspectj.AspectJPointcutAdvisor) Advisor(org.springframework.aop.Advisor)

Aggregations

Advisor (org.springframework.aop.Advisor)70 Test (org.junit.jupiter.api.Test)33 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)25 Advised (org.springframework.aop.framework.Advised)21 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)19 Test (org.junit.Test)16 TestBean (org.springframework.beans.testfixture.beans.TestBean)16 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)14 AspectJPointcutAdvisor (org.springframework.aop.aspectj.AspectJPointcutAdvisor)11 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)11 ArrayList (java.util.ArrayList)10 JoinPoint (org.aspectj.lang.JoinPoint)8 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)8 Method (java.lang.reflect.Method)7 SyntheticInstantiationAdvisor (org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor)7 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)7 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)7 LockMixinAdvisor (test.mixin.LockMixinAdvisor)7 Advice (org.aopalliance.aop.Advice)6 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)6