aboutsummaryrefslogtreecommitdiffstats
path: root/sample/duplicate/DuplicatedObject.java
blob: 5995abccb24253a37eec54062bc73d3a3208060a (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
package sample.duplicate;

import javassist.reflect.*;

public class DuplicatedObject extends Metaobject {
    private DuplicatedObject backup;

    // if a base-level object is created, this metaobject creates
    // a copy of the base-level object.

    public DuplicatedObject(Object self, Object[] args) {
	super(self, args);
	ClassMetaobject clazz = getClassMetaobject();
	if (clazz.isInstance(args[0]))
	    backup = null;	// self is a backup object.
	else {
	    Object[] args2 = new Object[1];
	    args2[0] = self;
	    try {
		Metalevel m = (Metalevel)clazz.newInstance(args2);
		backup = (DuplicatedObject)m._getMetaobject();
	    }
	    catch (CannotCreateException e) {
		backup = null;
	    }
	}
    }

    public Object trapMethodcall(int identifier, Object[] args) 
	throws Throwable
    {
	Object obj = super.trapMethodcall(identifier, args);
	if (backup != null)
	    backup.trapMethodcall(identifier, args);

	return obj;
    }
}