AOP proxies with annotation-based aspects are serializable now

Issue: SPR-6910
This commit is contained in:
Juergen Hoeller
2015-12-22 21:20:33 +01:00
parent bb0bc3d415
commit 4adb7e2500
16 changed files with 214 additions and 108 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,9 +31,10 @@ import static org.junit.Assert.*;
* @author Rod Johnson
* @author Chris Beams
*/
public final class AspectJPointcutAdvisorTests {
public class AspectJPointcutAdvisorTests {
private final AspectJAdvisorFactory af = new ReflectiveAspectJAdvisorFactory();
private AspectJAdvisorFactory af = new ReflectiveAspectJAdvisorFactory();
@Test
public void testSingleton() throws SecurityException, NoSuchMethodException {
@@ -41,8 +42,8 @@ public final class AspectJPointcutAdvisorTests {
ajexp.setExpression(AspectJExpressionPointcutTests.MATCH_ALL_METHODS);
InstantiationModelAwarePointcutAdvisorImpl ajpa = new InstantiationModelAwarePointcutAdvisorImpl(af, ajexp,
new SingletonMetadataAwareAspectInstanceFactory(new AbstractAspectJAdvisorFactoryTests.ExceptionAspect(null),"someBean"),
TestBean.class.getMethod("getAge", (Class[]) null),1,"someBean");
new SingletonMetadataAwareAspectInstanceFactory(new AbstractAspectJAdvisorFactoryTests.ExceptionAspect(null), "someBean"),
TestBean.class.getMethod("getAge", (Class[]) null), 1, "someBean");
assertSame(Pointcut.TRUE, ajpa.getAspectMetadata().getPerClausePointcut());
assertFalse(ajpa.isPerInstance());
}
@@ -53,33 +54,32 @@ public final class AspectJPointcutAdvisorTests {
ajexp.setExpression(AspectJExpressionPointcutTests.MATCH_ALL_METHODS);
InstantiationModelAwarePointcutAdvisorImpl ajpa = new InstantiationModelAwarePointcutAdvisorImpl(af, ajexp,
new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(),"someBean"), null, 1, "someBean");
new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(),"someBean"),
TestBean.class.getMethod("getAge", (Class[]) null), 1, "someBean");
assertNotSame(Pointcut.TRUE, ajpa.getAspectMetadata().getPerClausePointcut());
assertTrue(ajpa.getAspectMetadata().getPerClausePointcut() instanceof AspectJExpressionPointcut);
assertTrue(ajpa.isPerInstance());
assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getClassFilter().matches(TestBean.class));
assertFalse(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(
TestBean.class.getMethod("getAge", (Class[]) null),
TestBean.class));
TestBean.class.getMethod("getAge", (Class[]) null), TestBean.class));
assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(
TestBean.class.getMethod("getSpouse", (Class[]) null),
TestBean.class));
TestBean.class.getMethod("getSpouse", (Class[]) null), TestBean.class));
}
@Test(expected=AopConfigException.class)
@Test(expected = AopConfigException.class)
public void testPerCflowTarget() {
testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowAspect.class);
}
@Test(expected=AopConfigException.class)
@Test(expected = AopConfigException.class)
public void testPerCflowBelowTarget() {
testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowBelowAspect.class);
}
private void testIllegalInstantiationModel(Class<?> c) throws AopConfigException {
new AspectMetadata(c,"someBean");
new AspectMetadata(c, "someBean");
}
}

View File

@@ -16,13 +16,13 @@
package org.springframework.aop.aspectj.annotation;
import java.io.Serializable;
import java.util.Arrays;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.junit.Ignore;
import org.junit.Test;
import test.aop.PerThisAspect;
@@ -80,7 +80,18 @@ public class AspectProxyFactoryTests {
}
@Test
@Ignore // InstantiationModelAwarePointcutAdvisorImpl not serializable yet
@SuppressWarnings("unchecked")
public void testSerializable() throws Exception {
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean());
proxyFactory.addAspect(LoggingAspectOnVarargs.class);
ITestBean proxy = proxyFactory.getProxy();
assertTrue(proxy.doWithVarargs(MyEnum.A, MyOtherEnum.C));
ITestBean tb = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy);
assertTrue(tb.doWithVarargs(MyEnum.A, MyOtherEnum.C));
}
@Test
@SuppressWarnings("unchecked")
public void testWithInstance() throws Exception {
MultiplyReturnValue aspect = new MultiplyReturnValue();
int multiple = 3;
@@ -133,7 +144,8 @@ public class AspectProxyFactoryTests {
}
public static class TestBean implements ITestBean {
@SuppressWarnings("serial")
public static class TestBean implements ITestBean, Serializable {
private int age;
@@ -171,7 +183,8 @@ public class AspectProxyFactoryTests {
@Aspect
public static class LoggingAspectOnVarargs {
@SuppressWarnings("serial")
public static class LoggingAspectOnVarargs implements Serializable {
@Around("execution(* doWithVarargs(*))")
public Object doLog(ProceedingJoinPoint pjp) throws Throwable {
@@ -193,11 +206,9 @@ public class AspectProxyFactoryTests {
}
/**
* @author Rod Johnson
*/
@Aspect
class MultiplyReturnValue {
@SuppressWarnings("serial")
class MultiplyReturnValue implements Serializable {
private int multiple = 2;