Java Code Examples for org.apache.commons.jexl3.introspection.JexlPropertySet#invoke()
The following examples show how to use
org.apache.commons.jexl3.introspection.JexlPropertySet#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: ObjectContext.java From commons-jexl with Apache License 2.0 | 5 votes |
@Override public void set(String name, Object value) { JexlPropertySet jset = jexl.getUberspect().getPropertySet(object, name, value); if (jset != null) { try { jset.invoke(object, value); } catch (Exception xany) { // ignore if (jexl.isStrict()) { throw new JexlException.Property(null, name, true, xany); } } } }
Example 2
Source File: DiscoveryTest.java From commons-jexl with Apache License 2.0 | 5 votes |
@Test public void testBeanIntrospection() throws Exception { Uberspect uber = Engine.getUberspect(null, null); Bean bean = new Bean("JEXL", "LXEJ"); JexlPropertyGet get = uber.getPropertyGet(bean, "value"); JexlPropertySet set = uber.getPropertySet(bean, "value", "foo"); Assert.assertTrue("bean property getter", get instanceof PropertyGetExecutor); Assert.assertTrue("bean property setter", set instanceof PropertySetExecutor); // introspector and uberspect should return same result Assert.assertEquals(get, uber.getPropertyGet(bean, "value")); Assert.assertEquals(set, uber.getPropertySet(bean, "value", "foo")); // different property should return different setter/getter Assert.assertNotEquals(get, uber.getPropertyGet(bean, "eulav")); Assert.assertNotEquals(set, uber.getPropertySet(bean, "eulav", "foo")); // setter returns argument Object bar = set.invoke(bean, "bar"); Assert.assertEquals("bar", bar); // getter should return last value Assert.assertEquals("bar", get.invoke(bean)); // tryExecute should succeed on same property Object quux = set.tryInvoke(bean, "value", "quux"); Assert.assertEquals("quux", quux); Assert.assertEquals("quux", get.invoke(bean)); // tryExecute should fail on different property Assert.assertEquals(AbstractExecutor.TRY_FAILED, set.tryInvoke(bean, "eulav", "nope")); }
Example 3
Source File: DiscoveryTest.java From commons-jexl with Apache License 2.0 | 5 votes |
@Test public void testDuckIntrospection() throws Exception { Uberspect uber = Engine.getUberspect(null, null); Duck duck = new Duck("JEXL", "LXEJ"); JexlPropertyGet get = uber.getPropertyGet(duck, "value"); JexlPropertySet set = uber.getPropertySet(duck, "value", "foo"); Assert.assertTrue("duck property getter", get instanceof DuckGetExecutor); Assert.assertTrue("duck property setter", set instanceof DuckSetExecutor); // introspector and uberspect should return same result Assert.assertEquals(get, uber.getPropertyGet(duck, "value")); Assert.assertEquals(set, uber.getPropertySet(duck, "value", "foo")); // different property should return different setter/getter Assert.assertNotEquals(get, uber.getPropertyGet(duck, "eulav")); Assert.assertNotEquals(set, uber.getPropertySet(duck, "eulav", "foo")); // setter returns argument Object bar = set.invoke(duck, "bar"); Assert.assertEquals("bar", bar); // getter should return last value Assert.assertEquals("bar", get.invoke(duck)); // tryExecute should succeed on same property Object quux = set.tryInvoke(duck, "value", "quux"); Assert.assertEquals("quux", quux); Assert.assertEquals("quux", get.invoke(duck)); // tryExecute should fail on different property Assert.assertEquals(AbstractExecutor.TRY_FAILED, set.tryInvoke(duck, "eulav", "nope")); }
Example 4
Source File: DiscoveryTest.java From commons-jexl with Apache License 2.0 | 5 votes |
@Test public void testListIntrospection() throws Exception { Uberspect uber = Engine.getUberspect(null, null); List<Object> list = new ArrayList<Object>(); list.add("LIST"); list.add("TSIL"); JexlPropertyGet get = uber.getPropertyGet(list, 1); JexlPropertySet set = uber.getPropertySet(list, 1, "foo"); Assert.assertTrue("list property getter", get instanceof ListGetExecutor); Assert.assertTrue("list property setter", set instanceof ListSetExecutor); // introspector and uberspect should return same result Assert.assertEquals(get, uber.getPropertyGet(list, 1)); Assert.assertEquals(set, uber.getPropertySet(list, 1, "foo")); // different property should return different setter/getter Assert.assertNotEquals(get, uber.getPropertyGet(list, 0)); Assert.assertNotEquals(get, uber.getPropertySet(list, 0, "foo")); // setter returns argument Object bar = set.invoke(list, "bar"); Assert.assertEquals("bar", bar); // getter should return last value Assert.assertEquals("bar", get.invoke(list)); // tryExecute should succeed on integer property Object quux = set.tryInvoke(list, 1, "quux"); Assert.assertEquals("quux", quux); // getter should return last value Assert.assertEquals("quux", get.invoke(list)); // tryExecute should fail on non-integer property class Assert.assertEquals(AbstractExecutor.TRY_FAILED, set.tryInvoke(list, "eulav", "nope")); }
Example 5
Source File: DiscoveryTest.java From commons-jexl with Apache License 2.0 | 5 votes |
@Test public void testMapIntrospection() throws Exception { Uberspect uber = Engine.getUberspect(null, null); Map<String, Object> map = new HashMap<String, Object>(); map.put("value", "MAP"); map.put("eulav", "PAM"); JexlPropertyGet get = uber.getPropertyGet(map, "value"); JexlPropertySet set = uber.getPropertySet(map, "value", "foo"); Assert.assertTrue("map property getter", get instanceof MapGetExecutor); Assert.assertTrue("map property setter", set instanceof MapSetExecutor); // introspector and uberspect should return same result Assert.assertEquals(get, uber.getPropertyGet(map, "value")); Assert.assertEquals(set, uber.getPropertySet(map, "value", "foo")); // different property should return different setter/getter Assert.assertNotEquals(get, uber.getPropertyGet(map, "eulav")); Assert.assertNotEquals(get, uber.getPropertySet(map, "eulav", "foo")); // setter returns argument Object bar = set.invoke(map, "bar"); Assert.assertEquals("bar", bar); // getter should return last value Assert.assertEquals("bar", get.invoke(map)); // tryExecute should succeed on same property class Object quux = set.tryInvoke(map, "value", "quux"); Assert.assertEquals("quux", quux); // getter should return last value Assert.assertEquals("quux", get.invoke(map)); // tryExecute should fail on different property class Assert.assertEquals(AbstractExecutor.TRY_FAILED, set.tryInvoke(map, 1, "nope")); }
Example 6
Source File: InterpreterBase.java From commons-jexl with Apache License 2.0 | 4 votes |
/** * Sets an attribute of an object. * * @param object to set the value to * @param attribute the attribute of the object, e.g. an index (1, 0, 2) or key for a map * @param value the value to assign to the object's attribute * @param node the node that evaluated as the object */ protected void setAttribute(Object object, Object attribute, Object value, JexlNode node) { cancelCheck(node); final JexlOperator operator = node != null && node.jjtGetParent() instanceof ASTArrayAccess ? JexlOperator.ARRAY_SET : JexlOperator.PROPERTY_SET; Object result = operators.tryOverload(node, operator, object, attribute, value); if (result != JexlEngine.TRY_FAILED) { return; } Exception xcause = null; try { // attempt to reuse last executor cached in volatile JexlNode.value if (node != null && cache) { Object cached = node.jjtGetValue(); if (cached instanceof JexlPropertySet) { JexlPropertySet setter = (JexlPropertySet) cached; Object eval = setter.tryInvoke(object, attribute, value); if (!setter.tryFailed(eval)) { return; } } } List<JexlUberspect.PropertyResolver> resolvers = uberspect.getResolvers(operator, object); JexlPropertySet vs = uberspect.getPropertySet(resolvers, object, attribute, value); // if we can't find an exact match, narrow the value argument and try again if (vs == null) { // replace all numbers with the smallest type that will fit Object[] narrow = {value}; if (arithmetic.narrowArguments(narrow)) { vs = uberspect.getPropertySet(resolvers, object, attribute, narrow[0]); } } if (vs != null) { // cache executor in volatile JexlNode.value vs.invoke(object, value); if (node != null && cache && vs.isCacheable()) { node.jjtSetValue(vs); } return; } } catch (Exception xany) { xcause = xany; } // lets fail if (node != null) { String attrStr = attribute != null ? attribute.toString() : null; unsolvableProperty(node, attrStr, true, xcause); } else { // direct call String error = "unable to set object property" + ", class: " + object.getClass().getName() + ", property: " + attribute + ", argument: " + value.getClass().getSimpleName(); throw new UnsupportedOperationException(error, xcause); } }