expression-language
expression-language copied to clipboard
ELResolver for java.util.Optional
There are often times where a field is better modelled as an Optional<T> instead using a traditional getter for T.
Using a getter allows expressions like #{domainObject.optionalProperty.requiredProperty} which resolves the requiredProperty even if the optionalProperty might be null.
- if
domainObject.optionalPropertyisnullthendomainObject.optionalProperty.requiredPropertywill resolve tonullas well - if
domainObject.optionalPropertyis notnullthen domainObject.optionalProperty.requiredProperty` will resolve to its content
This behavior denotes exactly how Optional as map its resolving operation works.
- if
domainObject.optionalPropertyisemptythendomainObject.maybeOptionalProperty().map(OptionalProperty::getRequiredProperty)will resolve toemptyas well - if
domainObject.optionalPropertyispresentthendomainObject.maybeOptionalProperty().map(OptionalProperty::getRequiredProperty)will resolve to its content
The current ELResolver does not support this natively and the current specification does not mention java.util.Optional in such way.
I would like to evaluate the expression #{domainObject.optionalProperty.requiredProperty} even though optionalProperty is of type Optional<T>.