Package javassist.tools

# Class Callback

java.lang.Object
javassist.tools.Callback

public abstract class Callback extends Object
Creates bytecode that when executed calls back to the instance's result method.

Example of how to create and insert a callback:

     ctMethod.insertAfter(new Callback("Thread.currentThread()") {
         public void result(Object[] objects) {
             Thread thread = (Thread) objects[0];
             // do something with thread...
         }
     }.sourceCode());

Contains utility methods for inserts callbacks in `CtBehaviour`, example:

     insertAfter(ctBehaviour, new Callback("Thread.currentThread(), dummyString") {
         public void result(Object[] objects) {
             Thread thread = (Thread) objects[0];
             // do something with thread...
         }
     });

Author:
  Marten Hedborg, Shigeru Chiba

## Field Summary

Fields
Modifier and Type
Field
Description
`static Map<String,Callback>`
`callbacks`

## Constructor Summary

Constructors
Constructor
Description
`Callback(String src)`
Constructs a new `Callback` object.

## Method Summary

Modifier and Type
Method
Description
`static void`
`insertAfter(CtBehavior behavior, Callback callback)`
Utility method to inserts callback at the end of the body.
`static void`
`insertAfter(CtBehavior behavior, Callback callback, boolean asFinally)`
Utility method to inserts callback at the end of the body.
`static int`
`insertAt(CtBehavior behavior, Callback callback, int lineNum)`
Utility method to inserts callback at the specified line in the body.
`static void`
`insertBefore(CtBehavior behavior, Callback callback)`
Utility method to insert callback at the beginning of the body.
`abstract void`
`result(Object[] objects)`
Gets called when bytecode is executed
`String`
`sourceCode()`
`String`
`toString()`

### Methods inherited from class java.lang.Object

`equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Field Details

### callbacks

public static Map<String,Callback> callbacks

## Constructor Details

### Callback

public Callback(String src)
Constructs a new `Callback` object.

Parameters:
  `src` - The source code representing the inserted callback bytecode. Can be one or many single statements each returning one object. If many single statements are used they must be comma separated.

## Method Details

### result

public abstract void result(Object[] objects)
Gets called when bytecode is executed

Parameters:
  `objects` - Objects that the bytecode in callback returns

### toString

public String toString()

Overrides:
  `toString` in class `Object`

### sourceCode

public String sourceCode()

### insertBefore

public static void insertBefore(CtBehavior behavior, Callback callback) throws CannotCompileException
Utility method to insert callback at the beginning of the body.

Parameters:
  `callback` - The callback
Throws:
  `CannotCompileException`
See Also:
  - `CtBehavior.insertBefore(String)`

### insertAfter

public static void insertAfter(CtBehavior behavior, Callback callback) throws CannotCompileException
Utility method to inserts callback at the end of the body. The callback is inserted just before every return instruction. It is not executed when an exception is thrown.

Parameters:
  `behavior` - The behaviour to insert callback in
  `callback` - The callback
Throws:
  `CannotCompileException`
See Also:
  - `CtBehavior.insertAfter(String, boolean)`

### insertAfter

public static void insertAfter(CtBehavior behavior, Callback callback, boolean asFinally) throws CannotCompileException
Utility method to inserts callback at the end of the body. The callback is inserted just before every return instruction. It is not executed when an exception is thrown.

Parameters:
  `behavior` - The behaviour to insert callback in
  `callback` - The callback representing the inserted.
  `asFinally` - True if the inserted is executed Not only when the control normally returns but also when an exception is thrown. If this parameter is true, the inserted code cannot access local variables.
Throws:
  `CannotCompileException`
See Also:
  - `CtBehavior.insertAfter(String, boolean)`

### insertAt

public static int insertAt(CtBehavior behavior, Callback callback, int lineNum) throws CannotCompileException
Utility method to inserts callback at the specified line in the body.

Parameters:
  `behavior` - The behaviour to insert callback in
  `callback` - The callback representing.
  `lineNum` - The line number. The callback is inserted at the beginning of the code at the line specified by this line number.
Returns:
  The line number at which the callback has been inserted.
Throws:
  `CannotCompileException`
See Also:
  - `CtBehavior.insertAt(int, String)`