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.

CloneMethod.java 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. aspect AspectA {
  2. protected interface I {
  3. }
  4. declare parents : MyString implements I;
  5. protected Object createCloneFor(I object) {
  6. if (object instanceof MyString) {
  7. return new MyString(((MyString) object).toString());
  8. } else {
  9. return null;
  10. }
  11. }
  12. public Object I.clone() throws CloneNotSupportedException {
  13. return super.clone();
  14. // return null;
  15. }
  16. public Object cloneObject(I object) {
  17. try {
  18. return object.clone();
  19. } catch (CloneNotSupportedException ex) {
  20. return createCloneFor(object);
  21. }
  22. }
  23. }
  24. class MyString implements Cloneable {
  25. protected String text;
  26. public MyString(String init) {
  27. text = init;
  28. }
  29. public void setText(String newText) {
  30. text = newText;
  31. }
  32. public String toString() {
  33. return "MyString: " + text;
  34. }
  35. }
  36. public class CloneMethod {
  37. public static void main(String[] args) {
  38. MyString orig1;
  39. MyString copy1;
  40. orig1 = new MyString(" This is I 1");
  41. copy1 = (MyString) AspectA.aspectOf().cloneObject(orig1);
  42. orig1.setText(" This is I 2");
  43. copy1.setText(" This is Clone 1");
  44. System.out.println("... done.");
  45. }
  46. }