org.springframework.aop.Advisor Java Examples

The following examples show how to use org.springframework.aop.Advisor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Check that the string is informative.
 */
@Test
public void testProxyConfigString() {
	TestBean target = new TestBean();
	ProxyFactory pc = new ProxyFactory(target);
	pc.setInterfaces(ITestBean.class);
	pc.addAdvice(new NopInterceptor());
	MethodBeforeAdvice mba = new CountingBeforeAdvice();
	Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
	pc.addAdvisor(advisor);
	ITestBean proxied = (ITestBean) createProxy(pc);

	String proxyConfigString = ((Advised) proxied).toProxyConfigString();
	assertTrue(proxyConfigString.contains(advisor.toString()));
	assertTrue(proxyConfigString.contains("1 interface"));
}
 
Example #2
Source File: AspectJAwareAdvisorAutoProxyCreator.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Sort the rest by AspectJ precedence. If two pieces of advice have
 * 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 after advice, then the advice declared
 * last gets highest precedence (runs last)</li>
 * <li>otherwise the advice declared first gets highest precedence (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
@SuppressWarnings("unchecked")
protected List<Advisor> sortAdvisors(List<Advisor> advisors) {
	List<PartiallyComparableAdvisorHolder> partiallyComparableAdvisors = new ArrayList<>(advisors.size());
	for (Advisor element : advisors) {
		partiallyComparableAdvisors.add(
				new PartiallyComparableAdvisorHolder(element, 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);
	}
}
 
Example #3
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAspectMethodThrowsExceptionLegalOnSignature() {
	TestBean target = new TestBean();
	UnsupportedOperationException expectedException = new UnsupportedOperationException();
	List<Advisor> advisors = getFixture().getAdvisors(
			new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException), "someBean"));
	assertEquals("One advice method was found", 1, advisors.size());
	ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);

	try {
		itb.getAge();
		fail();
	}
	catch (UnsupportedOperationException ex) {
		assertSame(expectedException, ex);
	}
}
 
Example #4
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAspectMethodThrowsExceptionLegalOnSignature() {
	TestBean target = new TestBean();
	UnsupportedOperationException expectedException = new UnsupportedOperationException();
	List<Advisor> advisors = getFixture().getAdvisors(
			new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException), "someBean"));
	assertEquals("One advice method was found", 1, advisors.size());
	ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);

	try {
		itb.getAge();
		fail();
	}
	catch (UnsupportedOperationException ex) {
		assertSame(expectedException, ex);
	}
}
 
Example #5
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
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();
}
 
Example #6
Source File: AspectJAwareAdvisorAutoProxyCreator.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Sort the rest by AspectJ precedence. If two pieces of advice have
 * 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 after advice, then the advice declared
 * last gets highest precedence (runs last)</li>
 * <li>otherwise the advice declared first gets highest precedence (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
@SuppressWarnings("unchecked")
protected List<Advisor> sortAdvisors(List<Advisor> advisors) {
	List<PartiallyComparableAdvisorHolder> partiallyComparableAdvisors = new ArrayList<>(advisors.size());
	for (Advisor element : advisors) {
		partiallyComparableAdvisors.add(
				new PartiallyComparableAdvisorHolder(element, 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);
	}
}
 
Example #7
Source File: GroovyAspectTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
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());
}
 
Example #8
Source File: ReflectiveAspectJAdvisorFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Build a {@link org.springframework.aop.aspectj.DeclareParentsAdvisor}
 * for the given introduction field.
 * <p>Resulting Advisors will need to be evaluated for targets.
 * @param introductionField the field to introspect
 * @return the Advisor instance, or {@code null} if not an Advisor
 */
@Nullable
private Advisor getDeclareParentsAdvisor(Field introductionField) {
	DeclareParents declareParents = introductionField.getAnnotation(DeclareParents.class);
	if (declareParents == null) {
		// Not an introduction field
		return null;
	}

	if (DeclareParents.class == declareParents.defaultImpl()) {
		throw new IllegalStateException("'defaultImpl' attribute must be set on DeclareParents");
	}

	return new DeclareParentsAdvisor(
			introductionField.getType(), declareParents.value(), declareParents.defaultImpl());
}
 
Example #9
Source File: DefaultAdvisorAdapterRegistry.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
	List<MethodInterceptor> interceptors = new ArrayList<>(3);
	Advice advice = advisor.getAdvice();
	if (advice instanceof MethodInterceptor) {
		interceptors.add((MethodInterceptor) advice);
	}
	for (AdvisorAdapter adapter : this.adapters) {
		if (adapter.supportsAdvice(advice)) {
			interceptors.add(adapter.getInterceptor(advisor));
		}
	}
	if (interceptors.isEmpty()) {
		throw new UnknownAdviceTypeException(advisor.getAdvice());
	}
	return interceptors.toArray(new MethodInterceptor[0]);
}
 
