Calling Java functions in expressions
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.
- 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.