-1

Please look at example code:

public static class MyTestClass {
    public List<Boolean> booleans = new ArrayList<>();
}

public static void main(String[] args) {
    SpelParserConfiguration config = new SpelParserConfiguration(true, true);

    MyTestClass myTestClass = new MyTestClass();

    SpelExpressionParser parser = new SpelExpressionParser(config);
    Object value = parser.parseExpression("booleans[1]").getValue(myTestClass); //exception

I try to initalize boolean list with default values. The problem is, Boolean class does not have default constructor so I got exception:

Caused by: java.lang.NoSuchMethodException: java.lang.Boolean.<init>()

It is possible to write how new objects should be initalized if they dont exists in list?

Thomas Banderas
  • 1,681
  • 1
  • 21
  • 43
  • *"I try to initalize boolean list with default values"* Where do you try that? --- Did you try e.g. `public List booleans = Arrays.asList(true, false, false, true);` for a list of size 4? – Andreas Jun 25 '19 at 17:21
  • @Andreas fixed 'simple' I try to intalize it automatically by SpEL expressions instead of manually while list creation – Thomas Banderas Jun 25 '19 at 18:54
  • Since your question is about the initialization, it would be good to actually see *how* you're (trying) to do that initialization, don't you think? – Andreas Jun 25 '19 at 18:59

1 Answers1

1

I am sorry that the auto-grow collection features in SpEL only works if the item has the default constructor.

Unless you have a strong reason the need to initialise a list using SpEL , I suggest you use the normal java method call , it is much faster than SpEL and also type safely.

If you insist doing it in SpEL , the best you can do is :

List<Boolean> result =  parser.parseExpression( "{true,true,false,false,true}").getValue(List.class);
myTestClass.booleans = result;
Ken Chan
  • 84,777
  • 26
  • 143
  • 172