You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Main.java 1.7KB

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