Class ExpressionUtils


  • public final class ExpressionUtils
    extends Object
    Utility classes to deal with Expression instances
    Since:
    0.3.0
    See Also:
    Utils
    • Constructor Detail

      • ExpressionUtils

        public ExpressionUtils()
    • Method Detail

      • hasArguments

        public boolean hasArguments​(MethodCallExpression methodCallExpr)
        Given a MethodCallExpression it returns whether it has arguments or not
        Parameters:
        methodCallExpr - a method call we want the arguments from
        Returns:
        true if the method has any argument false otherwise
        Since:
        0.3.0
      • getFirstArgumentAs

        public <U extends Expression> U getFirstArgumentAs​(MethodCallExpression methodCallExpr,
                                                           Class<U> asType)
        Return the first element of the ArgumentListExpression passed as parameters as the expected type.
        Parameters:
        methodCallExpr - a method call we want the arguments from
        asType - the expected type
        Returns:
        the first argument casted as the expected type
        Since:
        0.3.0
      • getFirstArgumentAs

        public <U extends Expression> U getFirstArgumentAs​(ArgumentListExpression args,
                                                           Class<U> asType)
        Return the first element of the ArgumentListExpression passed as parameters as the expected type.
        Parameters:
        args - the list of arguments
        asType - the expected type
        Returns:
        the first argument casted as the expected type
        Since:
        0.1.4
      • getArgumentByIndexAs

        public <U extends Expression> U getArgumentByIndexAs​(MethodCallExpression methodCallExpr,
                                                             Integer index,
                                                             Class<U> asType)
        Return the element in the specified index from the ArgumentListExpression passed as argument as the expected type.
        Parameters:
        methodCallExpr - a method call we want the arguments from
        index - the index where the expression is located
        asType - the Class of the expected expression
        Returns:
        the expression found in the expression list using the provided index or null if it couldn't be found
        Since:
        0.3.0
      • getArgumentByIndexAs

        public <U extends Expression> U getArgumentByIndexAs​(ArgumentListExpression args,
                                                             Integer index,
                                                             Class<U> asType)
        Return the element in the specified index from the ArgumentListExpression passed as argument as the expected type.
        Parameters:
        args - the argument list expression we want the argument from
        index - the index where the expression is located
        asType - the Class of the expected expression
        Returns:
        the expression found in the expression list using the provided index or null if it couldn't be found
        Since:
        0.2.8
      • getLastArgumentAs

        public <U extends Expression> U getLastArgumentAs​(MethodCallExpression methodCallExpr,
                                                          Class<U> asType)
        Return the last element of the ArgumentListExpression passed as parameters as the expected type.
        Parameters:
        asType - the expected type
        Returns:
        the last argument casted as the expected type
        Since:
        0.3.0
      • getLastArgumentAs

        public <U extends Expression> U getLastArgumentAs​(ArgumentListExpression args,
                                                          Class<U> asType)
        Return the last element of the ArgumentListExpression passed as parameters as the expected type.
        Parameters:
        args - the list of arguments
        asType - the expected type
        Returns:
        the last argument casted as the expected type
        Since:
        0.1.4
      • isMethodCallExpr

        public boolean isMethodCallExpr​(Expression expr)
        Checks whether an expression is of type MethodCallExpression
        Parameters:
        expr - the evaluated expression
        Returns:
        true if the expression is an instance of MethodCallExpression
        Since:
        0.3.0
      • isListExpr

        public boolean isListExpr​(Expression expr)
        Checks whether an expression is of type ListExpression
        Parameters:
        expr - the evaluated expression
        Returns:
        true if the expression is an instance of ListExpression
        Since:
        0.3.0
      • isBinaryExpr

        public boolean isBinaryExpr​(Expression expr)
        Checks whether an expression is of type BinaryExpression
        Parameters:
        expr - the evaluated expression
        Returns:
        true if the expression is an instance of BinaryExpression
        Since:
        0.3.0
      • isBinaryExpr

        public boolean isBinaryExpr​(Expression expr,
                                    int operator)
        Checks whether an expression is of type BinaryExpression using a specific binary operator
        Parameters:
        expr - the evaluated expression
        operator - int value taken from Types
        Returns:
        true if the expression is an instance of BinaryExpression
        Since:
        0.3.0
      • asBinaryExpr

        public <T> T asBinaryExpr​(Expression expr,
                                  T defaultValue,
                                  Closure<T> func)
        Executes a given computation if the expression is of type BinaryExpression if not, then a default value is provided
        Parameters:
        expr - the expression we want to compute over
        defaultValue - a value in case the expression is not of type BinaryExpression
        func - function executed in case expression is not of type BinaryExpression
        Returns:
        the result of the computation or the default value
        Since:
        0.3.0
      • leftExprAs

        public <T> T leftExprAs​(BinaryExpression binaryExpr,
                                Class<T> type)
        From a BinaryExpression gets the left expression as if it were of a given type
        Parameters:
        binaryExpr - the binary expression we want the left expr from
        type - the type we would like to cast the left expr to
        Returns:
        the left expression cast to a given type
        Since:
        0.3.0
      • rightExprAs

        public <T> T rightExprAs​(BinaryExpression binaryExpr,
                                 Class<T> type)
        From a BinaryExpression gets the right expression as if it were of a given type
        Parameters:
        binaryExpr - the binary expression we want the right expr from
        type - the type we would like to cast the right expr to
        Returns:
        the right expression cast to a given type
        Since:
        0.3.0
      • getText

        public String getText​(Expression expression)
        Extracts the String value of a given expression
        Parameters:
        expression - expression to get text from
        Returns:
        a String of a given Expression
        Since:
        0.3.2