Search in sources :

Example 16 with JexlMethod

use of org.apache.commons.jexl3.introspection.JexlMethod in project commons-jexl by apache.

the class ReferenceUberspect method getMethod.

@Override
public JexlMethod getMethod(Object ref, String method, Object... args) {
    // is this is a reference of some kind?
    ReferenceHandler handler = discoverHandler(ref);
    if (handler == null) {
        return uberspect.getMethod(ref, method, args);
    }
    // do we have an object referenced ?
    Object obj = handler.callGet(ref);
    if (ref == obj) {
        return null;
    }
    JexlMethod jexlMethod = null;
    if (obj != null) {
        jexlMethod = uberspect.getMethod(obj, method, args);
        if (jexlMethod == null) {
            throw new JexlException.Method(null, method, args, null);
        }
    } else {
        jexlMethod = new OptionalNullMethod(uberspect, method);
    }
    return new ReferenceMethodExecutor(handler, jexlMethod);
}
Also used : JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod) JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod)

Example 17 with JexlMethod

use of org.apache.commons.jexl3.introspection.JexlMethod in project commons-jexl by apache.

the class DiscoveryTest method testMethodIntrospection.

@Test
public void testMethodIntrospection() throws Exception {
    final Uberspect uber = new Uberspect(null, null);
    final 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);
}
Also used : JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod) Test(org.junit.Test)

Example 18 with JexlMethod

use of org.apache.commons.jexl3.introspection.JexlMethod in project commons-jexl by apache.

the class MethodTest method testScriptCall.

@Test
public void testScriptCall() throws Exception {
    JexlContext context = new MapContext();
    final JexlScript plus = JEXL.createScript("a + b", new String[] { "a", "b" });
    context.set("plus", plus);
    JexlScript forty2 = JEXL.createScript("plus(4, 2) * plus(4, 3)");
    Object o = forty2.execute(context);
    Assert.assertEquals("Result is not 42", new Integer(42), o);
    final Map<String, Object> foo = new HashMap<String, Object>();
    foo.put("plus", plus);
    context.set("foo", foo);
    forty2 = JEXL.createScript("foo.plus(4, 2) * foo.plus(4, 3)");
    o = forty2.execute(context);
    Assert.assertEquals("Result is not 42", new Integer(42), o);
    context = new ScriptContext(foo);
    forty2 = JEXL.createScript("script:plus(4, 2) * script:plus(4, 3)");
    o = forty2.execute(context);
    Assert.assertEquals("Result is not 42", new Integer(42), o);
    final JexlArithmetic ja = JEXL.getArithmetic();
    final JexlMethod mplus = new JexlMethod() {

        @Override
        public Object invoke(final Object obj, final Object... params) throws Exception {
            if (obj instanceof Map<?, ?>) {
                return ja.add(params[0], params[1]);
            }
            throw new Exception("not a script context");
        }

        @Override
        public Object tryInvoke(final String name, final Object obj, final Object... params) {
            try {
                if ("plus".equals(name)) {
                    return invoke(obj, params);
                }
            } catch (final Exception xany) {
            // ignore and fail by returning this
            }
            return this;
        }

        @Override
        public boolean tryFailed(final Object rval) {
            // this is the marker for failure
            return rval == this;
        }

        @Override
        public boolean isCacheable() {
            return true;
        }

        @Override
        public Class<?> getReturnType() {
            return Object.class;
        }
    };
    foo.put("PLUS", mplus);
    forty2 = JEXL.createScript("script:PLUS(4, 2) * script:PLUS(4, 3)");
    o = forty2.execute(context);
    Assert.assertEquals("Result is not 42", new Integer(42), o);
    context.set("foo.bar", foo);
    forty2 = JEXL.createScript("foo.'bar'.PLUS(4, 2) * foo.bar.PLUS(4, 3)");
    o = forty2.execute(context);
    Assert.assertEquals("Result is not 42", new Integer(42), o);
}
Also used : JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 19 with JexlMethod

use of org.apache.commons.jexl3.introspection.JexlMethod in project commons-jexl by apache.

the class MethodTest method testTryFailedScript.

@Test
public void testTryFailedScript() throws Exception {
    // JEXL-257
    final Functor func = new Functor();
    final JexlContext ctxt = new MapContext();
    ctxt.set("func", func);
    Object result;
    final JexlUberspect uber = JEXL.getUberspect();
    final JexlScript method = JEXL.createScript("(x, y)->{ func.over(x, y) }");
    // tryInvoke
    // JexlMethod method = uber.getMethod(func, "over", "foo", 42);
    Assert.assertNotNull(method);
    // tryInvoke succeeds
    result = method.execute(ctxt, "foo", 42);
    Assert.assertEquals("foo + 42", result);
    // tryInvoke fails
    func.setKill(true);
    try {
        /*result = */
        method.execute(ctxt, "foo", 42);
        Assert.fail("should throw TryFailed");
    } catch (final JexlException xfail) {
        Assert.assertEquals(UnsupportedOperationException.class, xfail.getCause().getClass());
    }
    func.setKill(false);
    final JexlScript setter = JEXL.createScript("(x)->{ func.under = x }");
    // JexlPropertySet setter = uber.getPropertySet(func, "under", "42");
    result = setter.execute(ctxt, "42");
    Assert.assertEquals("42", result);
    final JexlScript getter = JEXL.createScript("func.under");
    Assert.assertEquals("42", result);
    func.setKill(true);
    try {
        /*result = */
        setter.execute(ctxt, "42");
        Assert.fail("should throw TryFailed");
    } catch (final JexlException xfail) {
        Assert.assertEquals(UnsupportedOperationException.class, xfail.getCause().getClass());
    }
    func.setKill(false);
    result = setter.execute(ctxt, "-42");
    Assert.assertEquals("-42", result);
    func.setKill(true);
    try {
        /*result = */
        getter.execute(ctxt);
        Assert.fail("should throw TryFailed");
    } catch (final JexlException xfail) {
        Assert.assertEquals(UnsupportedOperationException.class, xfail.getCause().getClass());
    }
    func.setKill(false);
    result = getter.execute(ctxt);
    Assert.assertEquals("-42", result);
}
Also used : JexlUberspect(org.apache.commons.jexl3.introspection.JexlUberspect) Test(org.junit.Test)

Aggregations

JexlMethod (org.apache.commons.jexl3.introspection.JexlMethod)18 JexlException (org.apache.commons.jexl3.JexlException)14 JexlArithmetic (org.apache.commons.jexl3.JexlArithmetic)7 JexlUberspect (org.apache.commons.jexl3.introspection.JexlUberspect)7 Test (org.junit.Test)4 JexlPropertyGet (org.apache.commons.jexl3.introspection.JexlPropertyGet)3 JexlInfo (org.apache.commons.jexl3.JexlInfo)2 ASTNumberLiteral (org.apache.commons.jexl3.parser.ASTNumberLiteral)2 JexlNode (org.apache.commons.jexl3.parser.JexlNode)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 JexlOperator (org.apache.commons.jexl3.JexlOperator)1 JexlScript (org.apache.commons.jexl3.JexlScript)1 JexlPropertySet (org.apache.commons.jexl3.introspection.JexlPropertySet)1 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)1 ASTIdentifierAccess (org.apache.commons.jexl3.parser.ASTIdentifierAccess)1 ASTJexlScript (org.apache.commons.jexl3.parser.ASTJexlScript)1