org.apache.commons.jexl3.introspection.JexlPropertySet Java Examples

The following examples show how to use org.apache.commons.jexl3.introspection.JexlPropertySet. 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: StrategyTest.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
@Test
public void testRawResolvers() throws Exception {
    Object map  = new HashMap<String, Object>();
    final JexlEngine jexl = new JexlBuilder().create();
    JexlUberspect uberspect = jexl.getUberspect();
    JexlUberspect.PropertyResolver rfieldp = JexlUberspect.JexlResolver.FIELD;
    JexlPropertyGet fget = rfieldp.getPropertyGet(uberspect, map, "key");
    Assert.assertNull(fget);
    JexlPropertySet fset = rfieldp.getPropertySet(uberspect, map, "key", "value");
    Assert.assertNull(fset);
    JexlUberspect.PropertyResolver rmap = JexlUberspect.JexlResolver.MAP;
    JexlPropertyGet mget = rmap.getPropertyGet(uberspect, map, "key");
    Assert.assertNotNull(mget);
    JexlPropertySet mset = rmap.getPropertySet(uberspect, map, "key", "value");
    Assert.assertNotNull(mset);
}
 
Example #2
Source File: SandboxUberspect.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
@Override
public JexlPropertySet getPropertySet(final List<PropertyResolver> resolvers,
                                      final Object obj,
                                      final Object identifier,
                                      final Object arg) {
    if (obj != null && identifier != null) {
        String property = identifier.toString();
        String actual = sandbox.write(obj.getClass(), property);
        if (actual != null) {
             // no transformation, strict equality: use identifier before string conversion
            Object pty = actual == property? identifier : actual;
            return uberspect.getPropertySet(resolvers, obj, pty, arg);
        }
    }
    return null;
}
 
Example #3
Source File: DiscoveryTest.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@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 #4
Source File: DiscoveryTest.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@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 vote down vote up
@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 #6
Source File: JexlNode.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #7
Source File: ObjectContext.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@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 #8
Source File: DiscoveryTest.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@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 #9
Source File: FieldSetExecutor.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to discover a FieldSetExecutor.
 *
 * @param is the introspector
 * @param clazz the class to find the get method from
 * @param identifier the key to use as an argument to the get method
 * @param value the value to set the field to
 * @return the executor if found, null otherwise
 */
public static JexlPropertySet discover(Introspector is, Class<?> clazz, String identifier, Object value) {
    if (identifier != null) {
        Field field = is.getField(clazz, identifier);
        if (field != null
            && !Modifier.isFinal(field.getModifiers())
            && (value == null || MethodKey.isInvocationConvertible(field.getType(), value.getClass(), false))) {
            return new FieldSetExecutor(field);
        }
    }
    return null;
}
 
Example #10
Source File: Uberspect.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Override
public JexlPropertySet getPropertySet(final Object obj, final Object identifier, final Object arg) {
    return getPropertySet(null, obj, identifier, arg);
}
 
Example #11
Source File: SandboxUberspect.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Override
public JexlPropertySet getPropertySet(final Object obj,final Object identifier,final Object arg) {
    return getPropertySet(null, obj, identifier, arg);
}
 
Example #12
Source File: Uberspect.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Override
public JexlPropertySet getPropertySet(
        final List<PropertyResolver> resolvers, final Object obj, final Object identifier, final Object arg
) {
    final Class<?> claz = obj.getClass();
    final String property = AbstractExecutor.castString(identifier);
    final Introspector is = base();
    final List<PropertyResolver> actual = resolvers == null? strategy.apply(null, obj) : resolvers;
    JexlPropertySet executor = null;
    for (PropertyResolver resolver : actual) {
        if (resolver instanceof JexlResolver) {
            switch ((JexlResolver) resolver) {
                case PROPERTY:
                    // first try for a setFoo() type of property (also setfoo() )
                    executor = PropertySetExecutor.discover(is, claz, property, arg);
                    break;
                case MAP:
                    // let's see if we are a map...
                    executor = MapSetExecutor.discover(is, claz, identifier, arg);
                    break;
                case LIST:
                // let's see if we can convert the identifier to an int,
                    // if obj is an array or a list, we can still do something
                    Integer index = AbstractExecutor.castInteger(identifier);
                    if (index != null) {
                        executor = ListSetExecutor.discover(is, claz, identifier, arg);
                    }
                    break;
                case DUCK:
                    // if that didn't work, look for set(foo)
                    executor = DuckSetExecutor.discover(is, claz, identifier, arg);
                    if (executor == null && property != null && property != identifier) {
                        executor = DuckSetExecutor.discover(is, claz, property, arg);
                    }
                    break;
                case FIELD:
                    // a field may be?
                    executor = FieldSetExecutor.discover(is, claz, property, arg);
                    break;
                case CONTAINER:
                default:
                    continue; // in case we add new ones in enum
            }
        } else {
            executor = resolver.getPropertySet(this, obj, identifier, arg);
        }
        if (executor != null) {
            return executor;
        }
    }
    return null;
}
 
Example #13
Source File: SandboxJexlUberspect.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public JexlPropertySet getPropertySet(final Object obj, final Object identifier, final Object arg) {
  return null;
}
 
Example #14
Source File: InterpreterBase.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
}
 
Example #15
Source File: SandboxUberspect.java    From syncope with Apache License 2.0 4 votes vote down vote up
@Override
public JexlPropertySet getPropertySet(
        final List<PropertyResolver> resolvers, final Object obj, final Object identifier, final Object arg) {

    return null;
}
 
Example #16
Source File: SandboxUberspect.java    From syncope with Apache License 2.0 4 votes vote down vote up
@Override
public JexlPropertySet getPropertySet(final Object obj, final Object identifier, final Object arg) {
    return null;
}
 
Example #17
Source File: SandboxJexlUberspect.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public JexlPropertySet getPropertySet(final List<PropertyResolver> resolvers, final Object obj, final Object identifier, final Object arg) {
  return null;
}