Example #10
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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();
}
 
Example #11
Source File: GroovyAspectTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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());
}
 
Example #12
Source File: AspectJAwareAdvisorAutoProxyCreator.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected boolean shouldSkip(Class<?> beanClass, String beanName) {
	// TODO: Consider optimization by caching the list of the aspect names
	List<Advisor> candidateAdvisors = findCandidateAdvisors();
	for (Advisor advisor : candidateAdvisors) {
		if (advisor instanceof AspectJPointcutAdvisor &&
				((AspectJPointcutAdvisor) advisor).getAspectName().equals(beanName)) {
			return true;
		}
	}
	return super.shouldSkip(beanClass, beanName);
}
 
Example #13
Source File: AbstractAutoProxyCreator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create an AOP proxy for the given bean.
 *
 * 注释 8.9 为给定的bean创建AOP代理,实际在这一步中进行切面织入,生成动态代理
 *
 * @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);

	// 决定对于给定 bean 是否应该使用 targetClass 而不是他的接口代理
	if (!proxyFactory.isProxyTargetClass()) {
		// 检查 proxyTargetClass 设置以及 preserveTargetClass 属性
		if (shouldProxyTargetClass(beanClass, beanName)) {
			proxyFactory.setProxyTargetClass(true);
		}
		else {
			// 添加代理接口
			evaluateProxyInterfaces(beanClass, proxyFactory);
		}
	}
	// 这一步中,主要将拦截器封装为增强器
	Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
	proxyFactory.addAdvisors(advisors);
	proxyFactory.setTargetSource(targetSource);
	// 定制代理
	customizeProxyFactory(proxyFactory);
	// 用来控制代理工厂被配置之后,是否含允许修改通知
	// 缺省值为 false,不允许修改代理的配置
	proxyFactory.setFrozen(this.freezeProxy);
	if (advisorsPreFiltered()) {
		proxyFactory.setPreFiltered(true);
	}
	// 生成代理,委托给了 ProxyFactory 去处理。
	return proxyFactory.getProxy(getProxyClassLoader());
}
 
Example #14
Source File: ProxyFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testIndexOfMethods() {
	TestBean target = new TestBean();
	ProxyFactory pf = new ProxyFactory(target);
	NopInterceptor nop = new NopInterceptor();
	Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
	Advised advised = (Advised) pf.getProxy();
	// Can use advised and ProxyFactory interchangeably
	advised.addAdvice(nop);
	pf.addAdvisor(advisor);
	assertEquals(-1, pf.indexOf(new NopInterceptor()));
	assertEquals(0, pf.indexOf(nop));
	assertEquals(1, pf.indexOf(advisor));
	assertEquals(-1, advised.indexOf(new DefaultPointcutAdvisor(null)));
}
 
Example #15
Source File: ProxyFactoryBean.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Return an independent advisor chain.
 * We need to do this every time a new prototype instance is returned,
 * to return distinct instances of prototype Advisors and Advices.
 */
private List<Advisor> freshAdvisorChain() {
	Advisor[] advisors = getAdvisors();
	List<Advisor> freshAdvisors = new ArrayList<>(advisors.length);
	for (Advisor advisor : advisors) {
		if (advisor instanceof PrototypePlaceholderAdvisor) {
			PrototypePlaceholderAdvisor pa = (PrototypePlaceholderAdvisor) advisor;
			if (logger.isDebugEnabled()) {
				logger.debug("Refreshing bean named '" + pa.getBeanName() + "'");
			}
			// Replace the placeholder with a fresh prototype instance resulting
			// from a getBean() lookup
			if (this.beanFactory == null) {
				throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " +
						"- cannot resolve prototype advisor '" + pa.getBeanName() + "'");
			}
			Object bean = this.beanFactory.getBean(pa.getBeanName());
			Advisor refreshedAdvisor = namedBeanToAdvisor(bean);
			freshAdvisors.add(refreshedAdvisor);
		}
		else {
			// Add the shared instance.
			freshAdvisors.add(advisor);
		}
	}
	return freshAdvisors;
}
 
Example #16
Source File: AdvisedSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void addAdvisor(int pos, Advisor advisor) throws AopConfigException {
	if (advisor instanceof IntroductionAdvisor) {
		validateIntroductionAdvisor((IntroductionAdvisor) advisor);
	}
	addAdvisorInternal(pos, advisor);
}
 
