# method-script-api

Content of the MCP resource `jprofiler://api/method-scripts` of the JProfiler MCP server, see the [tool and resource reference](../tools.md) for the full list of tools and resources.

# Method script API

API reference for the scripts of `recordingConfig.advanced.splitMethods`,
`recordingConfig.advanced.complexityMethods` and `recordingConfig.advanced.scriptProbes`: the script
text is compiled as the body of the script interface method - `SplitScript.enter` returns the split key
string (null leaves the invocation unsplit), `ComplexityScript.enter` returns the input size of the
invocation as a long, `ProbeScript.getPayload` returns the payload string of a script probe event (null
records no payload).

Available parameters:

- `scriptContext`: the `ScriptContext` below, a per-script state store that survives across invocations.
- `clazz`: the `Class` of the declaring class of the instrumented method.
- `currentObject`: the instance the method is invoked on, null for static methods.
- `param0`, `param1`, ...: the declared method parameters in order, statically typed when their classes
  are on the compile classpath and `Object` otherwise. Add application JARs with
  `recordingConfig.advanced.scriptClassPath` to get static typing.

The script runs on every invocation of the method, so keep it cheap and do not instrument extremely hot
methods.

## Types

Flat alphabetical index of every type in the API. Use this when you know the qualified name and want to skip package navigation.

