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.

Callback.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package javassist.tools;
  2. import javassist.CannotCompileException;
  3. import javassist.CtBehavior;
  4. import java.util.HashMap;
  5. import java.util.UUID;
  6. /**
  7. * Creates bytecode that when executed calls back to the instance's result method.
  8. *
  9. * Inserts callbacks in <code>CtBehaviour</code>
  10. *
  11. */
  12. public abstract class Callback {
  13. public static HashMap<String, Callback> callbacks = new HashMap<String, Callback>();
  14. private final String sourceCode;
  15. /**
  16. * Constructs a new <code>Callback</code> object.
  17. *
  18. * @param src The source code representing the inserted callback bytecode.
  19. * Can be one or many single statements or blocks each returning one object.
  20. * If many single statements or blocks are used they must be comma separated.
  21. */
  22. public Callback(String src){
  23. String uuid = UUID.randomUUID().toString();
  24. callbacks.put(uuid, this);
  25. sourceCode = "((javassist.tools.Callback) javassist.tools.Callback.callbacks.get(\""+uuid+"\")).result(new Object[]{"+src+"});";
  26. }
  27. /**
  28. * Gets called when bytecode is executed
  29. *
  30. * @param objects Objects that the bytecode in callback returns
  31. */
  32. public abstract void result(Object... objects);
  33. @Override
  34. public String toString(){
  35. return sourceCode();
  36. }
  37. public String sourceCode(){
  38. return sourceCode;
  39. }
  40. /**
  41. * Inserts callback at the beginning of the body.
  42. *
  43. * @param callback The callback
  44. *
  45. * @see CtBehavior#insertBefore(String)
  46. */
  47. public static void insertBefore(CtBehavior behavior, Callback callback)
  48. throws CannotCompileException
  49. {
  50. behavior.insertBefore(callback.toString());
  51. }
  52. /**
  53. * Inserts callback at the end of the body.
  54. * The callback is inserted just before every return instruction.
  55. * It is not executed when an exception is thrown.
  56. *
  57. * @param behavior The behaviour to insert callback in
  58. * @param callback The callback
  59. *
  60. * @see CtBehavior#insertAfter(String, boolean)
  61. */
  62. public static void insertAfter(CtBehavior behavior,Callback callback)
  63. throws CannotCompileException
  64. {
  65. behavior.insertAfter(callback.toString(), false);
  66. }
  67. /**
  68. * Inserts callback at the end of the body.
  69. * The callback is inserted just before every return instruction.
  70. * It is not executed when an exception is thrown.
  71. *
  72. * @param behavior The behaviour to insert callback in
  73. * @param callback The callback representing the inserted.
  74. * @param asFinally True if the inserted is executed
  75. * Not only when the control normally returns
  76. * but also when an exception is thrown.
  77. * If this parameter is true, the inserted code cannot
  78. * access local variables.
  79. *
  80. * @see CtBehavior#insertAfter(String, boolean)
  81. */
  82. public static void insertAfter(CtBehavior behavior, Callback callback, boolean asFinally)
  83. throws CannotCompileException
  84. {
  85. behavior.insertAfter(callback.toString(), asFinally);
  86. }
  87. /**
  88. * Inserts callback at the specified line in the body.
  89. *
  90. * @param behavior The behaviour to insert callback in
  91. * @param callback The callback representing.
  92. * @param lineNum The line number. The callback is inserted at the
  93. * beginning of the code at the line specified by this
  94. * line number.
  95. *
  96. * @return The line number at which the callback has been inserted.
  97. *
  98. * @see CtBehavior#insertAt(int, String)
  99. */
  100. public static int insertAt(CtBehavior behavior, Callback callback, int lineNum)
  101. throws CannotCompileException
  102. {
  103. return behavior.insertAt(lineNum, callback.toString());
  104. }
  105. }