Example #17
Source File: AdvisedSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean removeAdvisor(Advisor advisor) {
	int index = indexOf(advisor);
	if (index == -1) {
		return false;
	}
	else {
		removeAdvisor(index);
		return true;
	}
}
 
Example #18
Source File: AdvisorAutoProxyCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Check that we can provide a common interceptor that will
 * appear in the chain before "specific" interceptors,
 * which are sourced from matching advisors
 */
@Test
public void testCommonInterceptorAndAdvisor() throws Exception {
	BeanFactory bf = new ClassPathXmlApplicationContext(COMMON_INTERCEPTORS_CONTEXT, CLASS);
	ITestBean test1 = (ITestBean) bf.getBean("test1");
	assertTrue(AopUtils.isAopProxy(test1));

	Lockable lockable1 = (Lockable) test1;
	NopInterceptor nop1 = (NopInterceptor) bf.getBean("nopInterceptor");
	NopInterceptor nop2 = (NopInterceptor) bf.getBean("pointcutAdvisor", Advisor.class).getAdvice();

	ITestBean test2 = (ITestBean) bf.getBean("test2");
	Lockable lockable2 = (Lockable) test2;

	// Locking should be independent; nop is shared
	assertFalse(lockable1.locked());
	assertFalse(lockable2.locked());
	// equals 2 calls on shared nop, because it's first and sees calls
	// against the Lockable interface introduced by the specific advisor
	assertEquals(2, nop1.getCount());
	assertEquals(0, nop2.getCount());
	lockable1.lock();
	assertTrue(lockable1.locked());
	assertFalse(lockable2.locked());
	assertEquals(5, nop1.getCount());
	assertEquals(0, nop2.getCount());

	PackageVisibleMethod packageVisibleMethod = (PackageVisibleMethod) bf.getBean("packageVisibleMethod");
	assertEquals(5, nop1.getCount());
	assertEquals(0, nop2.getCount());
	packageVisibleMethod.doSomething();
	assertEquals(6, nop1.getCount());
	assertEquals(1, nop2.getCount());
	assertTrue(packageVisibleMethod instanceof Lockable);
	Lockable lockable3 = (Lockable) packageVisibleMethod;
	lockable3.lock();
	assertTrue(lockable3.locked());
	lockable3.unlock();
	assertFalse(lockable3.locked());
}
 
Example #19
Source File: EnableCachingIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void assertCacheProxying(AnnotationConfigApplicationContext ctx) {
	FooRepository repo = ctx.getBean(FooRepository.class);

	boolean isCacheProxy = false;
	if (AopUtils.isAopProxy(repo)) {
		for (Advisor advisor : ((Advised)repo).getAdvisors()) {
			if (advisor instanceof BeanFactoryCacheOperationSourceAdvisor) {
				isCacheProxy = true;
				break;
			}
		}
	}
	assertTrue("FooRepository is not a cache proxy", isCacheProxy);
}
 
Example #20
Source File: AspectJPrecedenceComparatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Advisor createSpringAOPAfterAdvice(int order) {
	AfterReturningAdvice advice = new AfterReturningAdvice() {
		@Override
		public void afterReturning(@Nullable Object returnValue, Method method, Object[] args, @Nullable Object target) throws Throwable {
		}
	};
	DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(this.anyOldPointcut, advice);
	advisor.setOrder(order);
	return advisor;
}
 
Example #21
Source File: AopNamespaceHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testIsProxy() throws Exception {
	ITestBean bean = getTestBean();

	assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));

	// check the advice details
	Advised advised = (Advised) bean;
	Advisor[] advisors = advised.getAdvisors();

	assertTrue("Advisors should not be empty", advisors.length > 0);
}
 
Example #22
Source File: AspectJPrecedenceComparatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testSameAspectAfterAdvice() {
	Advisor advisor1 = createAspectJAfterAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect");
	Advisor advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect");
	assertEquals("advisor2 sorted before advisor1", 1, this.comparator.compare(advisor1, advisor2));

	advisor1 = createAspectJAfterReturningAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect");
	advisor2 = createAspectJAfterThrowingAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect");
	assertEquals("advisor1 sorted before advisor2", -1, this.comparator.compare(advisor1, advisor2));
}
 
Example #23
Source File: AspectJPrecedenceComparatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testHigherAdvisorPrecedenceNoAfterAdvice() {
	Advisor advisor1 = createSpringAOPBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER);
	Advisor advisor2 = createAspectJBeforeAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect");
	assertEquals("advisor1 sorted before advisor2", -1, this.comparator.compare(advisor1, advisor2));

	advisor1 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect");
	advisor2 = createAspectJAroundAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect");
	assertEquals("advisor1 sorted before advisor2", -1, this.comparator.compare(advisor1, advisor2));
}
 
