Search in sources :

Example 26 with BeetlException

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

the class NativeCallExpression method infer.

@Override
public void infer(InferContext inferCtx) {
    Type type = null;
    if (insNode != null) {
        insNode.ref.infer(inferCtx);
        type = insNode.ref.type.copy();
    } else {
        Class cls = inferCtx.gt.loadClassBySimpleName(this.clsNode.cls);
        if (cls == null) {
            BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "该类不存在");
            be.pushToken(GrammarToken.createToken(clsNode.cls, token.line));
            throw be;
        }
        type = new Type(cls);
    }
    for (NativeNode node : chain) {
        if (type.cls == Object.class) {
            this.type = type;
            // 以后生成代码有问题
            return;
        }
        if (node instanceof NativeAtrributeNode) {
            String attr = ((NativeAtrributeNode) node).attribute;
            try {
                Field f = type.cls.getField(attr);
                Class c = f.getType();
                type.cls = c;
            } catch (SecurityException e) {
                BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "无法访问属性", e);
                be.pushToken(GrammarToken.createToken(attr, token.line));
                throw be;
            } catch (NoSuchFieldException e) {
                BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "无此属性", e);
                be.pushToken(GrammarToken.createToken(attr, token.line));
                throw be;
            }
        } else if (node instanceof NativeArrayNode) {
            if (!type.cls.isArray()) {
                BeetlException be = new BeetlException(BeetlException.ARRAY_TYPE_ERROR);
                // 最好是把上一个字符显示出来
                be.pushToken(GrammarToken.createToken("[]", token.line));
                throw be;
            }
            type.cls = type.cls.getComponentType();
        } else if (node instanceof NativeMethodNode) {
            NativeMethodNode methodNode = (NativeMethodNode) node;
            String method = methodNode.method;
            Expression[] expList = methodNode.paras;
            Class[] argTypes = expList.length == 0 ? ObjectUtil.EMPTY_CLASS_ARRAY : new Class[expList.length];
            for (int i = 0; i < expList.length; i++) {
                expList[i].infer(inferCtx);
                argTypes[i] = expList[i].type.cls;
            }
            try {
                ObjectMethodMatchConf conf = ObjectUtil.findMethod(type.cls, method, argTypes);
                if (conf == null) {
                    type.cls = Object.class;
                } else {
                    type.cls = conf.method.getReturnType();
                }
            } catch (SecurityException e) {
                BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "不能调用方法");
                be.pushToken(GrammarToken.createToken(method, token.line));
                throw be;
            }
        }
    }
    this.type = type;
}
Also used : NativeArrayNode(org.beetl.core.statement.nat.NativeArrayNode) BeetlException(org.beetl.core.exception.BeetlException) NativeNode(org.beetl.core.statement.nat.NativeNode) NativeAtrributeNode(org.beetl.core.statement.nat.NativeAtrributeNode) Field(java.lang.reflect.Field) ObjectMethodMatchConf(org.beetl.core.om.ObjectMethodMatchConf) NativeMethodNode(org.beetl.core.statement.nat.NativeMethodNode)

Example 27 with BeetlException

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

the class Json method call.

@Override
public Object call(Object[] paras, Context ctx) {
    if (tool == null) {
        return "找不到JSonTool工具,https://git.oschina.net/xiandafu/beetl-json/attach_files 下载并放到classpath下";
    }
    Object o = paras[0];
    String policy = "";
    if (paras.length == 2) {
        policy = paras[1].toString();
    }
    Method m;
    try {
        m = tool.getClass().getMethod("serialize", new Class[] { Object.class, String.class });
    } catch (Exception e) {
        throw new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "BTJsonTool出错");
    }
    String json;
    try {
        json = (String) m.invoke(tool, new Object[] { o, policy });
    } catch (Exception e) {
        throw new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "BTJsonTool 序列化对象出错", e);
    }
    return json;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) Method(java.lang.reflect.Method) BeetlException(org.beetl.core.exception.BeetlException)

Example 28 with BeetlException

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

the class SelectStatement method execute.

