Java Code Examples for org.springframework.aop.framework.ProxyFactory#setExposeProxy()

The following examples show how to use org.springframework.aop.framework.ProxyFactory#setExposeProxy() . 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: 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 2
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 3
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring4-understanding with Apache License 2.0 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);

	for (Object a : advisors) {
		pf.addAdvisor((Advisor) a);
	}

	pf.setExposeProxy(true);
	return pf.getProxy();
}
 
Example 4
Source File: DeclareMixinAspectJAdvisorFactoryTest.java    From jdal with Apache License 2.0 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);

	for (Object a : advisors) {
		pf.addAdvisor((Advisor) a);
	}

	pf.setExposeProxy(true);
	return pf.getProxy();
}
 
Example 5
Source File: SerializableProxyUtils.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new Serializable proxy for the given target.
 * @param target target to proxy
 * @param reference serializable reference 
 * @return a new serializable proxy
 */
public static Object createSerializableProxy(Object target, SerializableReference reference) {
	ProxyFactory pf = new ProxyFactory(target);		
	pf.setExposeProxy(true);		
	pf.setProxyTargetClass(reference.isProxyTargetClass());
	pf.addInterface(SerializableObject.class);
	pf.addAdvice(new SerializableIntroductionInterceptor(reference));
	
	return pf.getProxy(reference.getBeanFactory().getBeanClassLoader());
}
 
Example 6
Source File: MethodInvocationProceedingJoinPointTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testCanGetMethodSignatureFromJoinPoint() {
	final Object raw = new TestBean();
	// Will be set by advice during a method call
	final int newAge = 23;

	ProxyFactory pf = new ProxyFactory(raw);
	pf.setExposeProxy(true);
	pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
	pf.addAdvice(new MethodBeforeAdvice() {
		private int depth;

		@Override
		public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
			JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
			assertTrue("Method named in toString", jp.toString().contains(method.getName()));
			// Ensure that these don't cause problems
			jp.toShortString();
			jp.toLongString();

			assertSame(target, AbstractAspectJAdvice.currentJoinPoint().getTarget());
			assertFalse(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget()));

			ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
			assertTrue(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis()));

			assertNotSame(target, thisProxy);

			// Check getting again doesn't cause a problem
			assertSame(thisProxy, AbstractAspectJAdvice.currentJoinPoint().getThis());

			// Try reentrant call--will go through this advice.
			// Be sure to increment depth to avoid infinite recursion
			if (depth++ == 0) {
				// Check that toString doesn't cause a problem
				thisProxy.toString();
				// Change age, so this will be returned by invocation
				thisProxy.setAge(newAge);
				assertEquals(newAge, thisProxy.getAge());
			}

			assertSame(AopContext.currentProxy(), thisProxy);
			assertSame(target, raw);

			assertSame(method.getName(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getName());
			assertEquals(method.getModifiers(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers());

			MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
			assertSame("Return same MethodSignature repeatedly", msig, AbstractAspectJAdvice.currentJoinPoint().getSignature());
			assertSame("Return same JoinPoint repeatedly", AbstractAspectJAdvice.currentJoinPoint(), AbstractAspectJAdvice.currentJoinPoint());
			assertEquals(method.getDeclaringClass(), msig.getDeclaringType());
			assertTrue(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes()));
			assertEquals(method.getReturnType(), msig.getReturnType());
			assertTrue(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes()));
			msig.toLongString();
			msig.toShortString();
		}
	});
	ITestBean itb = (ITestBean) pf.getProxy();
	// Any call will do
	assertEquals("Advice reentrantly set age", newAge, itb.getAge());
}
 
