Java Code Examples for org.apache.commons.jexl3.JexlEngine#TRY_FAILED

The following examples show how to use org.apache.commons.jexl3.JexlEngine#TRY_FAILED . 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: MethodExecutor.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
@Override
public Object tryInvoke(String name, Object obj, Object... args) {
    MethodKey tkey = new MethodKey(name, args);
    // let's assume that invocation will fly if the declaring class is the
    // same and arguments have the same type
    if (objectClass.equals(obj.getClass()) && tkey.equals(key)) {
        try {
            return invoke(obj, args);
        } catch (IllegalAccessException | IllegalArgumentException xill) {
            return TRY_FAILED;// fail
        } catch (InvocationTargetException xinvoke) {
            throw JexlException.tryFailed(xinvoke); // throw
        }
    }
    return JexlEngine.TRY_FAILED;
}
 
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: Interpreter.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
@Override
protected Object visit(ASTUnaryPlusNode node, Object data) {
    // use cached value if literal
    Object value = node.jjtGetValue();
    if (value != null && !(value instanceof JexlMethod)) {
        return value;
    }
    JexlNode valNode = node.jjtGetChild(0);
    Object val = valNode.jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.POSITIVIZE, val);
        if (result != JexlEngine.TRY_FAILED) {
            return result;
        }
        Object number = arithmetic.positivize(val);
        if (valNode instanceof ASTNumberLiteral
            && number instanceof Number
            && arithmetic.isPositivizeStable()) {
            node.jjtSetValue(number);
        }
        return number;
    } catch (ArithmeticException xrt) {
        throw new JexlException(valNode, "- error", xrt);
    }
}
 
Example 4
Source File: InterpreterBase.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to reuse last funcall cached in volatile JexlNode.value (if
 * it was cacheable).
 *
 * @param ntarget the target instance
 * @param mname the method name
 * @param arguments the method arguments
 * @return TRY_FAILED if invocation was not possible or failed, the
 * result otherwise
 */
protected Object tryEval(final Object ntarget, final String mname, final Object[] arguments) {
    // do we have  a method/function name ?
    // attempt to reuse last funcall cached in volatile JexlNode.value (if it was not a variable)
    if (mname != null && cacheable && ntarget != null) {
        Object cached = node.jjtGetValue();
        if (cached instanceof Funcall) {
            return ((Funcall) cached).tryInvoke(InterpreterBase.this, mname, ntarget, arguments);
        }
    }
    return JexlEngine.TRY_FAILED;
}
 
Example 5
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTAddNode node, Object data) {
    Object left = node.jjtGetChild(0).jjtAccept(this, data);
    Object right = node.jjtGetChild(1).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.ADD, left, right);
        return result != JexlEngine.TRY_FAILED ? result : arithmetic.add(left, right);
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, "+ error", xrt);
    }
}
 
Example 6
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTNotNode node, Object data) {
    Object val = node.jjtGetChild(0).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.NOT, val);
        return result != JexlEngine.TRY_FAILED ? result : arithmetic.not(val);
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, "! error", xrt);
    }
}
 
Example 7
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTBitwiseComplNode node, Object data) {
    Object arg = node.jjtGetChild(0).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.COMPLEMENT, arg);
        return result != JexlEngine.TRY_FAILED ? result : arithmetic.complement(arg);
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, "~ error", xrt);
    }
}
 
Example 8
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTUnaryMinusNode node, Object data) {
    // use cached value if literal
    Object value = node.jjtGetValue();
    if (value != null && !(value instanceof JexlMethod)) {
        return value;
    }
    JexlNode valNode = node.jjtGetChild(0);
    Object val = valNode.jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.NEGATE, val);
        if (result != JexlEngine.TRY_FAILED) {
            return result;
        }
        Object number = arithmetic.negate(val);
        // attempt to recoerce to literal class
        if ((number instanceof Number)) {
            // cache if number literal and negate is idempotent
            if (valNode instanceof ASTNumberLiteral) {
                number = arithmetic.narrowNumber((Number) number, ((ASTNumberLiteral) valNode).getLiteralClass());
                if (arithmetic.isNegateStable()) {
                    node.jjtSetValue(number);
                }
            }
        }
        return number;
    } catch (ArithmeticException xrt) {
        throw new JexlException(valNode, "- error", xrt);
    }
}
 
