Java Code Examples for org.apache.commons.jexl3.introspection.JexlUberspect#getMethod()

The following examples show how to use org.apache.commons.jexl3.introspection.JexlUberspect#getMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TemplateInterpreter.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
/**
 * Prints to output.
 * <p>
 * This will dynamically try to find the best suitable method in the writer through uberspection.
 * Subclassing Writer by adding 'print' methods should be the preferred way to specialize output.
 * </p>
 * @param info the source info
 * @param arg  the argument to print out
 */
private void doPrint(JexlInfo info, Object arg) {
    try {
        if (writer != null) {
            if (arg instanceof CharSequence) {
                writer.write(arg.toString());
            } else if (arg != null) {
                Object[] value = {arg};
                JexlUberspect uber = jexl.getUberspect();
                JexlMethod method = uber.getMethod(writer, "print", value);
                if (method != null) {
                    method.invoke(writer, value);
                } else {
                    writer.write(arg.toString());
                }
            }
        }
    } catch (java.io.IOException xio) {
        throw TemplateEngine.createException(info, "call print", null, xio);
    } catch (java.lang.Exception xany) {
        throw TemplateEngine.createException(info, "invoke print", null, xany);
    }
}
 
Example 2
Source File: Operators.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
/**
 * Calculate the <code>size</code> of various types:
 * Collection, Array, Map, String, and anything that has a int size() method.
 * <p>Note that the result may not be an integer.
 *
 * @param node   the node that gave the value to size
 * @param object the object to get the size of
 * @return the evaluation result
 */
protected Object size(JexlNode node, Object object) {
    if (object == null) {
        return 0;
    }
    Object result = tryOverload(node, JexlOperator.SIZE, object);
    if (result != JexlEngine.TRY_FAILED) {
        return result;
    }
    final JexlArithmetic arithmetic = interpreter.arithmetic;
    result = arithmetic.size(object, null);
    if (result == null) {
        final JexlUberspect uberspect = interpreter.uberspect;
        // check if there is a size method on the object that returns an
        // integer and if so, just use it
        JexlMethod vm = uberspect.getMethod(object, "size", Interpreter.EMPTY_PARAMS);
        if (returnsInteger(vm)) {
            try {
                result = vm.invoke(object, Interpreter.EMPTY_PARAMS);
            } catch (Exception xany) {
                interpreter.operatorError(node, JexlOperator.SIZE, xany);
            }
        }
    }
    return result instanceof Number ? ((Number) result).intValue() : 0;
}
 
Example 3
Source File: Operators.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * The 'startsWith' operator implementation.
 * @param node     the node
 * @param operator the calling operator, $= or $!
 * @param left     the left operand
 * @param right    the right operand
 * @return true if left starts with right, false otherwise
 */
protected boolean startsWith(JexlNode node, String operator, Object left, Object right) {
    final JexlArithmetic arithmetic = interpreter.arithmetic;
    final JexlUberspect uberspect = interpreter.uberspect;
    try {
        // try operator overload
        Object result = tryOverload(node, JexlOperator.STARTSWITH, left, right);
        if (result instanceof Boolean) {
            return (Boolean) result;
        }
        // use arithmetic / pattern matching ?
        Boolean matched = arithmetic.startsWith(left, right);
        if (matched != null) {
            return matched;
        }
        // try a startsWith method (duck type)
        try {
            Object[] argv = {right};
            JexlMethod vm = uberspect.getMethod(left, "startsWith", argv);
            if (returnsBoolean(vm)) {
                return (Boolean) vm.invoke(left, argv);
            } else if (arithmetic.narrowArguments(argv)) {
                vm = uberspect.getMethod(left, "startsWith", argv);
                if (returnsBoolean(vm)) {
                    return (Boolean) vm.invoke(left, argv);
                }
            }
        } catch (Exception e) {
            throw new JexlException(node, operator + " error", e);
        }
        // defaults to equal
        return arithmetic.equals(left, right) ? Boolean.TRUE : Boolean.FALSE;
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, operator + " error", xrt);
    }
}
 
Example 4
Source File: Operators.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * The 'endsWith' operator implementation.
 * @param node     the node
 * @param operator the calling operator, ^= or ^!
 * @param left     the left operand
 * @param right    the right operand
 * @return true if left ends with right, false otherwise
 */
