169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage sample.duplicate;
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  Runtime metaobject (JDK 1.2 or later only).
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  With the javassist.tools.reflect package, the users can attach a metaobject
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  to an object.  The metaobject can control the behavior of the object.
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  For example, you can implement fault tolerancy with this ability.  One
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  of the implementation techniques of fault tolernacy is to make a copy
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  of every object containing important data and maintain it as a backup.
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  If the machine running the object becomes down, the backup object on a
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  different machine is used to continue the execution.
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  To make the copy of the object a real backup, all the method calls to
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  the object must be also sent to that copy.  The metaobject is needed
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  for this duplication of the method calls.  It traps every method call
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  and invoke the same method on the copy of the object so that the
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  internal state of the copy is kept equivalent to that of the original
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  object.
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  First, run sample.duplicate.Viewer without a metaobject.
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  % java sample.duplicate.Viewer
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  This program shows a ball in a window.
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  Then, run the same program with a metaobject, which is an instance
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  of sample.duplicate.DuplicatedObject.
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  % java sample.duplicate.Main
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  You would see two balls in a window.  This is because
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  sample.duplicate.Viewer is loaded by javassist.tools.reflect.Loader so that
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal  a metaobject would be attached.
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal*/
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class Main {
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static void main(String[] args) throws Throwable {
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	javassist.tools.reflect.Loader cl = new javassist.tools.reflect.Loader();
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	cl.makeReflective("sample.duplicate.Ball",
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			  "sample.duplicate.DuplicatedObject",
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			  "javassist.tools.reflect.ClassMetaobject");
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	cl.run("sample.duplicate.Viewer", args);
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
45