Example usage for java.beans Expression Expression

List of usage examples for java.beans Expression Expression

Introduction

In this page you can find the example usage for java.beans Expression Expression.

Prototype

@ConstructorProperties({ "target", "methodName", "arguments" })
public Expression(Object target, String methodName, Object[] arguments) 

Source Link

Document

Creates a new Expression object for the specified target object to invoke the method specified by the name and by the array of arguments.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object o = new MyBean();

    Expression expr = new Expression(o, "getProp3", new Object[0]);
    expr.execute();//  w w  w  . j  ava  2s  .co  m
    byte[] bytes = (byte[]) expr.getValue();

    Statement stmt = new Statement(o, "setProp3", new Object[] { new byte[] { 0x12, 0x23 } });
    stmt.execute();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object o = new MyBean();
    // Get the value of prop2
    Expression expr = new Expression(o, "getProp2", new Object[0]);
    expr.execute();//w w w  .j a v a  2  s . c  o  m
    int i = ((Integer) expr.getValue()).intValue();

    // Set the value of prop2
    Statement stmt = new Statement(o, "setProp2", new Object[] { new Integer(123) });
    stmt.execute();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object o = new MyBean();
    // Get the value of prop1
    Expression expr = new Expression(o, "getProp1", new Object[0]);
    expr.execute();/* w ww .j  av a  2s . c o  m*/
    String s = (String) expr.getValue();

    // Set the value of prop1
    Statement stmt = new Statement(o, "setProp1", new Object[] { "new string" });
    stmt.execute();
}

From source file:edu.northwestern.bioinformatics.studycalendar.web.taglibs.Functions.java

public static Collection pluck(Collection collection, String property) {
    Collection result = new ArrayList(collection.size());
    for (Object o : collection) {
        try {/*  w  w  w . ja va  2  s .  c  o  m*/
            Object plucked = new Expression(o, "get" + capitalize(property), null).getValue();
            result.add(plucked);
        } catch (Exception e) {
            throw new StudyCalendarError("Unable to get the '" + property + "' property", e);
        }

    }
    return result;
}

From source file:cz.autoclient.league_of_legends.maps.GameObjectMap.java

public synchronized void loadData() {
    if (data != null)
        return;// w w w.  ja v a  2 s. co  m
    data = new HashMap();
    JSONObject jsonData = getJSONData();
    try {
        jsonData = jsonData.getJSONObject("data");
    } catch (JSONException e) {
        return;
    }

    if (jsonData != null) {
        try {
            Iterator<String> keys = jsonData.keys();
            String key;
            JSONObject json_entry;
            while (keys.hasNext() && (key = keys.next()) != null) {
                if ((json_entry = jsonData.getJSONObject(key)) != null) {
                    try {
                        T result = (T) new Expression(type, "new", new Object[] { this, key, json_entry })
                                .getValue();
                        data.put(key, result);
                    } catch (Exception e) {
                        //must not happen
                        e.printStackTrace();
                        throw new RuntimeException(
                                "Failed to instantiate game object of type " + type.getName());

                        //throw new NoSuchMethodException("Constructor for "+handler.getName()+" failed with exception:"+e);
                    }
                }
            }
        } catch (JSONException ex) {
            return;
        }
    }
}

From source file:ca.oson.json.util.ObjectUtil.java

@SuppressWarnings("unchecked")
public static <E, R> R getMethodValue(E obj, Method method, Object... args) {
    R value = null;//from   w  w  w. j  a v a2 s . c  om

    try {
        method.setAccessible(true);

        value = (R) method.invoke(obj, args);
    } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
        // e.printStackTrace();
        try {
            if (obj != null) {
                Expression expr = new Expression(obj, method.getName(), args);
                expr.execute();
                value = (R) expr.getValue();
            }

            if (value == null) {
                value = (R) method.getDefaultValue();
            }

        } catch (Exception e1) {
            // e1.printStackTrace();
        }
    }

    return value;
}

From source file:org.pentaho.ui.xul.jface.tags.JfaceCMenuList.java

private String extractLabel(T t) {
    String attribute = getBinding();
    if (StringUtils.isEmpty(attribute)) {
        return t.toString();
    } else {// w w w .j  a va 2s  .  c om
        String getter = "get" + (String.valueOf(attribute.charAt(0)).toUpperCase()) + attribute.substring(1);
        try {
            return new Expression(t, getter, null).getValue().toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:org.pentaho.ui.xul.swing.tags.SwingListbox.java

private <T> String extractLabel(T t) {
    String attribute = getBinding();
    if (StringUtils.isEmpty(attribute)) {
        return t.toString();
    } else {// ww w.  j av a  2s. c o  m
        String getter = "get" + (String.valueOf(attribute.charAt(0)).toUpperCase()) + attribute.substring(1);
        try {
            return new Expression(t, getter, null).getValue().toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:org.pentaho.ui.xul.swt.tags.SwtListbox.java

private <T> void wireLabel(T t, SwtListitem item) {
    if (t == null) {
        return;/*  w ww .  j  a v a2  s.  com*/
    }

    String attribute = getBinding();
    if (StringUtils.isNotEmpty(attribute)) {

        try {

            if (t instanceof XulEventSource) {

                Binding binding = createBinding((XulEventSource) t, attribute, item, "label");
                elementBindings.add(binding);
                binding.setBindingType(Binding.Type.ONE_WAY);
                container.addBinding(binding);
                binding.fireSourceChanged();

            } else {

                // do things the old way; backward compatibility to pre-binding code

                String getter = "get" + (String.valueOf(attribute.charAt(0)).toUpperCase())
                        + attribute.substring(1);
                String label = "" + (new Expression(t, getter, null).getValue());
                item.setValue(label); // the object was not preserved in history .. it shouldn't be here
                item.setLabel(label);

            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        item.setLabel(t.toString());
    }
}

From source file:org.op4j.functions.Get.java

@SuppressWarnings("unchecked")
public R execute(final T input, final ExecCtx ctx) throws Exception {

    final Class<? super R> resultClass = this.resultType.getRawClass();

    final Expression expression = new Expression(input, "get" + StringUtils.capitalize(this.attributeName),
            null);//from  w  ww .  ja va 2 s .  c  om
    final R result = (R) expression.getValue();

    if (result != null && resultClass != null && !Object.class.equals(resultClass)) {
        if (!(resultClass.isAssignableFrom(result.getClass()))) {
            throw new IllegalStateException("Result of calling method \"" + this.attributeName + "\" is not "
                    + "assignable from class " + resultClass.getName());
        }
    }

    return result;

}