autoGrowCollections example

In this post under Spring SpEL, I will show with example the purpose of “autoGrowCollections” configuration.

When evaluating a SpEL expression, containing collections, if we try to access an entry which doesn’t exist in collection. We get an exception.

Lets say we have the below pojo class

Employee

package spel.package22;

public class Employee {
    private Integer id;
    private String name;
    private int salary;

    public Employee() {
    }

    public Employee(Integer id, String name, int salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    //Removed getter and setter for brevity
}

and we have a collection of “Employee” class with only two entries.

The below SpEL expression will fail

employees[3]

As index 3 is beyond the collections size.

We get the below exception

org.springframework.expression.spel.SpelEvaluationException: EL1025E: The collection has '2' elements, index '3' is invalid

To prevent this exception from happening, we can take help of “autoGrowCollections” configuration.

If “autoGrowCollections” value is true, SpEL creates a two new entry in collection at index 2 and 3.

Create two instances of “Employee” class and assign it to index 2 and 3 in the collection.

Below is the complete main class showing how to use “autoGrowCollections”.

Main class

package spel.package22;

import org.springframework.expression.Expression;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.ArrayList;
import java.util.List;

public class Example22 {
    private List<Employee> employees;

    public Example22() {
        employees = new ArrayList<>(2);
        employees.add(new Employee(1, "John Doe 1", 1000));
        employees.add(new Employee(2, "John Doe 2", 2000));
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }

    public static void main(String[] args) {
        SpelParserConfiguration spelParserConfiguration = new SpelParserConfiguration(false, true);
        SpelExpressionParser expressionParser = new SpelExpressionParser(spelParserConfiguration);
        Expression expression = expressionParser.parseExpression("employees[3]");
        Example22 example22 = new Example22();
        System.out.println("Size: " + example22.getEmployees().size());
        expression.getValue(example22, Employee.class);
        System.out.println("Size: " + example22.getEmployees().size());
    }
}

In the above code, at line 28, I create an instance of “SpelParserConfiguration” named “spelParserConfiguration” with constructor argument “false” for “autoGrowNullReferences” and “true” for “autoGrowCollections”.

At line 29, I create an instance of “SpelExpressionParser” passing instance “spelParserConfiguration” as constructor argument.

At line 30, I parse the expression “employees[3]” and create an instance of “Expression” interface.

At line 31, I create an instance of “Example22” class.

At line 32, I print the size of employees collection, which in this case will be 2.

At line 33, I evaluate the expression created at line 30.

At line 34, I again print the size of employees collection, which in this case will be 4.

Below is the output

Output

Size: 2
Size: 4

In this way we can use “autoGrowCollections” configuration

Leave a comment