I have the following SPel expression:
custData.address[0].postcode
The custData
is an existing object but the address
is an empty list. It is an existing object but it is empty. When I try to set a the post code on this path then I got the
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'postcode' cannot be found on null
What I'd need that a new address
object will be put to the list and set its postcode
attribute.
Is it something that can be done in the SPel expression?
Thanks,
V.
Eventually I ended up using a custom function in spel expression.
#addIfNecessary(custData.address, 0, "uk.co.acme.AddressType").postcode
The user defined function is
import org.springframework.util.ReflectionUtils;
import java.util.List;
public class CustomFunc {
public static Object addIfNecessary(List<Object> list, Integer index, String className) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
Object o = null;
if (list != null) {
if (list.size() <= index || list.get(index) == null) {
list.set(index, Class.forName(className).newInstance());
}
o = list.get(index);
}
return o;
}
}
It is neither nice nor elegant but it works.
Please let me know if you have a more elegant one!