In Spel, it is easy to assign some values for a List property. For example having object foo with a property defined as List, I usually do:
SpelParserConfiguration config = new SpelParserConfiguration(true,true);
ExpressionParser parser = new SpelExpressionParser(config);
Foo foo = new Foo();
EvaluationContext context = new StandardEvaluationContext(foo);
parser.parseExpression("barList[1].test='11111111'")
.getValue(context);
But what do you do for the case you want to assign values for a given List defined as a variable in a method. e.g:
List<String> fooList = new ArrayList<String>();
context = new StandardEvaluationContext(fooList);
parser.parseExpression("SOMETHING[0]='come on'")
.getValue(context);
In the above example, I don't know what to put instead of SOMETHING to make this work. If I put "fooList[0]='....'", it throws an exception complaining there is no fooList property in fooList.
If I put "[0]='....'", it throws Unable to grow collection: unable to determine list element type.
Then I came to define a generic wrapper like this:
public static class SpelWrapper<T>
{
T obj;
public SpelWrapper(T obj)
{
this.obj = obj;
}
public T getObj() {
return obj;
}
public void setObj(T obj) {
this.obj = obj;
}
}
and then tried to test this one:
List<String> fooList = new ArrayList<String>();
SpelWrapper<List<String>> no = new SpelWrapper<List<String>>(fooList);
context = new StandardEvaluationContext(no);
parser.parseExpression("obj[0]='olaaa'")
.getValue(context);
But it did not work and still get this ugly message:
Unable to grow collection: unable to determine list element type
I tried other expression languages like MVEL, OGNL, JEXL but I noticed they don't support auto null reference initialization which is important for me. However they did not seem to have a solution around above problem either.
I also started to think what if what I need is not a case for expression languages! The thing is my need is not just defining a variable and try to assign values using an EL.
In my case I have some simple POJO domain class beans and Some input Strings like
"bar[0].foo.value=3434"
Now I should be able to create List of Bar and put a Bar instance as its first element, then set foo property with a Foo instance and finally set Foo's value as 3434.
Any idea around this issue?
Thanks in advance
Eit I was wrong in "However they did not seem to have a solution around above problem either". For example in MVEL, this a very easy task to do. But unfortunately the SPLE's ability to auto expanding lists and automatically assigning initiators in null chains makes it incredibly proper for my case.