Example 9
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTLTNode node, Object data) {
    Object left = node.jjtGetChild(0).jjtAccept(this, data);
    Object right = node.jjtGetChild(1).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.LT, left, right);
        return result != JexlEngine.TRY_FAILED
               ? result
               : arithmetic.lessThan(left, right) ? Boolean.TRUE : Boolean.FALSE;
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, "< error", xrt);
    }
}
 
Example 10
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTLENode node, Object data) {
    Object left = node.jjtGetChild(0).jjtAccept(this, data);
    Object right = node.jjtGetChild(1).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.LTE, left, right);
        return result != JexlEngine.TRY_FAILED
               ? result
               : arithmetic.lessThanOrEqual(left, right) ? Boolean.TRUE : Boolean.FALSE;
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, "<= error", xrt);
    }
}
 
Example 11
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTGTNode node, Object data) {
    Object left = node.jjtGetChild(0).jjtAccept(this, data);
    Object right = node.jjtGetChild(1).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.GT, left, right);
        return result != JexlEngine.TRY_FAILED
               ? result
               : arithmetic.greaterThan(left, right) ? Boolean.TRUE : Boolean.FALSE;
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, "> error", xrt);
    }
}
 
Example 12
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTGENode node, Object data) {
    Object left = node.jjtGetChild(0).jjtAccept(this, data);
    Object right = node.jjtGetChild(1).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.GTE, left, right);
        return result != JexlEngine.TRY_FAILED
               ? result
               : arithmetic.greaterThanOrEqual(left, right) ? Boolean.TRUE : Boolean.FALSE;
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, ">= error", xrt);
    }
}
 
Example 13
Source File: Operators.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to call an operator.
 * <p>
 * This takes care of finding and caching the operator method when appropriate
 * @param node     the syntactic node
 * @param operator the operator
 * @param args     the arguments
 * @return the result of the operator evaluation or TRY_FAILED
 */
protected Object tryOverload(JexlNode node, JexlOperator operator, Object... args) {
    if (operators != null && operators.overloads(operator)) {
        final JexlArithmetic arithmetic = interpreter.arithmetic;
        final boolean cache = interpreter.cache;
        try {
            if (cache) {
                Object cached = node.jjtGetValue();
                if (cached instanceof JexlMethod) {
                    JexlMethod me = (JexlMethod) cached;
                    Object eval = me.tryInvoke(operator.getMethodName(), arithmetic, args);
                    if (!me.tryFailed(eval)) {
                        return eval;
                    }
                }
            }
            JexlMethod vm = operators.getOperator(operator, args);
            if (vm != null && !isArithmetic(vm)) {
                Object result = vm.invoke(arithmetic, args);
                if (cache) {
                    node.jjtSetValue(vm);
                }
                return result;
            }
        } catch (Exception xany) {
            return interpreter.operatorError(node, operator, xany);
        }
    }
    return JexlEngine.TRY_FAILED;
}
 
Example 14
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTEQNode node, Object data) {
    Object left = node.jjtGetChild(0).jjtAccept(this, data);
    Object right = node.jjtGetChild(1).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.EQ, left, right);
        return result != JexlEngine.TRY_FAILED
               ? result
               : arithmetic.equals(left, right) ? Boolean.TRUE : Boolean.FALSE;
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, "== error", xrt);
    }
}
 
Example 15
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTBitwiseXorNode node, Object data) {
    Object left = node.jjtGetChild(0).jjtAccept(this, data);
    Object right = node.jjtGetChild(1).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.XOR, left, right);
        return result != JexlEngine.TRY_FAILED ? result : arithmetic.xor(left, right);
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, "^ error", xrt);
    }
}
 
Example 16
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTBitwiseOrNode node, Object data) {
    Object left = node.jjtGetChild(0).jjtAccept(this, data);
    Object right = node.jjtGetChild(1).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.OR, left, right);
        return result != JexlEngine.TRY_FAILED ? result : arithmetic.or(left, right);
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, "| error", xrt);
    }
}
 