Example 7
Source File: MethodInvocationProceedingJoinPointTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testCanGetMethodSignatureFromJoinPoint() {
	final Object raw = new TestBean();
	// Will be set by advice during a method call
	final int newAge = 23;

	ProxyFactory pf = new ProxyFactory(raw);
	pf.setExposeProxy(true);
	pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
	pf.addAdvice(new MethodBeforeAdvice() {
		private int depth;

		@Override
		public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
			JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
			assertTrue("Method named in toString", jp.toString().contains(method.getName()));
			// Ensure that these don't cause problems
			jp.toShortString();
			jp.toLongString();

			assertSame(target, AbstractAspectJAdvice.currentJoinPoint().getTarget());
			assertFalse(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget()));

			ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
			assertTrue(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis()));

			assertNotSame(target, thisProxy);

			// Check getting again doesn't cause a problem
			assertSame(thisProxy, AbstractAspectJAdvice.currentJoinPoint().getThis());

			// Try reentrant call--will go through this advice.
			// Be sure to increment depth to avoid infinite recursion
			if (depth++ == 0) {
				// Check that toString doesn't cause a problem
				thisProxy.toString();
				// Change age, so this will be returned by invocation
				thisProxy.setAge(newAge);
				assertEquals(newAge, thisProxy.getAge());
			}

			assertSame(AopContext.currentProxy(), thisProxy);
			assertSame(target, raw);

			assertSame(method.getName(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getName());
			assertEquals(method.getModifiers(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers());

			MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
			assertSame("Return same MethodSignature repeatedly", msig, AbstractAspectJAdvice.currentJoinPoint().getSignature());
			assertSame("Return same JoinPoint repeatedly", AbstractAspectJAdvice.currentJoinPoint(), AbstractAspectJAdvice.currentJoinPoint());
			assertEquals(method.getDeclaringClass(), msig.getDeclaringType());
			assertTrue(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes()));
			assertEquals(method.getReturnType(), msig.getReturnType());
			assertTrue(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes()));
			msig.toLongString();
			msig.toShortString();
		}
	});
	ITestBean itb = (ITestBean) pf.getProxy();
	// Any call will do
	assertEquals("Advice reentrantly set age", newAge, itb.getAge());
}
 
Example 8
Source File: MethodInvocationProceedingJoinPointTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testCanGetMethodSignatureFromJoinPoint() {
	final Object raw = new TestBean();
	// Will be set by advice during a method call
	final int newAge = 23;

	ProxyFactory pf = new ProxyFactory(raw);
	pf.setExposeProxy(true);
	pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
	pf.addAdvice(new MethodBeforeAdvice() {
		private int depth;

		@Override
		public void before(Method method, Object[] args, Object target) throws Throwable {
			JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
			assertTrue("Method named in toString", jp.toString().contains(method.getName()));
			// Ensure that these don't cause problems
			jp.toShortString();
			jp.toLongString();

			assertSame(target, AbstractAspectJAdvice.currentJoinPoint().getTarget());
			assertFalse(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget()));

			ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
			assertTrue(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis()));

			assertNotSame(target, thisProxy);

			// Check getting again doesn't cause a problem
			assertSame(thisProxy, AbstractAspectJAdvice.currentJoinPoint().getThis());

			// Try reentrant call--will go through this advice.
			// Be sure to increment depth to avoid infinite recursion
			if (depth++ == 0) {
				// Check that toString doesn't cause a problem
				thisProxy.toString();
				// Change age, so this will be returned by invocation
				thisProxy.setAge(newAge);
				assertEquals(newAge, thisProxy.getAge());
			}

			assertSame(AopContext.currentProxy(), thisProxy);
			assertSame(target, raw);

			assertSame(method.getName(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getName());
			assertEquals(method.getModifiers(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers());

			MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
			assertSame("Return same MethodSignature repeatedly", msig, AbstractAspectJAdvice.currentJoinPoint().getSignature());
			assertSame("Return same JoinPoint repeatedly", AbstractAspectJAdvice.currentJoinPoint(), AbstractAspectJAdvice.currentJoinPoint());
			assertEquals(method.getDeclaringClass(), msig.getDeclaringType());
			assertTrue(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes()));
			assertEquals(method.getReturnType(), msig.getReturnType());
			assertTrue(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes()));
			msig.toLongString();
			msig.toShortString();
		}
	});
	ITestBean itb = (ITestBean) pf.getProxy();
	// Any call will do
	assertEquals("Advice reentrantly set age", newAge, itb.getAge());
}