In the following Java class TaxCalculator, belongs to the com.me package:
package com.me;
public class TaxCalculator {
private Double percentBaseTax = 7.0;
public Double calculateTax(Double price, Integer percentAdditionalTax) {
return price * (this.percentBaseTax + percentAdditionalTax) / 100;
}
public Boolean isTaxFree(Double price) {
if (price < 10) {
return true;
}
return false;
}
}
|
|
Unless the classes used with the Java module belong to the default package,
the classes must be exported; otherwise, execution fails with a JAVA:CLASS_NOT_FOUND error. See how to export resources.
|
To invoke instance methods:
-
Use the new operation from the Java module to create an object on which a method is later invoked. This provides an instanced object that can then call one of its methods.
-
Set the invoke operation to call one of the object’s methods. Just like the new operation,
invoke takes a map for input parameters (if the method has them) and supports
target parameters.
In the next example, an instance of the TaxCalculator class is created and placed
into the taxCalculator target variable. Then the calculateTax(Double, Integer) method is
called, which takes price and percentAdditionalTax as arguments, and its return value is
placed in the totalTax variable.
<java:new class="com.me.TaxCalculator"
constructor="TaxCalculator()"
target="taxCalculator"/>
<java:invoke instance="#[vars.taxCalculator]"
class="com.me.TaxCalculator"
method="calculateTax(Double, Integer)"
target="totalTax">
<java:args>#[{
price: 25.5,
percentAdditionalTax: 2
}]</java:args>
</java:invoke>
For the method parameters, the full package name can be specified, for example
constructor="Person(java.lang.Double, java.lang.Integer)". This is not needed, but it can
be useful to add more clarity in the code or in the case there are clashing class names in the Java code.
In Anypoint Studio, the Java module supports DataSense for the invoke operation, which provides metadata
for both the input arguments and the output value.
In the example, DataSense discovers that calculateTax returns a Number, so
the output metadata for the invoke operation looks like:
When configuring the constructor arguments in the args parameter,
the keys of the map determine how the parameters are passed to the constructor.
To reference the parameters by name (price, percentAdditionalTax, etc.),
the Java class containing the method or constructor has to be compiled
using the
-parameters
compiler flag.
If the class was not compiled with this flag, the same parameters
must be referenced in the declared order and with the canonical names
(arg0, arg1, etc.).
<java:args>#[{
arg0: 25.5,
arg1: 2
}]</java:args>
If the Java classes are defined in a Studio project, the Maven compiler plugin must be
configured in the pom.xml to compile Java classes with the -parameters flag:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>