@Override
public void execute(Context ctx) {
    Object base = null;
    if (value != null) {
        base = value.evaluate(ctx);
        if (base == null) {
            BeetlException ex = new BeetlException(BeetlException.NULL);
            ex.pushToken(value.token);
            throw ex;
        }
    }
    boolean isMatch = false;
    for (int i = 0; i < conditions.length; i++) {
        Expression exp = conditions[i];
        Object condValue = exp.evaluate(ctx);
        if (value == null) {
            if (condValue instanceof Boolean) {
                isMatch = (Boolean) condValue;
            } else {
                BeetlException be = new BeetlException(BeetlException.BOOLEAN_EXPECTED_ERROR);
                be.pushToken(exp.token);
                throw be;
            }
        } else {
            if (ALU.equals(base, condValue)) {
                isMatch = true;
            }
        }
        if (isMatch) {
            BlockStatement block = this.blocks[i];
            if (block != null) {
                block.execute(ctx);
            }
            break;
        }
    }
    if (!isMatch && defaultBlock != null) {
        defaultBlock.execute(ctx);
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException)

Example 29 with BeetlException

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

the class TagStatement method execute.

@Override
public void execute(Context ctx) {
    Tag tag = null;
    try {
        TagFactory tagFactory = ctx.gt.getTagFactory(this.tagName);
        tag = tagFactory.createTag();
        Object[] args = null;
        if (paras.length == 0) {
            args = ObjectUtil.EMPTY_OBJECT_ARRAY;
        } else {
            args = new Object[paras.length];
            for (int i = 0; i < args.length; i++) {
                args[i] = paras[i].evaluate(ctx);
            }
        }
        tag.init(ctx, args, block);
        runTag(tag, ctx);
    } catch (BeetlException ex) {
        ex.pushToken(this.token);
        throw ex;
    } catch (RuntimeException ex) {
        BeetlException bex = new BeetlException(BeetlException.TAG_INSTANCE_ERROR, ex.getMessage(), ex);
        bex.pushToken(token);
        throw bex;
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) TagFactory(org.beetl.core.TagFactory) Tag(org.beetl.core.Tag)

Example 30 with BeetlException

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

the class VarRefAssignStatement method execute.

public void execute(Context ctx) {
    Object value = exp.evaluate(ctx);
    Object obj = varRef.evaluateUntilLast(ctx);
    Object key = null;
    if (lastVarAttribute instanceof VarSquareAttribute) {
        key = (((VarSquareAttribute) lastVarAttribute).exp).evaluate(ctx);
    } else {
        key = lastVarAttribute.name;
    }
    try {
        ObjectAA.defaultObjectAA().setValue(obj, key, value);
    } catch (ClassCastException ex) {
        BeetlException bx = new BeetlException(BeetlException.ATTRIBUTE_INVALID, ex);
        bx.pushToken(lastVarAttribute.token);
        throw bx;
    } catch (BeetlException be) {
        be.pushToken(lastVarAttribute.token);
        throw be;
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException)

Aggregations

BeetlException (org.beetl.core.exception.BeetlException)60 GrammarToken (org.beetl.core.statement.GrammarToken)10 IOException (java.io.IOException)7 BeetlParserException (org.beetl.core.exception.BeetlParserException)6 Expression (org.beetl.core.statement.Expression)6 AndExpression (org.beetl.core.statement.AndExpression)5 ArthExpression (org.beetl.core.statement.ArthExpression)5 CompareExpression (org.beetl.core.statement.CompareExpression)5 ContentBodyExpression (org.beetl.core.statement.ContentBodyExpression)5 FormatExpression (org.beetl.core.statement.FormatExpression)5 FunctionExpression (org.beetl.core.statement.FunctionExpression)5 IncDecExpression (org.beetl.core.statement.IncDecExpression)5 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)5 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)5 NativeCallExpression (org.beetl.core.statement.NativeCallExpression)5 NegExpression (org.beetl.core.statement.NegExpression)5 NotBooleanExpression (org.beetl.core.statement.NotBooleanExpression)5 OrExpression (org.beetl.core.statement.OrExpression)5 StatementExpression (org.beetl.core.statement.StatementExpression)5 TernaryExpression (org.beetl.core.statement.TernaryExpression)5