Java Code Examples for org.apache.commons.jexl3.introspection.JexlMethod#invoke()

The following examples show how to use org.apache.commons.jexl3.introspection.JexlMethod#invoke() . 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: InterpreterBase.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * Attempt to call close() if supported.
 * <p>This is used when dealing with auto-closeable (duck-like) objects
 * @param closeable the object we'd like to close
 */
protected void closeIfSupported(Object closeable) {
    if (closeable != null) {
        JexlMethod mclose = uberspect.getMethod(closeable, "close", EMPTY_PARAMS);
        if (mclose != null) {
            try {
                mclose.invoke(closeable, EMPTY_PARAMS);
            } catch (Exception xignore) {
                logger.warn(xignore);
            }
        }
    }
}
 
Example 4
Source File: Uberspect.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Iterator<?> getIterator(Object obj) {
    if (obj instanceof Iterator<?>) {
        return ((Iterator<?>) obj);
    }
    if (obj.getClass().isArray()) {
        return new ArrayIterator(obj);
    }
    if (obj instanceof Map<?, ?>) {
        return ((Map<?, ?>) obj).values().iterator();
    }
    if (obj instanceof Enumeration<?>) {
        return new EnumerationIterator<Object>((Enumeration<Object>) obj);
    }
    if (obj instanceof Iterable<?>) {
        return ((Iterable<?>) obj).iterator();
    }
    try {
        // look for an iterator() method to support the JDK5 Iterable
        // interface or any user tools/DTOs that want to work in
        // foreach without implementing the Collection interface
        JexlMethod it = getMethod(obj, "iterator", (Object[]) null);
        if (it != null && Iterator.class.isAssignableFrom(it.getReturnType())) {
            return (Iterator<Object>) it.invoke(obj, (Object[]) null);
        }
    } catch (Exception xany) {
        if (logger != null && logger.isDebugEnabled()) {
            logger.info("unable to solve iterator()", xany);
        }
    }
    return null;
}
 
Example 5
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 6
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 7
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 8
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 9
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;
}
 
Example 10
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;
}
 
Example 11
Source File: DiscoveryTest.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Test
public void testMethodIntrospection() throws Exception {
    Uberspect uber = new Uberspect(null, null);
    Bulgroz bulgroz = new Bulgroz();
    JexlMethod jmethod;
    Object result;
    jmethod = uber.getMethod(bulgroz, "list", 0);
    result = jmethod.invoke(bulgroz, 0);
    Assert.assertEquals(0, result);
    jmethod = uber.getMethod(bulgroz, "list", "1");
    result = jmethod.invoke(bulgroz, "1");
    Assert.assertEquals(1, result);
    jmethod = uber.getMethod(bulgroz, "list", bulgroz);
    result = jmethod.invoke(bulgroz, bulgroz);
    Assert.assertEquals(2, result);
    jmethod = uber.getMethod(bulgroz, "list", 1, bulgroz);
    result = jmethod.invoke(bulgroz, 1, bulgroz);
    Assert.assertEquals(3, result);
    jmethod = uber.getMethod(bulgroz, "list", 1, bulgroz, bulgroz);
    result = jmethod.invoke(bulgroz, 1, bulgroz, bulgroz);
    Assert.assertEquals(3, result);
    jmethod = uber.getMethod(bulgroz, "list", 1, 2);
    result = jmethod.invoke(bulgroz, 1, 2);
    Assert.assertEquals(4, result);
    jmethod = uber.getMethod(bulgroz, "list", "1", bulgroz);
    result = jmethod.invoke(bulgroz, "1", bulgroz);
    Assert.assertEquals(5, result);
    jmethod = uber.getMethod(bulgroz, "list", "1", "2");
    result = jmethod.invoke(bulgroz, "1", "2");
    Assert.assertEquals(6, result);
    jmethod = uber.getMethod(bulgroz, "list", bulgroz, bulgroz);
    result = jmethod.invoke(bulgroz, bulgroz, bulgroz);
    Assert.assertEquals(8, result);
    jmethod = uber.getMethod(bulgroz, "list", bulgroz, 1, bulgroz);
    result = jmethod.invoke(bulgroz, bulgroz, 1, bulgroz);
    Assert.assertEquals(7, result);
    jmethod = uber.getMethod(bulgroz, "list", bulgroz, 1, "1");
    result = jmethod.invoke(bulgroz, bulgroz, 1, "1");
    Assert.assertEquals(7, result);
    jmethod = uber.getMethod(bulgroz, "list", (Object) null);
    result = jmethod.invoke(bulgroz,  (Object) null);
    Assert.assertEquals(2, result);
    jmethod = uber.getMethod(bulgroz, "list", bulgroz, (Object) null);
    result = jmethod.invoke(bulgroz, bulgroz, (Object) null);
    Assert.assertEquals(8, result);
    jmethod = uber.getMethod(bulgroz, "list", null, "1");
    result = jmethod.invoke(bulgroz, null, "1");
    Assert.assertEquals(8, result);
    jmethod = uber.getMethod(bulgroz, "list", bulgroz, null, null);
    result = jmethod.invoke(bulgroz, bulgroz, null, null);
    Assert.assertEquals(7, result);

    jmethod = uber.getMethod(bulgroz, "amb", 3d);
    Assert.assertNotNull(null, jmethod);
}