Class OptionalELResolver

java.lang.Object
jakarta.el.ELResolver
jakarta.el.OptionalELResolver

public class OptionalELResolver extends ELResolver
Defines property resolution, method invocation and type conversion behaviour on Optionals.

This resolver handles base objects that are instances of Optional.

This resolver is always a read-only resolver since Optional instances are immutable.

Since:
EL 6.0
  • Constructor Details

    • OptionalELResolver

      public OptionalELResolver()
  • Method Details

    • getValue

      public Object getValue(ELContext context, Object base, Object property)
      Obtain the value of the given property on the given object using the given context.
      Specified by:
      getValue in class ELResolver
      Parameters:
      context - The EL context for this evaluation
      base - The base object on which the property is to be found
      property - The property whose value is to be returned
      Returns:
      If the base object is an Optional and Optional.isEmpty() returns true then the resulting value is null.

      If the base object is an Optional, Optional.isPresent() returns true and the property is null then the resulting value is the result of calling Optional.get() on the base object.

      If the base object is an Optional, Optional.isPresent() returns true and the property is not null then the resulting value is the result of calling ELResolver.getValue(ELContext, Object, Object) using the ELResolver obtained from ELContext.getELResolver() with the following parameters:

      • The ELContext is the current context
      • The base object is the result of calling Optional.get() on the current base object
      • The property object is the current property object

      If the base object is not an Optional then the return value is undefined.

    • getType

      public Class<?> getType(ELContext context, Object base, Object property)
      Obtain the most generally acceptable type that may be used to set the given property on the given object using the given context.
      Specified by:
      getType in class ELResolver
      Parameters:
      context - The EL context for this evaluation
      base - The base object on which the property is to be found
      property - The property whose type is to be returned
      Returns:
      If the base object is an Optional this method always returns null since instances of this resolver are always read-only.

      If the base object is not an Optional then the return value is undefined.

    • setValue

      public void setValue(ELContext context, Object base, Object property, Object value)
      Set the value of the given property on the given object using the given context.

      If the base object is an Optional this method always throws a PropertyNotWritableException since instances of this resolver are always read-only.

      Specified by:
      setValue in class ELResolver
      Parameters:
      context - The EL context for this evaluation
      base - The base object on which the property is to be found
      property - The property whose value is to be set
      value - The value to set the property to
    • isReadOnly

      public boolean isReadOnly(ELContext context, Object base, Object property)
      Determine if the given property on the given object is read-only using the given context.
      Specified by:
      isReadOnly in class ELResolver
      Parameters:
      context - The EL context for this evaluation
      base - The base object on which the property is to be found
      property - The property to be checked for read only status
      Returns:
      If the base object is an Optional this method always returns true since instances of this resolver are always read-only.

      If the base object is not an Optional then the return value is undefined.

    • getCommonPropertyType

      public Class<?> getCommonPropertyType(ELContext context, Object base)
      Obtain the most common type that is acceptable for the given base object.
      Specified by:
      getCommonPropertyType in class ELResolver
      Parameters:
      context - The context in which the examination takes place
      base - The object to examine
      Returns:
      If the base object is an Optional this method always returns Object.class.

      If the base object is not an Optional then the return value is undefined.

    • convertToType

      public <T> T convertToType(ELContext context, Object obj, Class<T> type)
      Converts the given object to the given type.
      Overrides:
      convertToType in class ELResolver
      Type Parameters:
      T - The type to which the object should be converted
      Parameters:
      context - The EL context for this evaluation
      obj - The object to convert
      type - The type to which the object should be converted
      Returns:
      If the base object is an Optional and Optional.isEmpty() returns true then this method returns the result of coercing null to the requested type.

      If the base object is an Optional and Optional.isPresent() returns true then this method returns the result of coercing Optional#get() to the requested type.

      If the base object is not an Optional then the return value is undefined.

    • invoke

      public Object invoke(ELContext context, Object base, Object method, Class<?>[] paramTypes, Object[] params)
      Invokes a method on the the given object.
      Overrides:
      invoke in class ELResolver
      Parameters:
      context - The EL context for this evaluation
      base - The base object on which the method is to be found
      method - The method to invoke
      paramTypes - The types of the parameters of the method to invoke
      params - The parameters with which to invoke the method
      Returns:
      If the base object is an Optional and Optional.isEmpty() returns true then this method returns null.

      If the base object is an Optional and Optional.isPresent() returns true then this method returns the result of invoking the specified method on the object obtained by calling Optional.get() with the specified parameters.

      If the base object is not an Optional then the return value is undefined.