Search in sources :

Example 36 with Advisor

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

the class MockitoAopProxyTargetInterceptor method applyTo.

@Autowired
public static void applyTo(Object source) {
    Assert.state(AopUtils.isAopProxy(source), "Source must be an AOP proxy");
    try {
        Advised advised = (Advised) source;
        for (Advisor advisor : advised.getAdvisors()) {
            if (advisor instanceof MockitoAopProxyTargetInterceptor) {
                return;
            }
        }
        Object target = AopTestUtils.getUltimateTargetObject(source);
        Advice advice = new MockitoAopProxyTargetInterceptor(source, target);
        advised.addAdvice(0, advice);
    } catch (Exception ex) {
        throw new IllegalStateException("Unable to apply Mockito AOP support", ex);
    }
}
Also used : Advised(org.springframework.aop.framework.Advised) Advisor(org.springframework.aop.Advisor) Advice(org.aopalliance.aop.Advice) Autowired(org.springframework.beans.factory.annotation.Autowired)

Example 37 with Advisor

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

the class AdvisedSupport method removeAdvisor.

@Override
public void removeAdvisor(int index) throws AopConfigException {
    if (isFrozen()) {
        throw new AopConfigException("Cannot remove Advisor: Configuration is frozen.");
    }
    if (index < 0 || index > this.advisors.size() - 1) {
        throw new AopConfigException("Advisor index " + index + " is out of bounds: " + "This configuration only has " + this.advisors.size() + " advisors.");
    }
    Advisor advisor = this.advisors.get(index);
    if (advisor instanceof IntroductionAdvisor) {
        IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
        // We need to remove introduction interfaces.
        for (int j = 0; j < ia.getInterfaces().length; j++) {
            removeInterface(ia.getInterfaces()[j]);
        }
    }
    this.advisors.remove(index);
    updateAdvisorArray();
    adviceChanged();
}
Also used : IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Advisor(org.springframework.aop.Advisor)

Example 38 with Advisor

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

the class AdvisedSupport method addAdvisors.

/**
	 * Add all of the given advisors to this proxy configuration.
	 * @param advisors the advisors to register
	 */
public void addAdvisors(Collection<Advisor> advisors) {
    if (isFrozen()) {
        throw new AopConfigException("Cannot add advisor: Configuration is frozen.");
    }
    if (!CollectionUtils.isEmpty(advisors)) {
        for (Advisor advisor : advisors) {
            if (advisor instanceof IntroductionAdvisor) {
                validateIntroductionAdvisor((IntroductionAdvisor) advisor);
            }
            Assert.notNull(advisor, "Advisor must not be null");
            this.advisors.add(advisor);
        }
        updateAdvisorArray();
        adviceChanged();
    }
}
Also used : IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) Advisor(org.springframework.aop.Advisor)

Example 39 with Advisor

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

the class DefaultAdvisorChainFactory method getInterceptorsAndDynamicInterceptionAdvice.

@Override
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Advised config, Method method, Class<?> targetClass) {
    // This is somewhat tricky... We have to process introductions first,
    // but we need to preserve order in the ultimate list.
    List<Object> interceptorList = new ArrayList<>(config.getAdvisors().length);
    Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
    boolean hasIntroductions = hasMatchingIntroductions(config, actualClass);
    AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
    for (Advisor advisor : config.getAdvisors()) {
        if (advisor instanceof PointcutAdvisor) {
            // Add it conditionally.
            PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
            if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
                MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
                MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
                if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) {
                    if (mm.isRuntime()) {
                        // isn't a problem as we normally cache created chains.
                        for (MethodInterceptor interceptor : interceptors) {
                            interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm));
                        }
                    } else {
                        interceptorList.addAll(Arrays.asList(interceptors));
                    }
                }
            }
        } else if (advisor instanceof IntroductionAdvisor) {
            IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
            if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
                Interceptor[] interceptors = registry.getInterceptors(advisor);
                interceptorList.addAll(Arrays.asList(interceptors));
            }
        } else {
            Interceptor[] interceptors = registry.getInterceptors(advisor);
            interceptorList.addAll(Arrays.asList(interceptors));
        }
    }
    return interceptorList;
}
Also used : PointcutAdvisor(org.springframework.aop.PointcutAdvisor) IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) ArrayList(java.util.ArrayList) PointcutAdvisor(org.springframework.aop.PointcutAdvisor) IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) Advisor(org.springframework.aop.Advisor) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) GlobalAdvisorAdapterRegistry(org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry) AdvisorAdapterRegistry(org.springframework.aop.framework.adapter.AdvisorAdapterRegistry) MethodMatcher(org.springframework.aop.MethodMatcher)

Example 40 with Advisor

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

the class ProxyFactoryBean method addAdvisorOnChainCreation.

/**
	 * Invoked when advice chain is created.
	 * <p>Add the given advice, advisor or object to the interceptor list.
	 * Because of these three possibilities, we can't type the signature
	 * more strongly.
	 * @param next advice, advisor or target object
	 * @param name bean name from which we obtained this object in our owning
	 * bean factory
	 */
private void addAdvisorOnChainCreation(Object next, String name) {
    // We need to convert to an Advisor if necessary so that our source reference
    // matches what we find from superclass interceptors.
    Advisor advisor = namedBeanToAdvisor(next);
    if (logger.isTraceEnabled()) {
        logger.trace("Adding advisor with name '" + name + "'");
    }
    addAdvisor(advisor);
}
Also used : 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