Class Operator

java.lang.Object
org.apache.commons.jexl3.internal.Operator
All Implemented Interfaces:
JexlArithmetic.Uberspect, JexlOperator.Uberspect

public final class Operator extends Object implements JexlOperator.Uberspect
Helper class to deal with operator overloading and specifics.
Since:
3.0
  • Field Details

    • METHOD_IS_EMPTY

      private static final String METHOD_IS_EMPTY
      See Also:
    • METHOD_SIZE

      private static final String METHOD_SIZE
      See Also:
    • METHOD_CONTAINS

      private static final String METHOD_CONTAINS
      See Also:
    • METHOD_STARTS_WITH

      private static final String METHOD_STARTS_WITH
      See Also:
    • METHOD_ENDS_WITH

      private static final String METHOD_ENDS_WITH
      See Also:
    • CMP_OPS

      private static final Set<JexlOperator> CMP_OPS
      The comparison operators.

      Used to determine if a compare method overload might be used.

    • POSTFIX_OPS

      private static final Set<JexlOperator> POSTFIX_OPS
      The postfix operators.

      Used to determine the returned value in assignment.

    • uberspect

      private final JexlUberspect uberspect
      The uberspect.
    • arithmetic

      private final JexlArithmetic arithmetic
      The arithmetic instance being analyzed.
    • overloads

      private final Set<JexlOperator> overloads
      The set of overloaded operators.
    • delegate

      private final JexlArithmetic.Uberspect delegate
      The delegate if built as a 3.4 legacy.
    • caching

      private volatile int caching
      Caching state: -1 unknown, 0 false, 1 true.
  • Constructor Details

    • Operator

      public Operator(JexlUberspect theUberspect, JexlArithmetic theArithmetic)
      Creates an instance.

      Mostly used as a compatibility measure by delegating instead of extending.

      Parameters:
      theUberspect - The uberspect instance
      theArithmetic - The arithmetic instance used to delegate operator overloads
    • Operator

      public Operator(JexlUberspect theUberspect, JexlArithmetic theArithmetic, Set<JexlOperator> theOverloads)
      Creates an instance.
      Parameters:
      theUberspect - The uberspect instance
      theArithmetic - The arithmetic instance
      theOverloads - The overloaded operators
    • Operator

      public Operator(JexlUberspect theUberspect, JexlArithmetic theArithmetic, Set<JexlOperator> theOverloads, int theCache)
      Creates an instance.
      Parameters:
      theUberspect - The uberspect instance
      theArithmetic - The arithmetic instance
      theOverloads - The overloaded operators
      theCache - The caching state
  • Method Details

    • getOperator

      public JexlMethod getOperator(JexlOperator operator, Object... args)
      Description copied from interface: JexlArithmetic.Uberspect
      Gets the most specific method for an operator.
      Specified by:
      getOperator in interface JexlArithmetic.Uberspect
      Parameters:
      operator - The operator
      args - The arguments
      Returns:
      The most specific method or null if no specific override could be found
    • overloads

      public boolean overloads(JexlOperator operator)
      Description copied from interface: JexlArithmetic.Uberspect
      Checks whether this uberspect has overloads for a given operator.
      Specified by:
      overloads in interface JexlArithmetic.Uberspect
      Parameters:
      operator - The operator to check
      Returns:
      true if an overload exists, false otherwise
    • isCaching

      private boolean isCaching()
      Returns:
      whether caching is enabled in the engine
    • arguments

      private Object[] arguments(JexlOperator operator, Object... args)
      Tidy arguments based on operator arity.

      The interpreter may add a null to the arguments of operator expecting only one parameter.

      Parameters:
      operator - The operator
      args - The arguments (as seen by the interpreter)
      Returns:
      The tidied arguments
    • booleanDuckCall

      private Boolean booleanDuckCall(String methodName, Object left, Object right) throws Exception
      Attempts finding a method in left and eventually narrowing right.
      Parameters:
      methodName - The method name
      right - The left argument in the operator
      left - The right argument in the operator
      Returns:
      A boolean is call was possible, null otherwise
      Throws:
      Exception - Thrown if invocation fails
    • controlNullOperands

      private void controlNullOperands(JexlArithmetic arithmetic, JexlOperator operator, Object... args)
      Throw a NPE if operator is strict and one of the arguments is null.
      Parameters:
      arithmetic - The JEXL arithmetic instance
      operator - The operator to check
      args - The operands
      Throws:
      JexlArithmetic.NullOperand - if operator is strict and an operand is null
    • operatorError

      private <T> T operatorError(JexlCache.Reference ref, JexlOperator operator, Throwable cause, T alt)
      Triggered when an operator fails.
      Type Parameters:
      T - the return type
      Parameters:
      ref - The node where the error originated from
      operator - The operator symbol
      cause - The cause of error (if any)
      alt - what to return if not strict
      Returns:
      throws JexlException if strict and not silent, null otherwise
    • uberspectOperator

      private JexlMethod uberspectOperator(JexlArithmetic arithmetic, JexlOperator operator, Object... args)
      Seeks an implementation of an operator method in an arithmetic instance.

      Method must <>not/em belong to JexlArithmetic

      Parameters:
      arithmetic - The arithmetic instance
      operator - The operator
      args - The arguments
      Returns:
      A JexlMethod instance or null
    • returnsBoolean

      private boolean returnsBoolean(JexlMethod vm)
      Checks whether a method returns a boolean or a Boolean.
      Parameters:
      vm - The JexlMethod (can be null)
      Returns:
      true of false
    • returnsInteger

      private boolean returnsInteger(JexlMethod vm)
      Checks whether a method returns an int or an Integer.
      Parameters:
      vm - The JexlMethod (can be null)
      Returns:
      true of false
    • empty

      public Object empty(JexlCache.Reference node, Object object)
      Description copied from interface: JexlOperator.Uberspect
      Check for emptiness of various types: Collection, Array, Map, String, and anything that has a boolean isEmpty() method.

      Seeks an overload or use the default arithmetic implementation.

      Note that the result may not be a boolean.

      Specified by:
      empty in interface JexlOperator.Uberspect
      Parameters:
      node - The node holding the object
      object - The object to check the emptiness of
      Returns:
      The evaluation result
    • size

      public Object size(JexlCache.Reference node, Object object)
      Description copied from interface: JexlOperator.Uberspect
      Calculate the size of various types: Collection, Array, Map, String, and anything that has an int size() method.

      Seeks an overload or use the default arithmetic implementation.

      Note that the result may not be an integer.

      Specified by:
      size in interface JexlOperator.Uberspect
      Parameters:
      node - An optional reference caching resolved method or failing signature
      object - The object to get the size of
      Returns:
      The evaluation result
    • contains

      public boolean contains(JexlCache.Reference node, JexlOperator operator, Object left, Object right)
      Description copied from interface: JexlOperator.Uberspect
      The 'match'/'in' operator implementation.

      Seeks an overload or use the default arithmetic implementation.

      Note that 'x in y' or 'x matches y' means 'y contains x' ; the JEXL operator arguments order syntax is the reverse of this method call.

      Specified by:
      contains in interface JexlOperator.Uberspect
      Parameters:
      node - An optional reference caching resolved method or failing signature
      operator - The calling operator, =~ or !~
      left - The right operand
      right - The left operand
      Returns:
      true if left matches right, false otherwise
    • startsWith

      public boolean startsWith(JexlCache.Reference node, JexlOperator operator, Object left, Object right)
      Description copied from interface: JexlOperator.Uberspect
      The 'startsWith' operator implementation.

      Seeks an overload or use the default arithmetic implementation.

      Specified by:
      startsWith in interface JexlOperator.Uberspect
      Parameters:
      node - An optional reference caching resolved method or failing signature
      operator - The calling operator, $= or $!
      left - The left operand
      right - The right operand
      Returns:
      true if left starts with right, false otherwise
    • endsWith

      public boolean endsWith(JexlCache.Reference node, JexlOperator operator, Object left, Object right)
      Description copied from interface: JexlOperator.Uberspect
      The 'endsWith' operator implementation.

      Seeks an overload or use the default arithmetic implementation.

      Specified by:
      endsWith in interface JexlOperator.Uberspect
      Parameters:
      node - An optional reference caching resolved method or failing signature
      operator - The calling operator, ^= or ^!
      left - The left operand
      right - The right operand
      Returns:
      true if left ends with right, false otherwise
    • tryAssignOverload

      public Object tryAssignOverload(JexlCache.Reference node, JexlOperator operator, Consumer<Object> assignFun, Object... args)
      Description copied from interface: JexlOperator.Uberspect
      Evaluates an assign operator.

      This takes care of finding and caching the operator method when appropriate. If an overloads returns a value not-equal to TRY_FAILED, it means the side-effect is complete. Otherwise, a += b <=> a = a + b

      Specified by:
      tryAssignOverload in interface JexlOperator.Uberspect
      Parameters:
      node - An optional reference caching resolved method or failing signature
      operator - The operator
      assignFun - The actual function that performs the side effect
      args - The arguments, the first one being the target of assignment
      Returns:
      JexlEngine.TRY_FAILED if no operation was performed, the value to use as the side effect argument otherwise
    • performBaseOperation

      private Object performBaseOperation(JexlOperator operator, Object... args)
      Performs the base operation of an assignment.
      Parameters:
      operator - The operator
      args - The arguments
      Returns:
      The result
    • tryOverload

      public Object tryOverload(JexlCache.Reference node, JexlOperator operator, Object... args)
      Description copied from interface: JexlOperator.Uberspect
      Try to find the most specific method and evaluate an operator.

      This method does not call JexlArithmetic.Uberspect.overloads(JexlOperator) and shall not be called with an assignment operator; use JexlOperator.Uberspect.tryAssignOverload(JexlCache.Reference, JexlOperator, Consumer, Object...) in that case.

      Specified by:
      tryOverload in interface JexlOperator.Uberspect
      Parameters:
      node - An optional reference caching resolved method or failing signature
      operator - The operator
      args - The arguments
      Returns:
      TRY_FAILED if no specific method could be found, the evaluation result otherwise
    • tryEval

      private Object tryEval(JexlCache.Reference node, JexlOperator operator, Object... args)
      Tries operator evaluation, handles method resolution caching.
      Parameters:
      node - The node
      operator - The operator
      args - The operator arguments
      Returns:
      The operator evaluation result or TRY_FAILED
    • getAlternateOverload

      private JexlMethod getAlternateOverload(JexlOperator operator, Object... args)
      Special handling of overloads where another attempt at finding a method may be attempted.

      As of 3.5.0, only the comparison operators attempting to use compare() are handled.

      Parameters:
      operator - The operator
      args - The arguments
      Returns:
      An instance or null