Example #24
Source File: AspectJPrecedenceComparatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Advisor createAspectJAdvice(int advisorOrder, int adviceDeclarationOrder, String aspectName, AbstractAspectJAdvice advice) {
	advice.setDeclarationOrder(adviceDeclarationOrder);
	advice.setAspectName(aspectName);
	AspectJPointcutAdvisor advisor = new AspectJPointcutAdvisor(advice);
	advisor.setOrder(advisorOrder);
	return advisor;
}
 
Example #25
Source File: AdvisedSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Is the given advice included in any advisor within this proxy configuration?
 * @param advice the advice to check inclusion of
 * @return whether this advice instance is included
 */
public boolean adviceIncluded(@Nullable Advice advice) {
	if (advice != null) {
		for (Advisor advisor : this.advisors) {
			if (advisor.getAdvice() == advice) {
				return true;
			}
		}
	}
	return false;
}
 
Example #26
Source File: AspectJPrecedenceComparatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testSameAdvisorPrecedenceDifferentAspectAfterAdvice() {
	Advisor advisor1 = createAspectJAfterAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect");
	Advisor advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someOtherAspect");
	assertEquals("nothing to say about order here", 0, this.comparator.compare(advisor1, advisor2));

	advisor1 = createAspectJAfterReturningAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect");
	advisor2 = createAspectJAfterThrowingAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect");
	assertEquals("nothing to say about order here", 0, this.comparator.compare(advisor1, advisor2));
}
 
Example #27
Source File: DefaultAdvisorChainFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Determine whether the Advisors contain matching introductions.
 */
private static boolean hasMatchingIntroductions(Advisor[] advisors, Class<?> actualClass) {
	for (Advisor advisor : advisors) {
		if (advisor instanceof IntroductionAdvisor) {
			IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
			if (ia.getClassFilter().matches(actualClass)) {
				return true;
			}
		}
	}
	return false;
}
 
Example #28
Source File: AspectJPrecedenceComparatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testHigherAdvisorPrecedenceNoAfterAdvice() {
	Advisor advisor1 = createSpringAOPBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER);
	Advisor advisor2 = createAspectJBeforeAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect");
	assertEquals("advisor1 sorted before advisor2", -1, this.comparator.compare(advisor1, advisor2));

	advisor1 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect");
	advisor2 = createAspectJAroundAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect");
	assertEquals("advisor1 sorted before advisor2", -1, this.comparator.compare(advisor1, advisor2));
}
 
Example #29
Source File: AspectJPrecedenceComparatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testLowerAdvisorPrecedenceNoAfterAdvice() {
	Advisor advisor1 = createAspectJBeforeAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someAspect");
	Advisor advisor2 = createAspectJBeforeAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect");
	assertEquals("advisor1 sorted after advisor2", 1, this.comparator.compare(advisor1, advisor2));

	advisor1 = createAspectJBeforeAdvice(LOW_PRECEDENCE_ADVISOR_ORDER, LATE_ADVICE_DECLARATION_ORDER, "someAspect");
	advisor2 = createAspectJAroundAdvice(HIGH_PRECEDENCE_ADVISOR_ORDER, EARLY_ADVICE_DECLARATION_ORDER, "someOtherAspect");
	assertEquals("advisor1 sorted after advisor2", 1, this.comparator.compare(advisor1, advisor2));
}
 
Example #30
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testMultiAdvice() throws Throwable {
	CountingMultiAdvice cca = new CountingMultiAdvice();
	@SuppressWarnings("serial")
	Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {
		@Override
		public boolean matches(Method m, @Nullable Class<?> targetClass) {
			return m.getParameterCount() == 0 || "exceptional".equals(m.getName());
		}
	};
	TestBean target = new TestBean();
	target.setAge(80);
	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(new NopInterceptor());
	pf.addAdvisor(matchesNoArgs);
	assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
	ITestBean proxied = (ITestBean) createProxy(pf);

	assertEquals(0, cca.getCalls());
	assertEquals(0, cca.getCalls("getAge"));
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(2, cca.getCalls());
	assertEquals(2, cca.getCalls("getAge"));
	assertEquals(0, cca.getCalls("setAge"));
	// Won't be advised
	proxied.setAge(26);
	assertEquals(2, cca.getCalls());
	assertEquals(26, proxied.getAge());
	assertEquals(4, cca.getCalls());
	try {
		proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException)null));
		fail("Should have thrown CannotGetJdbcConnectionException");
	}
	catch (SpecializedUncheckedException ex) {
		// expected
	}
	assertEquals(6, cca.getCalls());
}