/Users/lyon/j4p/src/javassist/sample/duplicate/DuplicatedObject.java
|
1 package javassist.sample.duplicate;
2
3 import javassist.reflect.*;
4
5 public class DuplicatedObject extends Metaobject {
6 private DuplicatedObject backup;
7
8 // if a base-level object is created, this metaobject creates
9 // 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 }
37