Contact Free trial Login

Test for an Instance of a Class

The Java Module provides a Validate type operation that tests whether an object is an instance of a class. This operation can also be configured to accept or reject subtypes of a class. If true, the operation does not modify the payload and the flow continues its execution. Otherwise, the operation throws an error, interrupting the flow, and passes control to the error handler.

Validate Type Operation

Assume the following Java classes have been defined:

package com.me;

public class Person {

    private String name;
    private Integer age;

    public Person() {
        this.name = "Max Mule";
        this.age = 10;
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}

And:

package com.me;

public class Employee extends Person {

    private String company;

    public Employee(String name, Integer age, String company) {
        super(name, age);
        this.company = company;
    }
}

The classes used with the Java Module must be exported (unless they belong to the default package). If not, the execution fails with a JAVA:CLASS_NOT_FOUND error. See how to export resources.

To check whether an object belongs to a certain class, first an instance of said object is needed. That can be done by calling the new operation to create an Employee object and placing it in a variable, in this case, named jane:

<java:new class="com.me.Employee"
    constructor="Employee(String, Integer, String)"
    target="jane">
    <java:args>#[{
      name: 'Jane Avi',
      age: 28,
      company: MuleSoft
    }]</java:args>
</java:new>

To check if the object stored in vars.jane belongs to the Employee class, the Validate type operation can be used:

<java:validate-type class="com.me.Employee" instance="#[vars.jane]" />

In this case, the object is an instance of Employee, so the flow continues executing normally, and the payload is not modified.

Check Against Subtypes (Inheritance)

The Validate type operation has the Accept subtypes parameter, which indicates if the operation should accept all subclasses of a class. This parameter can be true, false, or an expression, and it defaults to true if omitted.

Continuing with the previous example, the object in vars.jane belongs to the Employee class which extends the Person class, that is, Employee is a subtype of Person. So executing this example resolves to true and the flow executes normally.

<java:validate-type
    class="com.me.Person"
    instance="#[vars.jane]"
    acceptSubtypes="true" />

Because when omitting the Accept subtypes parameter it defaults to true, the previous code is equivalent to:

<java:validate-type
    class="com.me.Person"
    instance="#[vars.jane]" />

However, when setting the Accept subtypes to false:

<java:validate-type
    class="com.me.Person"
    instance="#[vars.jane]"
    acceptSubtypes="false" />

During execution the operation throws an error, interrupting the flow and passing control to the error handler.

Validate Type Function in DataWeave

In this example, the Java module provides a DataWeave isInstanceOf() function that returns true or false if an object is an instance of a class. Unlike the Validate type operation, it does not throw an error when it evaluates to false.

<choice>
    <when expression="#[Java::isInstanceOf(vars.jane, com.me.Employee)]">
        <flow-ref name="routeToCompany" />
    </when>
</choice>

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.