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.

<br />
%dw 2.0<br />
import asList from java!java::util::Arrays<br />
output application/java<br />
---<br />
asList([&quot;Santa Clarita Diet&quot;, &quot;Grimm&quot;, &quot;Harry Potter&quot;, &quot;Under the dome&quot;, &quot;Penny Dreadful&quot;])<br />

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.

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

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().

<br />
package com.profitonilne.utils;</p>
<p>import java.nio.charset.Charset;<br />
import java.util.Random;</p>
<p>public class Item {</p>
<p>  private String value;</p>
<p>  public Item() {<br />
    byte[] array = new byte[7]; // length is bounded by 7<br />
    new Random().nextBytes(array);<br />
    String generatedString = new String(array, Charset.forName(&quot;UTF-8&quot;));</p>
<p>    this.value = generatedString;<br />
  }</p>
<p>   public Item(String value) {<br />
   this.value = value;<br />
   }</p>
<p>   public void toUpper() {<br />
     this.value = value.toUpperCase();<br />
   }<br />
}<br />

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.

</p>
<p>%dw 2.0<br />
import java!com::profitonilne::utils::Item<br />
output application/json<br />
---<br />
payload ++ [Item::new(&quot;How I met your mother&quot;).value]</p>
<p>

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()

Summary

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
Tagged on:             

Leave a Reply

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