Home | History | Annotate | Download | only in duplicate
      1 package sample.duplicate;
      2 
      3 import javassist.tools.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 	    }
     23 	    catch (CannotCreateException e) {
     24 		backup = null;
     25 	    }
     26 	}
     27     }
     28 
     29     public Object trapMethodcall(int identifier, Object[] args)
     30 	throws Throwable
     31     {
     32 	Object obj = super.trapMethodcall(identifier, args);
     33 	if (backup != null)
     34 	    backup.trapMethodcall(identifier, args);
     35 
     36 	return obj;
     37     }
     38 }
     39