Search code examples
java-8jasper-reports

Can I use lambda expressions in Jaspersoft Studio 6.2.0?


I use Jaspersoft Studio 6.2.0, and compile the report in a maven project with dependencies:

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.2.0</version>
</dependency>   

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports-fonts</artifactId>
    <version>6.0.0</version>
</dependency>

The report is fed with a

JRBeanArrayDataSource beanDS = new JRBeanArrayDataSource(new Incident[]{incident}, false);

I have tried to use a labmda expression in Print When Expression of a band. It seems not to recognise it. The expression is:

$F{actionList} == null || $F{actionList}.stream().allMatch(a -> ActionStatus.COMPLETED.equals(a.getStatus()))

ActionStatus is an Enum. And I get syntax errors like:

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. a cannot be resolved to a variable
    (((java.util.List)field_actionList.getValue()) == null || ((java.util.List)field_actionList.getValue()).stream().allMatch(a -> ActionStatus.COMPLETED.equals(a.getStatus()))) //$JR_EXPR_ID=248$
                                                                                                                           ^
2. Syntax error on token "-", -- expected
    (((java.util.List)field_actionList.getValue()) == null || ((java.util.List)field_actionList.getValue()).stream().allMatch(a -> ActionStatus.COMPLETED.equals(a.getStatus()))) //$JR_EXPR_ID=248$

I also tried using the method from the object directly like:

this.hasAllActionsCompleted()

that does the same thing (this is in the Incident object that is set for the report's DS) :

/**
 * Checks if the incident has all actions completed
 * @return true if all actions are completed or none defined, false otherwise
 */
public boolean hasAllActionsCompleted() {
    return actionList == null || actionList.stream().allMatch(a -> ActionStatus.COMPLETED.equals(a.getStatus()));
}

and I get:

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. The method hasAllActionsCompleted() is undefined for the type Incident_1461053626798_784639
    this.hasAllActionsCompleted() //$JR_EXPR_ID=248$

Is there another way to do this? Or should I just set the lambda expression result in a parameter for the report (this is an obvious walkaround, but I was hoping for a direct approach)?


Solution

  • And the answer is change the method to a getter, and use it as a field. In my case I changed hasAllActionsCompleted to isAllActionsCompleted and added a new field allActionsCompleted as a Boolean. Worked as a charm. Thank you.