Skip to main content Skip to complementary content

Calling Java functions in expressions

A Java function call is a call to a method of a Java class (static method) or a Java object that you can use directly in data shaping expressions using the java:call keyword. To enable Java function call, you must select the Enable java function calls in DSQL Maps check box in Preferences > Talend > Hierarchical Mapper.

Calling static Java methods

To call a static method, specify the class name, the method name, and any required arguments as follows: java:call(ClassName, MethodName, argsOfMethod...), where ClassName and MethodName are string values and the other arguments can be any type of value. For example:


java:call("java.lang.Double", "compare", 3.1415d, 1.51d)
      

This example compares two double values using Java Double.compare() method.

Calling non-static Java methods

To call a non-static method, create an instance of the class with java:new by specifying the class name and the arguments of the constructor as follows: java:call(java:new(ClassName, argsOfConstructor), MethodName,argsOfMethod...), where ClassName and MethodName are string values and the other arguments can be any type of value. The instance of the class with java:new must be specified as the first argument of the Java call. The second argument must be the name of the method. For example:


java:call(java:new("java.util.Random", 536815L), "nextInt", 100)
      

This example creates a Random object with a seed and calls its nextInt method.

Information noteImportant:
  • You cannot assign the result of java:new to a variable. Use it directly in java:call.
  • You cannot use java:new alone. java:new is only used as the first argument of a Java function call.

Chaining Java calls

You can chain calls by using the result of one java:call as the first argument in another. For example:

  • 
    java:call(java:call("java.lang.String", "valueOf", 100), "concat", "$")
          

    This example converts the integer 100 to a string, then concatenates a dollar sign.

  • 
    java:call(java:call(java:new("java.text.SimpleDateFormat", "yyyy-MM-dd"), "parse", "2018-10-15"), "getDate")
          

    This example creates a SimpleDateFormat object, parses a date string, and retrieves the date.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!