org.apache.commons.jexl3.introspection.JexlMethod Java Examples
The following examples show how to use
org.apache.commons.jexl3.introspection.JexlMethod.
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: Operators.java From commons-jexl with Apache License 2.0 | 6 votes |
/** * 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 #2
Source File: TemplateInterpreter.java From commons-jexl with Apache License 2.0 | 6 votes |
/** * 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 #3
Source File: Interpreter.java From commons-jexl with Apache License 2.0 | 6 votes |
@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: Operators.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * 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 #5
Source File: SandboxUberspect.java From commons-jexl with Apache License 2.0 | 5 votes |
@Override public JexlMethod getMethod(final Object obj, final String method, final Object... args) { if (obj != null && method != null) { Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass(); String actual = sandbox.execute(clazz, method); if (actual != null) { return uberspect.getMethod(obj, actual, args); } } return null; }
Example #6
Source File: SandboxUberspect.java From commons-jexl with Apache License 2.0 | 5 votes |
@Override public JexlMethod getConstructor(final Object ctorHandle, final Object... args) { final String className; if (ctorHandle instanceof Class<?>) { className = sandbox.execute((Class<?>) ctorHandle, ""); } else if (ctorHandle != null) { className = sandbox.execute(ctorHandle.toString(), ""); } else { className = null; } return className != null? uberspect.getConstructor(className, args) : null; }
Example #7
Source File: Operators.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * Checks whether a method returns a boolean or a Boolean. * @param vm the JexlMethod (may be null) * @return true of false */ private boolean returnsBoolean(JexlMethod vm) { if (vm !=null) { Class<?> rc = vm.getReturnType(); return Boolean.TYPE.equals(rc) || Boolean.class.equals(rc); } return false; }
Example #8
Source File: Operators.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * Checks whether a method returns an int or an Integer. * @param vm the JexlMethod (may be null) * @return true of false */ private boolean returnsInteger(JexlMethod vm) { if (vm !=null) { Class<?> rc = vm.getReturnType(); return Integer.TYPE.equals(rc) || Integer.class.equals(rc); } return false; }
Example #9
Source File: Uberspect.java From commons-jexl with Apache License 2.0 | 5 votes |
@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 #10
Source File: Operators.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * Checks whether a method is a JexlArithmetic method. * @param vm the JexlMethod (may be null) * @return true of false */ private boolean isArithmetic(JexlMethod vm) { if (vm instanceof MethodExecutor) { Method method = ((MethodExecutor) vm).getMethod(); return JexlArithmetic.class.equals(method.getDeclaringClass()); } return false; }
Example #11
Source File: Operators.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * 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 #12
Source File: Operators.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * 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 #13
Source File: Operators.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * 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 #14
Source File: Operators.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * 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 #15
Source File: InterpreterBase.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * 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 #16
Source File: Interpreter.java From commons-jexl with Apache License 2.0 | 5 votes |
@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 #17
Source File: JexlNode.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * Clears any cached value of type JexlProperty{G,S}et or JexlMethod. * <p> * This is called when the engine detects the evaluation of a script occurs with a class loader * different that the one that created it.</p> */ public void clearCache() { final Object value = jjtGetValue(); if (value instanceof JexlPropertyGet || value instanceof JexlPropertySet || value instanceof JexlMethod || value instanceof Funcall ) { jjtSetValue(null); } for (int n = 0; n < jjtGetNumChildren(); ++n) { jjtGetChild(n).clearCache(); } }
Example #18
Source File: SandboxUberspect.java From syncope with Apache License 2.0 | 5 votes |
@Override public JexlMethod getMethod(final Object obj, final String method, final Object... args) { if (obj instanceof AnyTO || obj instanceof Any || obj instanceof PlainAttr || obj instanceof Attr || obj instanceof MembershipTO || obj instanceof Membership || obj instanceof Realm || obj instanceof RealmTO) { return super.getMethod(obj, method, args); } else if (obj instanceof SyncopeJexlFunctions) { return super.getMethod(obj, method, args); } else if (obj instanceof Optional) { return super.getMethod(obj, method, args); } else if (obj.getClass().isArray()) { return super.getMethod(obj, method, args); } else if (obj instanceof String) { return super.getMethod(obj, method, args); } else if (obj instanceof Date || obj instanceof Instant) { return super.getMethod(obj, method, args); } else if (obj instanceof Map && MAP_METHODS.contains(method)) { return super.getMethod(obj, method, args); } else if (obj instanceof List && (LIST_METHODS.contains(method) || COLLECTION_METHODS.contains(method))) { return super.getMethod(obj, method, args); } else if (obj instanceof Collection && COLLECTION_METHODS.contains(method)) { return super.getMethod(obj, method, args); } return null; }
Example #19
Source File: SandboxJexlUberspect.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
@Override public JexlMethod getMethod(final Object obj, final String method, final Object... args) { if (obj instanceof String && STRING_METHODS.contains(method)) { return super.getMethod(obj, method, args); } else if (obj instanceof Map && MAP_METHODS.contains(method)) { return super.getMethod(obj, method, args); } else if (obj instanceof Collection && COLLECTION_METHODS.contains(method)) { return super.getMethod(obj, method, args); } return null; }
Example #20
Source File: DiscoveryTest.java From commons-jexl with Apache License 2.0 | 4 votes |
@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); }
Example #21
Source File: SandboxJexlUberspect.java From nexus-public with Eclipse Public License 1.0 | 4 votes |
@Override public JexlMethod getConstructor(final Object ctorHandle, final Object... args) { return null; }
Example #22
Source File: Uberspect.java From commons-jexl with Apache License 2.0 | 4 votes |
@Override public JexlMethod getOperator(JexlOperator operator, Object... args) { return overloads.contains(operator) && args != null ? getMethod(arithmetic, operator.getMethodName(), args) : null; }
Example #23
Source File: Uberspect.java From commons-jexl with Apache License 2.0 | 4 votes |
@Override public JexlMethod getConstructor(Object ctorHandle, Object... args) { return ConstructorMethod.discover(base(), ctorHandle, args); }
Example #24
Source File: Uberspect.java From commons-jexl with Apache License 2.0 | 4 votes |
@Override public JexlMethod getMethod(Object obj, String method, Object... args) { return MethodExecutor.discover(base(), obj, method, args); }
Example #25
Source File: InterpreterBase.java From commons-jexl with Apache License 2.0 | 4 votes |
/** * 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 #26
Source File: SandboxUberspect.java From syncope with Apache License 2.0 | 4 votes |
@Override public JexlMethod getConstructor(final Object ctorHandle, final Object... args) { return null; }
Example #27
Source File: InterpreterBase.java From commons-jexl with Apache License 2.0 | 2 votes |
/** * Constructor. * @param jme the method * @param flag the narrow flag */ protected ContextualCtor(JexlMethod jme, boolean flag) { super(jme, flag); }
Example #28
Source File: InterpreterBase.java From commons-jexl with Apache License 2.0 | 2 votes |
/** * Constructor. * @param jme the method * @param flag the narrow flag */ protected ContextFuncall(JexlMethod jme, boolean flag) { super(jme, flag); }
Example #29
Source File: InterpreterBase.java From commons-jexl with Apache License 2.0 | 2 votes |
/** * Constructor. * @param jme the method * @param flag the narrow flag */ protected ArithmeticFuncall(JexlMethod jme, boolean flag) { super(jme, flag); }
Example #30
Source File: InterpreterBase.java From commons-jexl with Apache License 2.0 | 2 votes |
/** * Constructor. * @param jme the method * @param flag the narrow flag */ protected Funcall(JexlMethod jme, boolean flag) { this.me = jme; this.narrow = flag; }