01: package sample.duplicate;
02:
03: import javassist.tools.reflect.*;
04:
05: public class DuplicatedObject extends Metaobject {
06: private DuplicatedObject backup;
07:
08: // if a base-level object is created, this metaobject creates
09: // a copy of the base-level object.
10:
11: public DuplicatedObject(Object self, Object[] args) {
12: super (self, args);
13: ClassMetaobject clazz = getClassMetaobject();
14: if (clazz.isInstance(args[0]))
15: backup = null; // self is a backup object.
16: else {
17: Object[] args2 = new Object[1];
18: args2[0] = self;
19: try {
20: Metalevel m = (Metalevel) clazz.newInstance(args2);
21: backup = (DuplicatedObject) m._getMetaobject();
22: } catch (CannotCreateException e) {
23: backup = null;
24: }
25: }
26: }
27:
28: public Object trapMethodcall(int identifier, Object[] args)
29: throws Throwable {
30: Object obj = super.trapMethodcall(identifier, args);
31: if (backup != null)
32: backup.trapMethodcall(identifier, args);
33:
34: return obj;
35: }
36: }
|