You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

A.java 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.io.Serializable;
  6. public class A {
  7. public static void main(String[] args) throws Exception {
  8. A obj1 = new A();
  9. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  10. ObjectOutputStream oos = new ObjectOutputStream(baos);
  11. oos.writeObject(obj1);
  12. oos.close();
  13. byte[] data = baos.toByteArray();
  14. ByteArrayInputStream bais = new ByteArrayInputStream(data);
  15. ObjectInputStream ois = new ObjectInputStream(bais);
  16. Object o = ois.readObject();
  17. int before = ((Persistable)obj1).getPersistableId();
  18. int after = ((Persistable)o).getPersistableId();
  19. if (before!=after)
  20. System.out.println("The data was lost! before="+before+" after="+after);
  21. else
  22. System.out.println("It worked, data preserved!");
  23. }
  24. }
  25. interface Persistable extends Serializable {
  26. abstract public int getPersistableId();
  27. }
  28. aspect PersistableImpl {
  29. declare parents: A extends Persistable;
  30. final public int Persistable.persistableId = 42;
  31. public int Persistable.getPersistableId() { return persistableId; }
  32. }