org.apache.commons.jexl3.JexlEngine Java Examples
The following examples show how to use
org.apache.commons.jexl3.JexlEngine.
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 |
@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 |
/** * 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 |
@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: JexlRowFactory.java From timbuctoo with GNU General Public License v3.0 | 6 votes |
public JexlRowFactory(Map<String, String> customFields, JoinHandler joinHandler) { this.joinHandler = joinHandler; this.expressions = new HashMap<>(); Map<String, Object> ns = Maps.newHashMap(); ns.put("Json", JsonEncoder.class); // make method Json:stringify available in expressions ns.put("Math", Math.class); // make all methods of Math available ns.put("Integer", Integer.class); // make method Integer JexlEngine jexl = new JexlBuilder().namespaces(ns).create(); StringBuilder result = new StringBuilder(); customFields.forEach((key, value) -> { try { expressions.put(key, jexl.createExpression(value)); result.append(" ").append(key).append(": ").append(value).append("\n"); } catch (Exception e) { // Catch the runtime exceptions LOG.error("Could not compile expression '{}'", value); LOG.error("Exception thrown", e); } }); this.stringRepresentation = result.toString(); }
Example #5
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testCtorBlock() throws Exception { String expr = "new('" + Foo.class.getName() + "', '42')"; JexlScript script = JEXL.createScript(expr); Object result; result = script.execute(null); Assert.assertEquals("42", ((Foo) result).getName()); JexlSandbox sandbox = new JexlSandbox(); sandbox.block(Foo.class.getName()).execute(""); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); script = sjexl.createScript(expr); try { result = script.execute(null); Assert.fail("ctor should not be accessible"); } catch (JexlException.Method xmethod) { // ok, ctor should not have been accessible LOGGER.info(xmethod.toString()); } }
Example #6
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testMethodBlock() throws Exception { String expr = "foo.Quux()"; JexlScript script = JEXL.createScript(expr, "foo"); Foo foo = new Foo("42"); Object result; result = script.execute(null, foo); Assert.assertEquals(foo.Quux(), result); JexlSandbox sandbox = new JexlSandbox(); sandbox.block(Foo.class.getName()).execute("Quux"); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); script = sjexl.createScript(expr, "foo"); try { result = script.execute(null, foo); Assert.fail("Quux should not be accessible"); } catch (JexlException.Method xmethod) { // ok, Quux should not have been accessible LOGGER.info(xmethod.toString()); } }
Example #7
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testGetBlock() throws Exception { String expr = "foo.alias"; JexlScript script = JEXL.createScript(expr, "foo"); Foo foo = new Foo("42"); Object result; result = script.execute(null, foo); Assert.assertEquals(foo.alias, result); JexlSandbox sandbox = new JexlSandbox(); sandbox.block(Foo.class.getName()).read("alias"); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); script = sjexl.createScript(expr, "foo"); try { result = script.execute(null, foo); Assert.fail("alias should not be accessible"); } catch (JexlException.Property xvar) { // ok, alias should not have been accessible LOGGER.info(xvar.toString()); } }
Example #8
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testSetBlock() throws Exception { String expr = "foo.alias = $0"; JexlScript script = JEXL.createScript(expr, "foo", "$0"); Foo foo = new Foo("42"); Object result; result = script.execute(null, foo, "43"); Assert.assertEquals("43", result); JexlSandbox sandbox = new JexlSandbox(); sandbox.block(Foo.class.getName()).write("alias"); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); script = sjexl.createScript(expr, "foo", "$0"); try { result = script.execute(null, foo, "43"); Assert.fail("alias should not be accessible"); } catch (JexlException.Property xvar) { // ok, alias should not have been accessible LOGGER.info(xvar.toString()); } }
Example #9
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testCantSeeMe() throws Exception { JexlContext jc = new MapContext(); String expr = "foo.doIt()"; JexlScript script; Object result = null; JexlSandbox sandbox = new JexlSandbox(false); sandbox.allow(Foo.class.getName()); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); jc.set("foo", new CantSeeMe()); script = sjexl.createScript(expr); try { result = script.execute(jc); Assert.fail("should have failed, doIt()"); } catch (JexlException xany) { // } jc.set("foo", new Foo("42")); result = script.execute(jc); Assert.assertEquals(42, ((Integer) result).intValue()); }
Example #10
Source File: JexlUtils.java From syncope with Apache License 2.0 | 6 votes |
private static JexlEngine getEngine() { synchronized (LOG) { if (JEXL_ENGINE == null) { JEXL_ENGINE = new JexlBuilder(). uberspect(new SandboxUberspect()). loader(new EmptyClassLoader()). namespaces(Map.of("syncope", new SyncopeJexlFunctions())). cache(512). silent(false). strict(false). create(); } } return JEXL_ENGINE; }
Example #11
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testMethodNoJexl() throws Exception { Foo foo = new Foo("42"); String[] exprs = { "foo.cantCallMe()", "foo.tryMe()", "foo.tryMeARiver()", "foo.callMeNot()", "foo.NONO", "new('org.apache.commons.jexl3.SandboxTest$Foo', 'one', 'two')" }; JexlScript script; Object result; JexlEngine sjexl = new JexlBuilder().strict(true).safe(false).create(); for (String expr : exprs) { script = sjexl.createScript(expr, "foo"); try { result = script.execute(null, foo); Assert.fail("should have not been possible"); } catch (JexlException.Method | JexlException.Property xjm) { // ok LOGGER.info(xjm.toString()); } } }
Example #12
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testGetAllow() throws Exception { Foo foo = new Foo("42"); String expr = "foo.alias"; JexlScript script; Object result; JexlSandbox sandbox = new JexlSandbox(); sandbox.allow(Foo.class.getName()).read("alias"); sandbox.get(Foo.class.getName()).read().alias("alias", "ALIAS"); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).safe(false).strict(true).create(); script = sjexl.createScript(expr, "foo"); result = script.execute(null, foo); Assert.assertEquals(foo.alias, result); script = sjexl.createScript("foo.ALIAS", "foo"); result = script.execute(null, foo); Assert.assertEquals(foo.alias, result); }
Example #13
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testSetAllow() throws Exception { Foo foo = new Foo("42"); String expr = "foo.alias = $0"; JexlScript script; Object result; JexlSandbox sandbox = new JexlSandbox(); sandbox.allow(Foo.class.getName()).write("alias"); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).safe(false).strict(true).create(); script = sjexl.createScript(expr, "foo", "$0"); result = script.execute(null, foo, "43"); Assert.assertEquals("43", result); Assert.assertEquals("43", foo.alias); }
Example #14
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testSandboxInherit0() throws Exception { Object result; JexlContext ctxt = null; List<String> foo = new ArrayList<String>(); JexlSandbox sandbox = new JexlSandbox(false, true); sandbox.allow(java.util.List.class.getName()); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).safe(false).strict(true).create(); JexlScript method = sjexl.createScript("foo.add(y)", "foo", "y"); JexlScript set = sjexl.createScript("foo[x] = y", "foo", "x", "y"); JexlScript get = sjexl.createScript("foo[x]", "foo", "x"); result = method.execute(ctxt, foo, "nothing"); Assert.assertEquals(true, result); result = null; result = get.execute(null, foo, 0); Assert.assertEquals("nothing", result); result = null; result = set.execute(null, foo, 0, "42"); Assert.assertEquals("42", result); result = null; result = get.execute(null, foo, 0); Assert.assertEquals("42", result); }
Example #15
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 6 votes |
@Test public void testSandboxInherit1() throws Exception { Object result; JexlContext ctxt = null; Operation2 foo = new Operation2(12); JexlSandbox sandbox = new JexlSandbox(false, true); sandbox.allow(Operation.class.getName()); sandbox.block(Operation.class.getName()).execute("nonCallable"); //sandbox.block(Foo.class.getName()).execute(); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).safe(false).strict(true).create(); JexlScript someOp = sjexl.createScript("foo.someOp(y)", "foo", "y"); result = someOp.execute(ctxt, foo, 30); Assert.assertEquals(42, result); JexlScript nonCallable = sjexl.createScript("foo.nonCallable(y)", "foo", "y"); try { result = nonCallable.execute(null, foo, 0); Assert.fail("should not be possible"); } catch (JexlException xjm) { // ok LOGGER.info(xjm.toString()); } }
Example #16
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 5 votes |
@Test public void testNoJexl312() throws Exception { JexlContext ctxt = new MapContext(); JexlEngine sjexl = new JexlBuilder().safe(false).strict(true).create(); JexlScript foo = sjexl.createScript("x.getFoo()", "x"); try { foo.execute(ctxt, new Foo44()); Assert.fail("should have thrown"); } catch (JexlException xany) { Assert.assertNotNull(xany); } }
Example #17
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 5 votes |
@Test public void testMethodAllow() throws Exception { Foo foo = new Foo("42"); String expr = "foo.Quux()"; JexlScript script; Object result; JexlSandbox sandbox = new JexlSandbox(); sandbox.allow(Foo.class.getName()).execute("Quux"); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); script = sjexl.createScript(expr, "foo"); result = script.execute(null, foo); Assert.assertEquals(foo.Quux(), result); }
Example #18
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 #19
Source File: SandboxTest.java From commons-jexl with Apache License 2.0 | 5 votes |
@Test public void testCtorAllow() throws Exception { String expr = "new('" + Foo.class.getName() + "', '42')"; JexlScript script; Object result; JexlSandbox sandbox = new JexlSandbox(); sandbox.allow(Foo.class.getName()).execute(""); JexlEngine sjexl = new JexlBuilder().sandbox(sandbox).strict(true).safe(false).create(); script = sjexl.createScript(expr); result = script.execute(null); Assert.assertEquals("42", ((Foo) result).getName()); }
Example #20
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 #21
Source File: InterpreterBase.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * 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 #22
Source File: Interpreter.java From commons-jexl with Apache License 2.0 | 5 votes |
@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 #23
Source File: Interpreter.java From commons-jexl with Apache License 2.0 | 5 votes |
@Override protected Object visit(ASTNENode 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 ? arithmetic.toBoolean(result) ? Boolean.FALSE : Boolean.TRUE : arithmetic.equals(left, right) ? Boolean.FALSE : Boolean.TRUE; } catch (ArithmeticException xrt) { JexlNode xnode = findNullOperand(xrt, node, left, right); throw new JexlException(xnode, "!= error", xrt); } }
Example #24
Source File: ArrayTest.java From commons-jexl with Apache License 2.0 | 5 votes |
/** * An example for array access. */ static void example(Output out) throws Exception { /* * First step is to retrieve an instance of a JexlEngine; * it might be already existing and shared or created anew. */ JexlEngine jexl = new JexlBuilder().create(); /* * Second make a jexlContext and put stuff in it */ JexlContext jc = new MapContext(); List<Object> l = new ArrayList<Object>(); l.add("Hello from location 0"); Integer two = 2; l.add(two); jc.set("array", l); JexlExpression e = jexl.createExpression("array[1]"); Object o = e.evaluate(jc); out.print("Object @ location 1 = ", o, two); e = jexl.createExpression("array[0].length()"); o = e.evaluate(jc); out.print("The length of the string at location 0 is : ", o, 21); }
Example #25
Source File: Interpreter.java From commons-jexl with Apache License 2.0 | 5 votes |
@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 #26
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 #27
Source File: Interpreter.java From commons-jexl with Apache License 2.0 | 5 votes |
@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 #28
Source File: Interpreter.java From commons-jexl with Apache License 2.0 | 5 votes |
@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 #29
Source File: Interpreter.java From commons-jexl with Apache License 2.0 | 5 votes |
@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 #30
Source File: Interpreter.java From commons-jexl with Apache License 2.0 | 5 votes |
@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); } }