Search in sources :

Example 1 with El

use of org.nutz.el.El in project nutz by nutzam.

the class ObjConvertImpl method injectObj.

@SuppressWarnings("unchecked")
private Object injectObj(Object model, Mirror<?> mirror) {
    // zzh: 如果是 Object,那么就不要转换了
    if (mirror.getType() == Object.class)
        return model;
    Object obj = mirror.born();
    context.set(fetchPath(), obj);
    Map<String, ?> map = (Map<String, ?>) model;
    JsonEntity jen = Json.getEntity(mirror);
    for (String key : map.keySet()) {
        JsonEntityField jef = jen.getField(key);
        if (jef == null) {
            continue;
        }
        Object val = map.get(jef.getName());
        if (val == null) {
            continue;
        }
        if (isLeaf(val)) {
            if (val instanceof El) {
                val = ((El) val).eval(context);
            }
            // zzh@2012-09-14: 暂时去掉 createBy 吧
            // jef.setValue(obj, Castors.me().castTo(jef.createValue(obj,
            // val, null), Lang.getTypeClass(jef.getGenericType())));
            // jef.setValue(obj, jef.createValue(obj, val, null));
            jef.setValue(obj, Mapl.maplistToObj(val, jef.getGenericType()));
            continue;
        } else {
            path.push(key);
            // jef.setValue(obj, Mapl.maplistToObj(val,
            // me.getGenericsType(0)));
            jef.setValue(obj, Mapl.maplistToObj(val, jef.getGenericType()));
        // zzh@2012-09-14: 暂时去掉 createBy 吧
        // jef.setValue(obj, jef.createValue(obj, val,
        // me.getGenericsType(0)));
        }
    }
    return obj;
}
Also used : JsonEntity(org.nutz.json.entity.JsonEntity) JsonEntityField(org.nutz.json.entity.JsonEntityField) El(org.nutz.el.El) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with El

use of org.nutz.el.El in project nutz by nutzam.

the class Issue125Test method test2.

@Test
public void test2() {
    String[] a = new String[] { "a", "b" };
    String[] b = new String[] { "1", "2" };
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("a", a);
    map.put("b", b);
    // 预编译结果为一个 El 对象
    El exp = new El("util.test(map['a'][0],map['b'][0])");
    Context context = Lang.context();
    context.set("util", new StringUtil());
    context.set("map", map);
    System.out.println(exp.eval(context));
}
Also used : Context(org.nutz.lang.util.Context) HashMap(java.util.HashMap) El(org.nutz.el.El) Test(org.junit.Test)

Example 3 with El

use of org.nutz.el.El in project nutz by nutzam.

the class Issue125Test method test.

@Test
public void test() throws InstantiationException, IllegalAccessException {
    String[] a = new String[] { "a", "b" };
    Map<String, String[]> map = new HashMap<String, String[]>();
    map.put("a", a);
    El exp = new El("util.test(map['a'])");
    Context context = Lang.context();
    context.set("util", StringUtil.class.newInstance());
    context.set("map", map);
    assertEquals("ab", exp.eval(context));
}
Also used : Context(org.nutz.lang.util.Context) HashMap(java.util.HashMap) El(org.nutz.el.El) Test(org.junit.Test)

Example 4 with El

use of org.nutz.el.El in project nutz by nutzam.

the class SimpleSpeedTest method test_speed.

@Ignore
@Test
public void test_speed() throws SecurityException, NoSuchMethodException {
    final SimpleSpeedTest z = new SimpleSpeedTest();
    final String elstr = "num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7)-z.abc(i)";
    final Context context = Lang.context("{num:0}");
    context.set("z", z);
    System.out.println("\n" + Strings.dup('=', 100));
    Stopwatch sw = Stopwatch.run(new Atom() {

        public void run() {
            int num = 0;
            for (int i = 0; i < max; i++) num = num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7) - z.abc(i);
        //System.out.println("Num: " + num);
        }
    });
    System.out.println("\n" + Strings.dup('=', 100));
    Stopwatch sw3 = Stopwatch.run(new Atom() {

        public void run() {
            try {
                context.set("num", 0);
                for (int i = 0; i < max; i++) context.set("num", El.eval(context.set("i", i), elstr));
            //System.out.println("Num: " + context.getInt("num"));
            } catch (Exception e) {
                throw Lang.wrapThrow(e);
            }
        }
    });
    System.out.println("\n" + Strings.dup('=', 100));
    Stopwatch sw4 = Stopwatch.run(new Atom() {

        public void run() {
            try {
                El el2pre = new El(elstr);
                context.set("num", 0);
                context.set("z", z);
                for (int i = 0; i < max; i++) context.set("num", el2pre.eval(context.set("i", i)));
            //System.out.println("Num: " + context.getInt("num"));
            } catch (Exception e) {
                throw Lang.wrapThrow(e);
            }
        }
    });
    System.out.println("\n" + Strings.dup('=', 100));
    Stopwatch sw5 = Stopwatch.run(new Atom() {

        public void run() {
            try {
                El el2pre = new El(elstr);
                context.set("num", 0);
                context.set("z", z);
                for (int i = 0; i < max; i++) context.set("num", el2pre.eval(context.set("i", i)));
            //System.out.println("Num: " + context.getInt("num"));
            } catch (Exception e) {
                throw Lang.wrapThrow(e);
            }
        }
    });
    System.out.println("\n" + Strings.dup('=', 100));
    System.out.printf("\n%20s : %s", "Invoke", sw.toString());
    System.out.printf("\n%20s : %s", "Reflect", sw3.toString());
    System.out.printf("\n%20s : %s", "Reflect", sw4.toString());
    System.out.printf("\n%20s : %s", "Reflect", sw5.toString());
    System.out.println();
}
Also used : Context(org.nutz.lang.util.Context) El(org.nutz.el.El) Stopwatch(org.nutz.lang.Stopwatch) Atom(org.nutz.trans.Atom) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

El (org.nutz.el.El)4 Test (org.junit.Test)3 Context (org.nutz.lang.util.Context)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Ignore (org.junit.Ignore)1 JsonEntity (org.nutz.json.entity.JsonEntity)1 JsonEntityField (org.nutz.json.entity.JsonEntityField)1 Stopwatch (org.nutz.lang.Stopwatch)1 Atom (org.nutz.trans.Atom)1