protected boolean endsWith(JexlNode node, String operator, Object left, Object right) {
    final JexlArithmetic arithmetic = interpreter.arithmetic;
    final JexlUberspect uberspect = interpreter.uberspect;
    try {
        // try operator overload
        Object result = tryOverload(node, JexlOperator.ENDSWITH, left, right);
        if (result instanceof Boolean) {
            return (Boolean) result;
        }
        // use arithmetic / pattern matching ?
        Boolean matched = arithmetic.endsWith(left, right);
        if (matched != null) {
            return matched;
        }
        // try a endsWith method (duck type)
        try {
            Object[] argv = {right};
            JexlMethod vm = uberspect.getMethod(left, "endsWith", argv);
            if (returnsBoolean(vm)) {
                return (Boolean) vm.invoke(left, argv);
            } else if (arithmetic.narrowArguments(argv)) {
                vm = uberspect.getMethod(left, "endsWith", argv);
                if (returnsBoolean(vm)) {
                    return (Boolean) vm.invoke(left, argv);
                }
            }
        } catch (Exception e) {
            throw new JexlException(node, operator + " error", e);
        }
        // defaults to equal
        return arithmetic.equals(left, right) ? Boolean.TRUE : Boolean.FALSE;
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, operator + " error", xrt);
    }
}
 
Example 5
Source File: Operators.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * The 'match'/'in' operator implementation.
 * <p>
 * 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.
 * </p>
 * @param node  the node
 * @param op    the calling operator, =~ or !~
 * @param right the left operand
 * @param left  the right operand
 * @return true if left matches right, false otherwise
 */
protected boolean contains(JexlNode node, String op, Object left, Object right) {
    final JexlArithmetic arithmetic = interpreter.arithmetic;
    final JexlUberspect uberspect = interpreter.uberspect;
    try {
        // try operator overload
        Object result = tryOverload(node, JexlOperator.CONTAINS, left, right);
        if (result instanceof Boolean) {
            return (Boolean) result;
        }
        // use arithmetic / pattern matching ?
        Boolean matched = arithmetic.contains(left, right);
        if (matched != null) {
            return matched;
        }
        // try a contains method (duck type set)
        try {
            Object[] argv = {right};
            JexlMethod vm = uberspect.getMethod(left, "contains", argv);
            if (returnsBoolean(vm)) {
                return (Boolean) vm.invoke(left, argv);
            } else if (arithmetic.narrowArguments(argv)) {
                vm = uberspect.getMethod(left, "contains", argv);
                if (returnsBoolean(vm)) {
                    return (Boolean) vm.invoke(left, argv);
                }
            }
        } catch (Exception e) {
            throw new JexlException(node, op + " error", e);
        }
        // defaults to equal
        return arithmetic.equals(left, right);
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, op + " error", xrt);
    }
}
 
Example 6
Source File: Operators.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * Check for emptyness of various types: Collection, Array, Map, String, and anything that has a boolean isEmpty()
 * method.
 * <p>Note that the result may not be a boolean.
 *
 * @param node   the node holding the object
 * @param object the object to check the emptyness of
 * @return the evaluation result
 */
protected Object empty(JexlNode node, Object object) {
    if (object == null) {
        return true;
    }
    Object result = tryOverload(node, JexlOperator.EMPTY, object);
    if (result != JexlEngine.TRY_FAILED) {
        return result;
    }
    final JexlArithmetic arithmetic = interpreter.arithmetic;
    result = arithmetic.isEmpty(object, null);
    if (result == null) {
        final JexlUberspect uberspect = interpreter.uberspect;
        result = false;
        // check if there is an isEmpty method on the object that returns a
        // boolean and if so, just use it
        JexlMethod vm = uberspect.getMethod(object, "isEmpty", Interpreter.EMPTY_PARAMS);
        if (returnsBoolean(vm)) {
            try {
                result = vm.invoke(object, Interpreter.EMPTY_PARAMS);
            } catch (Exception xany) {
                interpreter.operatorError(node, JexlOperator.EMPTY, xany);
            }
        }
    }
    return result instanceof Boolean ? (Boolean) result : true;
}