Search in sources :

Example 1 with IntroductionAdvisor

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

the class AdvisedSupport method copyConfigurationFrom.

/**
	 * Copy the AOP configuration from the given AdvisedSupport object,
	 * but allow substitution of a fresh TargetSource and a given interceptor chain.
	 * @param other the AdvisedSupport object to take proxy configuration from
	 * @param targetSource the new TargetSource
	 * @param advisors the Advisors for the chain
	 */
protected void copyConfigurationFrom(AdvisedSupport other, TargetSource targetSource, List<Advisor> advisors) {
    copyFrom(other);
    this.targetSource = targetSource;
    this.advisorChainFactory = other.advisorChainFactory;
    this.interfaces = new ArrayList<>(other.interfaces);
    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 2 with IntroductionAdvisor

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

the class DelegatingIntroductionInterceptorTests method testAutomaticInterfaceRecognitionInSubclass.

@Test
public void testAutomaticInterfaceRecognitionInSubclass() throws Exception {
    final long t = 1001L;
    @SuppressWarnings("serial")
    class TestII extends DelegatingIntroductionInterceptor implements TimeStamped, ITester {

        @Override
        public void foo() throws Exception {
        }

        @Override
        public long getTimeStamp() {
            return t;
        }
    }
    DelegatingIntroductionInterceptor ii = new TestII();
    TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    IntroductionAdvisor ia = new DefaultIntroductionAdvisor(ii);
    assertTrue(ia.isPerInstance());
    pf.addAdvisor(0, ia);
    //assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
    TimeStamped ts = (TimeStamped) pf.getProxy();
    assertThat(ts, instanceOf(TimeStamped.class));
    // Shoulnd't proxy framework interfaces
    assertTrue(!(ts instanceof MethodInterceptor));
    assertTrue(!(ts instanceof IntroductionInterceptor));
    assertTrue(ts.getTimeStamp() == t);
    ((ITester) ts).foo();
    ((ITestBean) ts).getAge();
    // Test removal
    ii.suppressInterface(TimeStamped.class);
    // Note that we need to construct a new proxy factory,
    // or suppress the interface on the proxy factory
    pf = new ProxyFactory(target);
    pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
    Object o = pf.getProxy();
    assertTrue(!(o instanceof TimeStamped));
}
Also used : IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) IntroductionInterceptor(org.springframework.aop.IntroductionInterceptor) TimeStamped(org.springframework.tests.TimeStamped) ITestBean(org.springframework.tests.sample.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) INestedTestBean(org.springframework.tests.sample.beans.INestedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) Test(org.junit.Test)

Example 3 with IntroductionAdvisor

use of org.springframework.aop.IntroductionAdvisor 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 4 with IntroductionAdvisor

use of org.springframework.aop.IntroductionAdvisor 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 5 with IntroductionAdvisor

use of org.springframework.aop.IntroductionAdvisor 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)

Aggregations

IntroductionAdvisor (org.springframework.aop.IntroductionAdvisor)6 Advisor (org.springframework.aop.Advisor)5 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)3 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)3 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)2 PointcutAdvisor (org.springframework.aop.PointcutAdvisor)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1 IntroductionInterceptor (org.springframework.aop.IntroductionInterceptor)1 MethodMatcher (org.springframework.aop.MethodMatcher)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 AdvisorAdapterRegistry (org.springframework.aop.framework.adapter.AdvisorAdapterRegistry)1 GlobalAdvisorAdapterRegistry (org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry)1 TimeStamped (org.springframework.tests.TimeStamped)1 INestedTestBean (org.springframework.tests.sample.beans.INestedTestBean)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)1 TestBean (org.springframework.tests.sample.beans.TestBean)1