Search in sources :

Example 1 with Function

use of org.beetl.core.Function in project beetl2.0 by javamonkey.

the class FunctionExpression method infer.

public void infer(InferContext inferCtx) {
    Function fn = inferCtx.gt.getFunction(name);
    if (fn == null) {
        Resource resource = getResource(inferCtx.gt, name);
        if (resource == null) {
            BeetlException ex = new BeetlException(BeetlException.FUNCTION_NOT_FOUND);
            ex.pushToken(token);
            throw ex;
        } else {
            fn = new FileFunctionWrapper(resource.getId());
        }
    }
    for (Expression exp : exps) {
        exp.infer(inferCtx);
    }
    // return type;
    Class c = null;
    if (fn instanceof SingleFunctionWrapper) {
        SingleFunctionWrapper singleWrapper = (SingleFunctionWrapper) fn;
        c = singleWrapper.getReturnType();
    } else if (fn instanceof MutipleFunctionWrapper) {
        try {
            Class[] parasType = new Class[exps.length];
            int i = 0;
            for (Expression exp : exps) {
                exp.infer(inferCtx);
                parasType[i++] = exp.getType().cls;
            }
            c = ((MutipleFunctionWrapper) fn).getReturnType(parasType);
        } catch (BeetlException ex) {
            ex.pushToken(token);
            throw ex;
        }
    } else {
        Method call = null;
        try {
            call = fn.getClass().getMethod("call", Object[].class, Context.class);
            c = call.getReturnType();
        } catch (NoSuchMethodException e) {
            BeetlException ex = new BeetlException(BeetlException.FUNCTION_INVALID);
            ex.pushToken(token);
            throw ex;
        } catch (SecurityException e) {
            BeetlException ex = new BeetlException(BeetlException.FUNCTION_INVALID);
            ex.pushToken(token);
            throw ex;
        }
    }
    Type lastType = new Type(c);
    if (vas == null) {
        this.type = lastType;
        return;
    } else {
        Type t = null;
        for (VarAttribute attr : vas) {
            inferCtx.temp = lastType;
            attr.infer(inferCtx);
            t = lastType;
            lastType = attr.type;
            attr.type = t;
        }
        this.type = lastType;
    }
    if (safeExp != null) {
        safeExp.infer(inferCtx);
        if (!safeExp.type.equals(this.type)) {
            this.type = Type.ObjectType;
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) FileFunctionWrapper(org.beetl.core.fun.FileFunctionWrapper) SingleFunctionWrapper(org.beetl.core.fun.SingleFunctionWrapper) Resource(org.beetl.core.Resource) Method(java.lang.reflect.Method) Function(org.beetl.core.Function) MutipleFunctionWrapper(org.beetl.core.fun.MutipleFunctionWrapper)

Example 2 with Function

use of org.beetl.core.Function in project beetl2.0 by javamonkey.

the class SimpleVATest method testFunctionAttribute.

@Test
public void testFunctionAttribute() throws Exception {
    gt.registerFunction("userArray", new Function() {

        @Override
        public Object call(Object[] paras, Context ctx) {
            // TODO Auto-generated method stub
            return new Object[] { User.getTestUser(), "a" };
        }
    });
    Template t = gt.getTemplate("/va/va_fun_template.html");
    String str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/va/va_fun_expected.html"), str);
    t = gt.getTemplate("/va/va_fun_template.html");
    str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/va/va_fun_expected.html"), str);
}
Also used : Context(org.beetl.core.Context) Function(org.beetl.core.Function) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 3 with Function

use of org.beetl.core.Function in project beetl2.0 by javamonkey.

the class FunctionExpression method evaluate.

public Object evaluate(Context ctx) {
    Function fn = ctx.gt.getFunction(name);
    if (fn == null) {
        // 检查html实现
        Resource resource = getResource(ctx.gt, name);
        if (resource.getResourceLoader().exist(resource.getId())) {
            fn = new FileFunctionWrapper(resource.getId());
        } else {
            BeetlException ex = new BeetlException(BeetlException.FUNCTION_NOT_FOUND);
            ex.pushToken(token);
            throw ex;
        }
    }
    Object[] paras = new Object[exps.length];
    for (int i = 0; i < paras.length; i++) {
        paras[i] = exps[i].evaluate(ctx);
    }
    Object value = null;
    try {
        value = fn.call(paras, ctx);
    } catch (BeetlException ex) {
        ex.pushToken(token);
        throw ex;
    } catch (RuntimeException ex) {
        BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "调用方法出错 " + name, ex);
        be.pushToken(this.token);
        throw be;
    }
    Object ret = null;
    if (vas == null) {
        ret = value;
    } else {
        for (VarAttribute attr : vas) {
            try {
                value = attr.evaluate(ctx, value);
            } catch (BeetlException ex) {
                ex.pushToken(attr.token);
                throw ex;
            } catch (RuntimeException ex) {
                BeetlException be = new BeetlException(BeetlException.ATTRIBUTE_INVALID, "属性访问出错", ex);
                be.pushToken(attr.token);
                throw be;
            }
            if (value == null) {
                if (hasSafe) {
                    return safeExp == null ? null : safeExp.evaluate(ctx);
                } else {
                    BeetlException be = new BeetlException(BeetlException.ERROR, "空指针 ");
                    be.pushToken(attr.token);
                    throw be;
                }
            }
        }
        ret = value;
    }
    if (ret == null && hasSafe) {
        return safeExp == null ? null : safeExp.evaluate(ctx);
    } else {
        return ret;
    }
}
Also used : Function(org.beetl.core.Function) FileFunctionWrapper(org.beetl.core.fun.FileFunctionWrapper) BeetlException(org.beetl.core.exception.BeetlException) Resource(org.beetl.core.Resource)

Aggregations

Function (org.beetl.core.Function)3 Resource (org.beetl.core.Resource)2 BeetlException (org.beetl.core.exception.BeetlException)2 FileFunctionWrapper (org.beetl.core.fun.FileFunctionWrapper)2 Method (java.lang.reflect.Method)1 Context (org.beetl.core.Context)1 Template (org.beetl.core.Template)1 MutipleFunctionWrapper (org.beetl.core.fun.MutipleFunctionWrapper)1 SingleFunctionWrapper (org.beetl.core.fun.SingleFunctionWrapper)1 Test (org.testng.annotations.Test)1