Search in sources :

Example 1 with MutipleFunctionWrapper

use of org.beetl.core.fun.MutipleFunctionWrapper 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)

Aggregations

Method (java.lang.reflect.Method)1 Function (org.beetl.core.Function)1 Resource (org.beetl.core.Resource)1 BeetlException (org.beetl.core.exception.BeetlException)1 FileFunctionWrapper (org.beetl.core.fun.FileFunctionWrapper)1 MutipleFunctionWrapper (org.beetl.core.fun.MutipleFunctionWrapper)1 SingleFunctionWrapper (org.beetl.core.fun.SingleFunctionWrapper)1