--- /dev/null
+\r
+\r
+import org.aspectj.lang.annotation.Aspect;\r
+import org.aspectj.lang.annotation.Before;\r
+import org.aspectj.lang.annotation.DeclareParents;\r
+import org.aspectj.lang.annotation.Pointcut;\r
+\r
+\r
+@Aspect\r
+public class Persistability {\r
+\r
+ static class Persistable implements IPersistable {\r
+\r
+ private static final long serialVersionUID = 7120491865883787353L;\r
+\r
+ private int id;\r
+\r
+ public Persistable() {\r
+ super();\r
+ }\r
+\r
+ public int getId() {\r
+ return id;\r
+ }\r
+\r
+ public void setId(int id) {\r
+ this.id = id;\r
+ }\r
+\r
+ }\r
+\r
+ @DeclareParents(value = "PersistabilityTest", defaultImpl = Persistable.class)\r
+ private IPersistable observable;\r
+\r
+ @Pointcut("initialization(IPersistable.new(..)) && this(bean) && !this(Persistable)")\r
+ void init(IPersistable bean) {\r
+ }\r
+\r
+ @Before("init(bean)")\r
+ public void beforeInit(IPersistable bean) {\r
+ bean.setId(System.identityHashCode(bean));\r
+ }\r
+\r
+}\r
--- /dev/null
+\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.ObjectInputStream;\r
+import java.io.ObjectOutputStream;\r
+\r
+public class PersistabilityTest {\r
+\r
+ public static void main(String []argv) throws Exception {\r
+\r
+ PersistabilityTest persistabilityTest1 = new PersistabilityTest();\r
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();\r
+ ObjectOutputStream oos = new ObjectOutputStream(bos);\r
+\r
+ oos.writeObject(persistabilityTest1);\r
+\r
+ ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());\r
+ ObjectInputStream ois = new ObjectInputStream(bis);\r
+\r
+ PersistabilityTest persistabilityTest2 = (PersistabilityTest) ois.readObject();\r
+\r
+ if (!(persistabilityTest1 instanceof IPersistable)) throw new RuntimeException("pTest1 not IPersistable");\r
+ if (!(persistabilityTest2 instanceof IPersistable)) throw new RuntimeException("pTest2 not IPersistable");\r
+ int o = ((IPersistable)persistabilityTest1).getId();\r
+ int o2 = ((IPersistable)persistabilityTest2).getId();\r
+ if (o!=o2) throw new RuntimeException(o+" != "+o2);\r
+ }\r
+}\r