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;
}
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));
}
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));
}
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();
}
Aggregations