Class Operator

    • Field Detail

      • METHOD_STARTS_WITH

        private static final java.lang.String METHOD_STARTS_WITH
        See Also:
        Constant Field Values
      • METHOD_ENDS_WITH

        private static final java.lang.String METHOD_ENDS_WITH
        See Also:
        Constant Field Values
      • CMP_OPS

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

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

      • POSTFIX_OPS

        private static final java.util.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 java.util.Set<JexlOperator> overloads
        The set of overloaded operators.
      • caching

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

      • 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,
                        java.util.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,
                        java.util.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 Detail

      • getOperator

        public JexlMethod getOperator​(JexlOperator operator,
                                      java.lang.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 java.lang.Object[] arguments​(JexlOperator operator,
                                             java.lang.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 java.lang.Boolean booleanDuckCall​(java.lang.String methodName,
                                                  java.lang.Object left,
                                                  java.lang.Object right)
                                           throws java.lang.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:
        java.lang.Exception - if invocation fails
      • controlNullOperands

        private void controlNullOperands​(JexlArithmetic arithmetic,
                                         JexlOperator operator,
                                         java.lang.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,
                                    java.lang.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,
                                             java.lang.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 java.lang.Object empty​(JexlCache.Reference node,
                                      java.lang.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 java.lang.Object size​(JexlCache.Reference node,
                                     java.lang.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,
                                java.lang.Object left,
                                java.lang.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,
                                  java.lang.Object left,
                                  java.lang.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,
                                java.lang.Object left,
                                java.lang.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 java.lang.Object tryAssignOverload​(JexlCache.Reference node,
                                                  JexlOperator operator,
                                                  java.util.function.Consumer<java.lang.Object> assignFun,
                                                  java.lang.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 java.lang.Object performBaseOperation​(JexlOperator operator,
                                                      java.lang.Object... args)
        Performs the base operation of an assignment.
        Parameters:
        operator - the operator
        args - the arguments
        Returns:
        the result
      • tryEval

        private java.lang.Object tryEval​(JexlCache.Reference node,
                                         JexlOperator operator,
                                         java.lang.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,
                                                java.lang.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