Scripts In JProfiler


  JProfiler's built-in script editor allows you to enter custom logic in various places in the JProfiler GUI, including custom probe configuration, heap walker filters and inspections and the "Run interceptor script" trigger action.
  The box above the edit area show the available parameters of the script as well as its return type. If parameters or return type are classes (and not primitive types), they will be shown as hyperlinks. Clicking on such a hyperlink opens the Javadoc in the external browser.

To get more information on classes from the com.jprofiler.api.* packages, please choose Help->Show Javadoc Overview from the menu and read the the help topic on custom probes.

A number of packages can be used without using fully-qualified class names. Those packages are:

  • java.util.*
  • java.io.*
  • com.jprofiler.api.*
  • com.jprofiler.api.probes.*

You can put a number of import statements as the first lines in the text area in order to avoid using fully qualified class names.

All scripts are passed an instance of com.jprofiler.api.agent.ScriptContext that allows you to save state between successive invocations of the script.

  Scripts can be
  • expressions
    An expression doesn't have a trailing semicolon and evaluates to the required return type.

    Example: object.toString().contains("test")

    The above example would work as a filter script in the outgoing reference view of the heap walker.

  • scripts
    A script consists of a series of Java statements with a return statement of the required return type as the last statement.

    Example: data[0] = ((Integer)probeContext.getMap().remove("myCount")).intValue();

    The above example would work as the telemetry script in a custom probe configuration.

JProfiler detects automatically whether you have entered an expression or a script.
  The Java editor offers the following code assistance powered by the eclipse platform:
  • Code completion
    Pressing CTRL-Space brings up a popup with code completion proposals. Also, typing a dot (".") shows this popup after a delay if no other character is typed. While the popup is displayed, you can continue to type or delete characters with Backspace and the popup will be updated accordingly. "Camel-hump completion" is supported, i.e. typing NPE and hitting CTRL-Space will propose NullPointerException among other classes. If you accept a class that is not automatically imported, the fully qualified name will be inserted.

    The completion popup can suggest:

    • variables and default parameters. Default parameters are displayed in bold font.
    • packages (when typing an import statement)
    • classes
    • fields (when the context is a class)
    • methods (when the context is a class or the parameter list of a method)

    You can configure code completion behavior in the Editor Settings.

    Parameter classes that are neither contained in the configured session class path nor in the configured JDK are marked as [unresolved] and are changed to the generic java.lang.Object type. To get code completion for these parameters, add the missing JAR files to the session class path.

  • Problem analysis
    The code that you enter is analyzed on the fly and checked for errors and warning conditions. Errors are shown as red underlines in the editor and red stripes in the right gutter. Warnings (such as an unused variable declaration) are shown as a yellow backgrounds in the editor and yellow stripes in the right gutter. Hovering the mouse over an error or warning in the editor as well as hovering the mouse over a stripe in the gutter area displays the error or warning message.

    The status indicator at the top of the right gutter is

    • green
      if there are no warnings or errors in the code.
    • yellow
      if there are warnings but no errors in the code.
    • red
      if there are errors in the code. In this case the code will not compile and the session cannot be started.

    You can configure the threshold for problem analysis in the Editor Settings.

  • Context-sensitive Javadoc
    Pressing SHIFT-F1 opens the browser at the Javadoc page that describes the element at the cursor position. If no corresponding Javadoc can be found, a warning message is displayed. Javadoc for the Java runtime library can only be displayed if a JDK is configured for the session and a valid Javadoc location is specified in the JDK settings.
All key bindings in the Java code editor are configurable. Choose Settings->Key Map to display the Key map editor.
  If the gutter icon in the top right corner of the dialog is green, your script is going to compile unless you have disabled error analysis in the Editor Settings. In some situations, you might want to try the actual compilation. Choosing Code->Test Compile from the menu will compile the script and display any errors in a separate dialog. Saving your script with the [OK] button will not test the syntactic correctness of the script unless the script is used right away.
  Often you will want to reuse a script that you have entered previously. The script history saves recently used scripts. If you click on the  script history tool bar button, a dialog will be shown where you can see all scripts that have been used recently. Scripts are organized by script signature and the current script signature is selected by default. When you confirm the script history dialog, the current script in the editor is replaced.