1 package sample.duplicate; 2 3 /* 4 Runtime metaobject (JDK 1.2 or later only). 5 6 With the javassist.tools.reflect package, the users can attach a metaobject 7 to an object. The metaobject can control the behavior of the object. 8 For example, you can implement fault tolerancy with this ability. One 9 of the implementation techniques of fault tolernacy is to make a copy 10 of every object containing important data and maintain it as a backup. 11 If the machine running the object becomes down, the backup object on a 12 different machine is used to continue the execution. 13 14 To make the copy of the object a real backup, all the method calls to 15 the object must be also sent to that copy. The metaobject is needed 16 for this duplication of the method calls. It traps every method call 17 and invoke the same method on the copy of the object so that the 18 internal state of the copy is kept equivalent to that of the original 19 object. 20 21 First, run sample.duplicate.Viewer without a metaobject. 22 23 % java sample.duplicate.Viewer 24 25 This program shows a ball in a window. 26 27 Then, run the same program with a metaobject, which is an instance 28 of sample.duplicate.DuplicatedObject. 29 30 % java sample.duplicate.Main 31 32 You would see two balls in a window. This is because 33 sample.duplicate.Viewer is loaded by javassist.tools.reflect.Loader so that 34 a metaobject would be attached. 35 */ 36 public class Main { main(String[] args)37 public static void main(String[] args) throws Throwable { 38 javassist.tools.reflect.Loader cl = new javassist.tools.reflect.Loader(); 39 cl.makeReflective("sample.duplicate.Ball", 40 "sample.duplicate.DuplicatedObject", 41 "javassist.tools.reflect.ClassMetaobject"); 42 cl.run("sample.duplicate.Viewer", args); 43 } 44 } 45