# User Defined Java Expression

## ![](/files/JSH7jsf9Ex95OrUmUSwk) User Defined Java Expression

### Description <a href="#description" id="description"></a>

This transform allows you to enter Java expressions that are used to calculate new values or replace values in fields. Any custom jar libraries that your expressions depend on need to be placed in the folder `plugins/transforms/janino/lib`.&#x20;

| Hop Engine | <sup>✓</sup> |
| ---------- | ------------ |
| Spark      | ?            |
| Flink      | ?            |
| Dataflow   | ?            |

### Usage

If you have a Java expression like :

```highlight
C=A+B
```

Then you can simply enter the right side of the expression in the dialog:

```highlight
A+B
```

The values are exposed to the expressions as the Java objects they are :

| Data type | Java Class       |
| --------- | ---------------- |
| BigNumber | BigDecimal       |
| Binary    | byte\[]          |
| Date      | java.util.Date   |
| Integer   | java.lang.Long   |
| Number    | java.lang.Double |
| String    | java.lang.String |

### Options

| Option          | Description                                                                                                                                                                                        |
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Transform name  | Specify a name for this transform. This must be unique wihin the pipeline.                                                                                                                         |
| New field       | The new field in the data stream that contains the result of the expression. If you want to overwrite an existing field, you need to define the same field here and in the "Replace value" option. |
| Java expression | The Java expression to evaluate. See examples below.                                                                                                                                               |
| Value type      | The field’s data type.                                                                                                                                                                             |
| Length          | The maximum length of a string in the new field.                                                                                                                                                   |
| Precision       | The number of decimal places used to express numbers.                                                                                                                                              |
| Replace value   | Set this to be identical to the "New field" name when you want to replace the value in an existing field.                                                                                          |

### Examples

**Add 2 integers, A and B**

```highlight
A+B
```

**Concatenate 2 Strings : firstname and name and put a space in between**

```highlight
firstname+" "+name
```

or if you really care about performance, this might be faster:

```highlight
new StringBuffer(firstname).append(" ").append(name).toString()
```

**Use native Java and API functions**

```highlight
System.getProperty("os.name")
```

**Business rules (If / Then / Else)**

```highlight
a<c?true:false
```

This can be more complicated

```highlight
a<c?(a==1?1:2):3
```

even with OR and AND and other operators and functions

**Using Constants**

If you use a constant, you may need to define the right type in some expressions otherwise it could throw an error. Eg if you use "0" that is "int" but Apache Hop "Integer" is "java.lang.Long", so you’ll get an exception:

Incompatible expression types "int" and "java.lang.Long"

To solve this, use:

```highlight
test == null ? new Long(0) : test
```

The above expression checks if "test" is null and replaces it with a zero Long. Otherwise, it return "test" unchanged.

**Cut a string from end and test for null and minimal length**

Imagine you have input strings "location" like

```
Orlando FL
New York NY
```

and you want to separate the state and city. You could use the following expressions:

For state (get the last 2 characters):

```highlight
location != null && location.length()>2 ? location.substring(location.length()-2, location.length()) : null
```

For city (get the beginning without the last 2 characters and trim):

```highlight
location != null && location.length()>2 ? location.substring(0, location.length()-2).trim() : location
```

**Functionality of a LIKE operator (contains string)**

The following example returns 1 when abc is within the source string, otherwise 2. It returns also 2 when the source string is null. If you prefer the return values to be of value type Integer, use "new Long(1)" and "new Long(2)".

```highlight
samplestr != null && samplestr.indexOf("abc")>-1 ? 1 : 2
```

### Blocking specific code

As a simple security measure you can block the execution of code containing specific strings. This can be done by adding exclusions to the `codeExclusions.xml` file located at \<Hop Installation>/plugins/transforms/janino

Example:

```highlight
    <exclusions>
        <exclusion>System.</exclusion>
        <exclusion>HopVfs.</exclusion>
    </exclusions>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.primeur.com/data-shaper-1.21/knowing-the-data-shaper-designer/pipelines/transforms/userdefinedjavaexpression.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
