Search in sources :

Example 1 with ElException

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

the class ByMake method run.

public Object run(List<Object> fetchParam) {
    if (fetchParam.isEmpty())
        throw new ElException("'by' must have params");
    String p = (String) fetchParam.remove(0);
    String className = p;
    String methodName = null;
    if (p.contains("#")) {
        String[] tmp = p.split("#");
        className = tmp[0];
        methodName = tmp[1];
    }
    try {
        Class<?> klass = Lang.loadClass(className);
        if (methodName == null) {
            methodName = "run";
        }
        if (RunMethod.class.isAssignableFrom(klass)) {
            return ((RunMethod) klass.newInstance()).run(fetchParam);
        }
        Object[] args = fetchParam.toArray();
        Method method = Mirror.me(klass).findMethod(methodName, args);
        if (Modifier.isStatic(method.getModifiers())) {
            return method.invoke(null, args);
        } else {
            return method.invoke(klass.newInstance(), args);
        }
    } catch (Exception e) {
        throw new ElException(e);
    }
}
Also used : RunMethod(org.nutz.el.opt.RunMethod) ElException(org.nutz.el.ElException) Method(java.lang.reflect.Method) RunMethod(org.nutz.el.opt.RunMethod) ElException(org.nutz.el.ElException)

Example 2 with ElException

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

the class AccessOpt method calculate.

public Object calculate() {
    //如果直接调用计算方法,那基本上就是直接调用属性了吧...我也不知道^^
    Object obj = fetchVar();
    if (obj == null) {
        throw new ElException("obj is NULL, can't call obj." + right);
    }
    if (obj instanceof Map) {
        Map<?, ?> om = (Map<?, ?>) obj;
        if (om.containsKey(right.toString())) {
            return om.get(right.toString());
        }
    }
    if (obj instanceof Context) {
        Context sc = (Context) obj;
        if (sc.has(right.toString())) {
            return sc.get(right.toString());
        }
    }
    Mirror<?> me = Mirror.me(obj);
    return me.getValue(obj, right.toString());
}
Also used : Context(org.nutz.lang.util.Context) ElException(org.nutz.el.ElException) Map(java.util.Map)

Aggregations

ElException (org.nutz.el.ElException)2 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 RunMethod (org.nutz.el.opt.RunMethod)1 Context (org.nutz.lang.util.Context)1