org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor Java Examples

The following examples show how to use org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor. 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 5 votes vote down vote up
/**
 * There are times when we want to call proceed() twice.
 * We can do this if we clone the invocation.
 */
@Test
public void testCloneInvocationToProceedThreeTimes() throws Throwable {
	TestBean tb = new TestBean();
	ProxyFactory pc = new ProxyFactory(tb);
	pc.addInterface(ITestBean.class);

	MethodInterceptor twoBirthdayInterceptor = new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation mi) throws Throwable {
			// Clone the invocation to proceed three times
			// "The Moor's Last Sigh": this technology can cause premature aging
			MethodInvocation clone1 = ((ReflectiveMethodInvocation) mi).invocableClone();
			MethodInvocation clone2 = ((ReflectiveMethodInvocation) mi).invocableClone();
			clone1.proceed();
			clone2.proceed();
			return mi.proceed();
		}
	};
	@SuppressWarnings("serial")
	StaticMethodMatcherPointcutAdvisor advisor = new StaticMethodMatcherPointcutAdvisor(twoBirthdayInterceptor) {
		@Override
		public boolean matches(Method m, @Nullable Class<?> targetClass) {
			return "haveBirthday".equals(m.getName());
		}
	};
	pc.addAdvisor(advisor);
	ITestBean it = (ITestBean) createProxy(pc);

	final int age = 20;
	it.setAge(age);
	assertEquals(age, it.getAge());
	// Should return the age before the third, AOP-induced birthday
	assertEquals(age + 2, it.haveBirthday());
	// Return the final age produced by 3 birthdays
	assertEquals(age + 3, it.getAge());
}
 
Example #2
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testBeforeAdvisorIsInvoked() {
	CountingBeforeAdvice cba = new CountingBeforeAdvice();
	@SuppressWarnings("serial")
	Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {
		@Override
		public boolean matches(Method m, @Nullable Class<?> targetClass) {
			return m.getParameterCount() == 0;
		}
	};
	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, cba.getCalls());
	assertEquals(0, cba.getCalls("getAge"));
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(1, cba.getCalls());
	assertEquals(1, cba.getCalls("getAge"));
	assertEquals(0, cba.getCalls("setAge"));
	// Won't be advised
	proxied.setAge(26);
	assertEquals(1, cba.getCalls());
	assertEquals(26, proxied.getAge());
}
 
Example #3
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());
}
 
Example #4
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * There are times when we want to call proceed() twice.
 * We can do this if we clone the invocation.
 */
@Test
public void testCloneInvocationToProceedThreeTimes() throws Throwable {
	TestBean tb = new TestBean();
	ProxyFactory pc = new ProxyFactory(tb);
	pc.addInterface(ITestBean.class);

	MethodInterceptor twoBirthdayInterceptor = new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation mi) throws Throwable {
			// Clone the invocation to proceed three times
			// "The Moor's Last Sigh": this technology can cause premature aging
			MethodInvocation clone1 = ((ReflectiveMethodInvocation) mi).invocableClone();
			MethodInvocation clone2 = ((ReflectiveMethodInvocation) mi).invocableClone();
			clone1.proceed();
			clone2.proceed();
			return mi.proceed();
		}
	};
	@SuppressWarnings("serial")
	StaticMethodMatcherPointcutAdvisor advisor = new StaticMethodMatcherPointcutAdvisor(twoBirthdayInterceptor) {
		@Override
		public boolean matches(Method m, @Nullable Class<?> targetClass) {
			return "haveBirthday".equals(m.getName());
		}
	};
	pc.addAdvisor(advisor);
	ITestBean it = (ITestBean) createProxy(pc);

	final int age = 20;
	it.setAge(age);
	assertEquals(age, it.getAge());
	// Should return the age before the third, AOP-induced birthday
	assertEquals(age + 2, it.haveBirthday());
	// Return the final age produced by 3 birthdays
	assertEquals(age + 3, it.getAge());
}
 
Example #5
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testBeforeAdvisorIsInvoked() {
	CountingBeforeAdvice cba = new CountingBeforeAdvice();
	@SuppressWarnings("serial")
	Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {
		@Override
		public boolean matches(Method m, @Nullable Class<?> targetClass) {
			return m.getParameterCount() == 0;
		}
	};
	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, cba.getCalls());
	assertEquals(0, cba.getCalls("getAge"));
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(1, cba.getCalls());
	assertEquals(1, cba.getCalls("getAge"));
	assertEquals(0, cba.getCalls("setAge"));
	// Won't be advised
	proxied.setAge(26);
	assertEquals(1, cba.getCalls());
	assertEquals(26, proxied.getAge());
}
 
Example #6
Source File: AbstractAopProxyTests.java    From java-technology-stack 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());
}
 
Example #7
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * There are times when we want to call proceed() twice.
 * We can do this if we clone the invocation.
 */
@Test
public void testCloneInvocationToProceedThreeTimes() throws Throwable {
	TestBean tb = new TestBean();
	ProxyFactory pc = new ProxyFactory(tb);
	pc.addInterface(ITestBean.class);

	MethodInterceptor twoBirthdayInterceptor = new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation mi) throws Throwable {
			// Clone the invocation to proceed three times
			// "The Moor's Last Sigh": this technology can cause premature aging
			MethodInvocation clone1 = ((ReflectiveMethodInvocation) mi).invocableClone();
			MethodInvocation clone2 = ((ReflectiveMethodInvocation) mi).invocableClone();
			clone1.proceed();
			clone2.proceed();
			return mi.proceed();
		}
	};
	@SuppressWarnings("serial")
	StaticMethodMatcherPointcutAdvisor advisor = new StaticMethodMatcherPointcutAdvisor(twoBirthdayInterceptor) {
		@Override
		public boolean matches(Method m, Class<?> targetClass) {
			return "haveBirthday".equals(m.getName());
		}
	};
	pc.addAdvisor(advisor);
	ITestBean it = (ITestBean) createProxy(pc);

	final int age = 20;
	it.setAge(age);
	assertEquals(age, it.getAge());
	// Should return the age before the third, AOP-induced birthday
	assertEquals(age + 2, it.haveBirthday());
	// Return the final age produced by 3 birthdays
	assertEquals(age + 3, it.getAge());
}
 
Example #8
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBeforeAdvisorIsInvoked() {
	CountingBeforeAdvice cba = new CountingBeforeAdvice();
	@SuppressWarnings("serial")
	Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {
		@Override
		public boolean matches(Method m, Class<?> targetClass) {
			return m.getParameterTypes().length == 0;
		}
	};
	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, cba.getCalls());
	assertEquals(0, cba.getCalls("getAge"));
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(1, cba.getCalls());
	assertEquals(1, cba.getCalls("getAge"));
	assertEquals(0, cba.getCalls("setAge"));
	// Won't be advised
	proxied.setAge(26);
	assertEquals(1, cba.getCalls());
	assertEquals(26, proxied.getAge());
}
 
Example #9
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 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, Class<?> targetClass) {
			return m.getParameterTypes().length == 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());
}