| Qualified name | Kind | File |
|---|---|---|
| `com.jprofiler.agent.scripts.ComplexityScript` | interface | [ComplexityScript](#com-jprofiler-agent-scripts-complexityscript) |
| `com.jprofiler.agent.scripts.ExpandParameter` | annotation | [ExpandParameter](#com-jprofiler-agent-scripts-expandparameter) |
| `com.jprofiler.agent.scripts.ProbeScript` | interface | [ProbeScript](#com-jprofiler-agent-scripts-probescript) |
| `com.jprofiler.agent.scripts.SplitScript` | interface | [SplitScript](#com-jprofiler-agent-scripts-splitscript) |
| `com.jprofiler.api.agent.ScriptContext` | interface | [ScriptContext](#com-jprofiler-api-agent-scriptcontext) |

## <a id="package-com-jprofiler-agent-scripts"></a>Package `com.jprofiler.agent.scripts`

### <a id="com-jprofiler-agent-scripts-complexityscript"></a>ComplexityScript

Import: `import com.jprofiler.agent.scripts.ComplexityScript;`

```java
interface ComplexityScript
```

#### Methods

| Signature | Returns | Description |
|---|---|---|
| [`enter(ScriptContext, Class, Object, Object, Object, Object, Object, Object)`](#com-jprofiler-agent-scripts-complexityscript-enter-scriptcontext-class-object-object-object-object-object-object) | `long` |  |
| [`enter(ScriptContext, Class, Object, Object[])`](#com-jprofiler-agent-scripts-complexityscript-enter-scriptcontext-class-object-object) | `long` |  |

<a id="com-jprofiler-agent-scripts-complexityscript-enter-scriptcontext-class-object-object-object-object-object-object"></a>##### enter(ScriptContext, Class, Object, Object, Object, Object, Object, Object)

```java
long enter(ScriptContext scriptContext, Class clazz, Object object, Object p0, Object p1, Object p2, Object p3, Object p4) throws Exception
```

<a id="com-jprofiler-agent-scripts-complexityscript-enter-scriptcontext-class-object-object"></a>##### enter(ScriptContext, Class, Object, Object[])

```java
long enter(ScriptContext scriptContext, Class clazz, Object object, Object[] parameters) throws Exception
```

### <a id="com-jprofiler-agent-scripts-expandparameter"></a>ExpandParameter

Import: `import com.jprofiler.agent.scripts.ExpandParameter;`

```java
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface ExpandParameter implements Annotation
```

### <a id="com-jprofiler-agent-scripts-probescript"></a>ProbeScript

Import: `import com.jprofiler.agent.scripts.ProbeScript;`

```java
@ExpandParameter
interface ProbeScript
```

#### Methods

| Signature | Returns | Description |
|---|---|---|
| [`getPayload(ScriptContext, Class<?>, Object, Object[])`](#com-jprofiler-agent-scripts-probescript-getpayload-scriptcontext-class-object-object) | `String` |  |

<a id="com-jprofiler-agent-scripts-probescript-getpayload-scriptcontext-class-object-object"></a>##### getPayload(ScriptContext, Class<?>, Object, Object[])

```java
String getPayload(ScriptContext scriptContext, Class<?> declaringClass, Object instance, Object[] arguments) throws Exception
```

### <a id="com-jprofiler-agent-scripts-splitscript"></a>SplitScript

Import: `import com.jprofiler.agent.scripts.SplitScript;`

```java
interface SplitScript
```

#### Methods

| Signature | Returns | Description |
|---|---|---|
| [`enter(ScriptContext, Class, Object, Object, Object, Object, Object, Object)`](#com-jprofiler-agent-scripts-splitscript-enter-scriptcontext-class-object-object-object-object-object-object) | `String` |  |
| [`enter(ScriptContext, Class, Object, Object[])`](#com-jprofiler-agent-scripts-splitscript-enter-scriptcontext-class-object-object) | `String` |  |

<a id="com-jprofiler-agent-scripts-splitscript-enter-scriptcontext-class-object-object-object-object-object-object"></a>##### enter(ScriptContext, Class, Object, Object, Object, Object, Object, Object)

```java
String enter(ScriptContext scriptContext, Class clazz, Object object, Object p0, Object p1, Object p2, Object p3, Object p4) throws Exception
```

<a id="com-jprofiler-agent-scripts-splitscript-enter-scriptcontext-class-object-object"></a>##### enter(ScriptContext, Class, Object, Object[])

```java
String enter(ScriptContext scriptContext, Class clazz, Object object, Object[] parameters) throws Exception
```

## <a id="package-com-jprofiler-api-agent"></a>Package `com.jprofiler.api.agent`

### <a id="com-jprofiler-api-agent-scriptcontext"></a>ScriptContext

Import: `import com.jprofiler.api.agent.ScriptContext;`

```java
interface ScriptContext
```

Context object that is passed to all scripts and provides ways to store key-value pairs for all subsequent
invocations of any custom script.

For example, if you need to calculate the change of an observed numeric value between script invocations, you can use
the context as follows:

```
    long previousValue = scriptContext.getLong("myValue");
    long currentValue = ...;
    long delta = currentValue - previousValue;
    // Use delta
    scriptContext.putLong("myValue", currentValue); // for the next invocation of the script
```

#### Methods

| Signature | Returns | Description |
|---|---|---|
| [`getObject(Object)`](#com-jprofiler-api-agent-scriptcontext-getobject-object) | `Object` | Returns the value for the specified key in the script context. |
| [`putObject(Object, Object)`](#com-jprofiler-api-agent-scriptcontext-putobject-object-object) | `Object` | Store a key-value pair in the script context. |
| [`getLong(Object)`](#com-jprofiler-api-agent-scriptcontext-getlong-object) | `long` | Returns the long value for the specified key in the script context. |
| [`putLong(Object, long)`](#com-jprofiler-api-agent-scriptcontext-putlong-object-long) | `long` | Store a key-value pair of value type long in the script context. |

<a id="com-jprofiler-api-agent-scriptcontext-getobject-object"></a>##### getObject(Object)

```java
Object getObject(Object key)
```

Returns the value for the specified key in the script context.


**Parameters:**

- `key` — the key

**Returns:** the value, or `null` if there was no mapping for the key.

<a id="com-jprofiler-api-agent-scriptcontext-putobject-object-object"></a>##### putObject(Object, Object)

```java
Object putObject(Object key, Object value)
```

Store a key-value pair in the script context. An existing mapping is overwritten.


**Parameters:**

- `key` — the key
- `value` — the value

**Returns:** the value that was previously associated with the specified key, or `null` if there was no mapping for the key.

<a id="com-jprofiler-api-agent-scriptcontext-getlong-object"></a>##### getLong(Object)

```java
long getLong(Object key)
```

Returns the long value for the specified key in the script context.


**Parameters:**

- `key` — the key

**Returns:** the long value, or 0 if there was no mapping

<a id="com-jprofiler-api-agent-scriptcontext-putlong-object-long"></a>##### putLong(Object, long)

```java
long putLong(Object key, long value)
```

Store a key-value pair of value type long in the script context. An existing mapping is overwritten.


**Parameters:**

- `key` — the key
- `value` — the long value

**Returns:** the value that was previously associated with the specified key, or 0 if there was no mapping for the key.
