DataWeave – Tip #3

Last Updated on 30/03/2020 by Patryk Bandurski

DataWeave - Tip #3

I have already presented how to call Java code using messages processors with the newest Java Module for Mule 4.x. For earlier mule’s version Entry Point Resolvers were used to invoke custom Java code. However for scenarios when we would like to use custom code in DataWeave, for transformations, another approach is needed. Approach presented in this article will be more concise comparing to using message processors. It was highly extended comparing to possibilities of Mule 3.x version. Mule 3 allowed to invoke static methods. In contract Mule 4 not only permits to call static methods but also instantiate classes and access its instance attributes.

Invoke a method

Below I have attached DataWeave  code responsible for converting an array into a list. To perform conversion I will use already existing asList static method in Arrays class.

%dw 2.0
import asList from java!java::util::Arrays
output application/java
asList(["Santa Clarita Diet", "Grimm", "Harry Potter", "Under the dome", "Penny Dreadful"])

To perform call on any class first we need to import it. In order to do this import keyword is used.


As you can see we need import from java! statement. After that we provide full path to class that we are importing. In contrast to java way, as a separator two colons are used. The last part musts be the class name. We are not able to perform import of the whole package. Only class is permitted.

In our example I have imported Arrays class from java.util package. During import I was more specific and I chose only asList method to import. If I would not specify anything between import and from keywords all methods would be available.

asList method accept array of any type. As you can see DataWeave nicely converted it to a java array. Some of you may be aware that asList also accept a list of parameters that are treated as array – varargs However this feature is not supported.

Below I have imported all methods from Collections class.

%dw 2.0
import java!com::profitonilne::utils::Collections
output application/java
Collections::intersection(vars.firstList, vars.secondList)

At line 5 you can see that in order to use static interasection() method I added before class name and separated it with double colons.

Create an instance

Creating an instance is a very simple processes, that is similar to invoking a method. I have prepared Item class with two constructors. Notice that we have one private attribute called value and one public method toUpper().

package com.profitonilne.utils;

import java.nio.charset.Charset;
import java.util.Random;

public class Item {

private String value;

public Item() {
byte[] array = new byte[7]; // length is bounded by 7
new Random().nextBytes(array);
String generatedString = new String(array, Charset.forName("UTF-8"));

this.value = generatedString;

public Item(String value) {
this.value = value;

public void toUpper() {
this.value = value.toUpperCase();

In order to instantiate it and get value variable in Java I would code something like

new Item(“How I met your mother”).value

Below as you can see I used a class name Item and called the constructor using ::new construct. After that value is returned. Please notice that in order to get attribute I used dot instead of double colons.

%dw 2.0
import java!com::profitonilne::utils::Item
output application/json
payload ++ [Item::new("How I met your mother").value]

You need to be aware of the fact that we are able to instantiate a class and get its attribute but we are not able to call any non-static method at all.  We would not be able to do something like that:

Item::new(“How I met your mother”)::toUpper()


Importing and using Java code within DataWeave is straightforward. You can import all methods or only interesting ones using import keyword in the header section. DataWeave behind the scene perform all conversions to java constructs that are required by method’s parameters. In comparison to using Java Module it may be even simplified while you do not need to add your class manually to mule-artifact.json file. I found it very interesting that DataWeave engine was able to enter private variable without any setters and getters and return it. It is a pity that we are not able to chain calls of methods and instance methods. However I rate this feature high 🙂 as it simplifies java calls in DataWeave.

DataWeave – Tip #3

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top