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