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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.tools;
  17. import javassist.CannotCompileException;
  18. import javassist.CtBehavior;
  19. import java.util.HashMap;
  20. import java.util.UUID;
  21. /**
  22. * Creates bytecode that when executed calls back to the instance's result method.
  23. *
  24. * Example of how to create and insert a callback:
  25. * <pre>{@code
  26. * ctMethod.insertAfter(new Callback("Thread.currentThread()") {
  27. * @literal@Override
  28. * public void result(Object... objects) {
  29. * Thread thread = (Thread) objects[0];
  30. * // do something with thread...
  31. * }
  32. * }.sourceCode());
  33. * }</pre>
  34. * Contains utility methods for inserts callbacks in <code>CtBehaviour</code>, example:
  35. * <pre>{@code
  36. * insertAfter(ctBehaviour, new Callback("Thread.currentThread(), dummyString") {
  37. * @literal@Override
  38. * public void result(Object... objects) {
  39. * Thread thread = (Thread) objects[0];
  40. * // do something with thread...
  41. * }
  42. * });
  43. * }</pre>
  44. *
  45. * @author Marten Hedborg
  46. */
  47. public abstract class Callback {
  48. public static HashMap<String, Callback> callbacks = new HashMap<String, Callback>();
  49. private final String sourceCode;
  50. /**
  51. * Constructs a new <code>Callback</code> object.
  52. *
  53. * @param src The source code representing the inserted callback bytecode.
  54. * Can be one or many single statements each returning one object.
  55. * If many single statements are used they must be comma separated.
  56. */
  57. public Callback(String src){
  58. String uuid = UUID.randomUUID().toString();
  59. callbacks.put(uuid, this);
  60. sourceCode = "((javassist.tools.Callback) javassist.tools.Callback.callbacks.get(\""+uuid+"\")).result(new Object[]{"+src+"});";
  61. }
  62. /**
  63. * Gets called when bytecode is executed
  64. *
  65. * @param objects Objects that the bytecode in callback returns
  66. */
  67. public abstract void result(Object... objects);
  68. @Override
  69. public String toString(){
  70. return sourceCode();
  71. }
  72. public String sourceCode(){
  73. return sourceCode;
  74. }
  75. /**
  76. * Utility method to insert callback at the beginning of the body.
  77. *
  78. * @param callback The callback
  79. *
  80. * @see CtBehavior#insertBefore(String)
  81. */
  82. public static void insertBefore(CtBehavior behavior, Callback callback)
  83. throws CannotCompileException
  84. {
  85. behavior.insertBefore(callback.toString());
  86. }
  87. /**
  88. * Utility method to inserts callback at the end of the body.
  89. * The callback is inserted just before every return instruction.
  90. * It is not executed when an exception is thrown.
  91. *
  92. * @param behavior The behaviour to insert callback in
  93. * @param callback The callback
  94. *
  95. * @see CtBehavior#insertAfter(String, boolean)
  96. */
  97. public static void insertAfter(CtBehavior behavior,Callback callback)
  98. throws CannotCompileException
  99. {
  100. behavior.insertAfter(callback.toString(), false);
  101. }
  102. /**
  103. * Utility method to inserts callback at the end of the body.
  104. * The callback is inserted just before every return instruction.
  105. * It is not executed when an exception is thrown.
  106. *
  107. * @param behavior The behaviour to insert callback in
  108. * @param callback The callback representing the inserted.
  109. * @param asFinally True if the inserted is executed
  110. * Not only when the control normally returns
  111. * but also when an exception is thrown.
  112. * If this parameter is true, the inserted code cannot
  113. * access local variables.
  114. *
  115. * @see CtBehavior#insertAfter(String, boolean)
  116. */
  117. public static void insertAfter(CtBehavior behavior, Callback callback, boolean asFinally)
  118. throws CannotCompileException
  119. {
  120. behavior.insertAfter(callback.toString(), asFinally);
  121. }
  122. /**
  123. * Utility method to inserts callback at the specified line in the body.
  124. *
  125. * @param behavior The behaviour to insert callback in
  126. * @param callback The callback representing.
  127. * @param lineNum The line number. The callback is inserted at the
  128. * beginning of the code at the line specified by this
  129. * line number.
  130. *
  131. * @return The line number at which the callback has been inserted.
  132. *
  133. * @see CtBehavior#insertAt(int, String)
  134. */
  135. public static int insertAt(CtBehavior behavior, Callback callback, int lineNum)
  136. throws CannotCompileException
  137. {
  138. return behavior.insertAt(lineNum, callback.toString());
  139. }
  140. }