Example 17
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTBitwiseAndNode node, Object data) {
    Object left = node.jjtGetChild(0).jjtAccept(this, data);
    Object right = node.jjtGetChild(1).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.AND, left, right);
        return result != JexlEngine.TRY_FAILED ? result : arithmetic.and(left, right);
    } catch (ArithmeticException xrt) {
        throw new JexlException(node, "& error", xrt);
    }
}
 
Example 18
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTDivNode node, Object data) {
    Object left = node.jjtGetChild(0).jjtAccept(this, data);
    Object right = node.jjtGetChild(1).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.DIVIDE, left, right);
        return result != JexlEngine.TRY_FAILED ? result : arithmetic.divide(left, right);
    } catch (ArithmeticException xrt) {
        if (!arithmetic.isStrict()) {
            return 0.0d;
        }
        JexlNode xnode = findNullOperand(xrt, node, left, right);
        throw new JexlException(xnode, "/ error", xrt);
    }
}
 
Example 19
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
protected Object visit(ASTMulNode node, Object data) {
    Object left = node.jjtGetChild(0).jjtAccept(this, data);
    Object right = node.jjtGetChild(1).jjtAccept(this, data);
    try {
        Object result = operators.tryOverload(node, JexlOperator.MULTIPLY, left, right);
        return result != JexlEngine.TRY_FAILED ? result : arithmetic.multiply(left, right);
    } catch (ArithmeticException xrt) {
        JexlNode xnode = findNullOperand(xrt, node, left, right);
        throw new JexlException(xnode, "* error", xrt);
    }
}
 
Example 20
Source File: InterpreterBase.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves a namespace, eventually allocating an instance using context as constructor argument.
 * <p>
 * The lifetime of such instances span the current expression or script evaluation.</p>
 * @param prefix the prefix name (may be null for global namespace)
 * @param node   the AST node
 * @return the namespace instance
 */
protected Object resolveNamespace(String prefix, JexlNode node) {
    Object namespace;
    // check whether this namespace is a functor
    synchronized (this) {
        if (functors != null) {
            namespace = functors.get(prefix);
            if (namespace != null) {
                return namespace;
            }
        }
    }
    // check if namespace is a resolver
    namespace = ns.resolveNamespace(prefix);
    if (namespace == null) {
        namespace = functions.get(prefix);
        if (prefix != null && namespace == null) {
            throw new JexlException(node, "no such function namespace " + prefix, null);
        }
    }
    // shortcut if ns is known to be not-a-functor
    final boolean cacheable = cache;
    Object cached = cacheable ? node.jjtGetValue() : null;
    if (cached != JexlContext.NamespaceFunctor.class) {
        // allow namespace to instantiate a functor with context if possible, not an error otherwise
        Object functor = null;
        if (namespace instanceof JexlContext.NamespaceFunctor) {
            functor = ((JexlContext.NamespaceFunctor) namespace).createFunctor(context);
        } else if (namespace instanceof Class<?> || namespace instanceof String) {
            // attempt to reuse last ctor cached in volatile JexlNode.value
            if (cached instanceof JexlMethod) {
                try {
                    Object eval = ((JexlMethod) cached).tryInvoke(null, context);
                    if (JexlEngine.TRY_FAILED != eval) {
                        functor = eval;
                    }
                } catch (JexlException.TryFailed xtry) {
                    throw new JexlException(node, "unable to instantiate namespace " + prefix, xtry.getCause());
                }
            }
            if (functor == null) {
                JexlMethod ctor = uberspect.getConstructor(namespace, context);
                if (ctor != null) {
                    try {
                        functor = ctor.invoke(namespace, context);
                        if (cacheable && ctor.isCacheable()) {
                            node.jjtSetValue(ctor);
                        }
                    } catch (Exception xinst) {
                        throw new JexlException(node, "unable to instantiate namespace " + prefix, xinst);
                    }
                }
            }
        }
        // got a functor, store it and return it
        if (functor != null) {
            synchronized (this) {
                if (functors == null) {
                    functors = new HashMap<String, Object>();
                }
                functors.put(prefix, functor);
            }
            return functor;
        } else {
            // use the NamespaceFunctor class to tag this node as not-a-functor
            node.jjtSetValue(JexlContext.NamespaceFunctor.class);
        }
    }
    return namespace;
}