Search in sources :

Example 46 with BeetlException

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

the class ObjectAA method setValue.

public void setValue(Object o, Object key, Object value) {
    if (o instanceof Map) {
        ((Map) o).put(key, value);
    } else if (o instanceof List) {
        try {
            ((List) o).set(((Number) key).intValue(), value);
        } catch (IndexOutOfBoundsException ex) {
            BeetlException be = new BeetlException(BeetlException.ATTRIBUTE_INVALID, ex);
            throw be;
        } catch (ClassCastException ex) {
            throw new ClassCastException("目标位为java.util.List,无法设置属性:" + key);
        }
    } else if (o.getClass().isArray()) {
        try {
            if (o.getClass().getComponentType().isPrimitive()) {
                PrimitiveArrayUtil.setObject(o, (((Number) key).intValue()), value);
            } else {
                ((Object[]) o)[(((Number) key).intValue())] = value;
            }
        } catch (ClassCastException ex) {
            throw new ClassCastException("类型为数组,无此属性:" + key);
        }
    } else {
        Class c = o.getClass();
        MethodInvoker invoker = ObjectUtil.getInvokder(c, (String) key);
        if (invoker != null) {
            invoker.set(o, value);
        } else {
            BeetlException ex = new BeetlException(BeetlException.ATTRIBUTE_NOT_FOUND, (String) key);
            throw ex;
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) List(java.util.List) Map(java.util.Map)

Example 47 with BeetlException

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

the class NativeCallExpression method evaluate.

public Object evaluate(Context ctx) {
    Class targetCls = null;
    Object targetObj = null;
    NativeNode lastNode = null;
    if (insNode != null) {
        targetObj = insNode.ref.evaluate(ctx);
        if (targetObj != null) {
            targetCls = targetObj.getClass();
        }
        lastNode = insNode;
    } else {
        targetCls = ctx.gt.loadClassBySimpleName(this.clsNode.cls);
        if (targetCls == null) {
            BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "该类不存在");
            be.pushToken(GrammarToken.createToken(clsNode.cls, token.line));
            throw be;
        }
        lastNode = clsNode;
    }
    for (NativeNode node : chain) {
        if (node instanceof NativeAtrributeNode) {
            String attr = ((NativeAtrributeNode) node).attribute;
            try {
                checkNull(targetCls, lastNode);
                Field f = targetCls.getField(attr);
                if (!Modifier.isStatic(f.getModifiers())) {
                    checkNull(targetObj, lastNode);
                }
                targetObj = f.get(targetObj);
                targetCls = f.getType();
            } 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;
            } catch (IllegalArgumentException e) {
                BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "访问属性出错", e);
                be.pushToken(GrammarToken.createToken(attr, token.line));
                throw be;
            } catch (IllegalAccessException e) {
                BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "访问属性出错", e);
                be.pushToken(GrammarToken.createToken(attr, token.line));
                throw be;
            }
        } else if (node instanceof NativeArrayNode) {
            checkNull(targetCls, lastNode);
            if (!targetCls.isArray()) {
                BeetlException be = new BeetlException(BeetlException.ARRAY_TYPE_ERROR);
                // 最好是把上一个字符显示出来
                be.pushToken(GrammarToken.createToken("[]", token.line));
                throw be;
            }
            Expression exp = ((NativeArrayNode) node).exp;
            Object value = exp.evaluate(ctx);
            if (value instanceof Number) {
                int index = ((Number) value).intValue();
                targetObj = ((Object[]) targetObj)[index];
                if (targetObj != null) {
                    targetCls = targetObj.getClass();
                } else {
                    // todo or component of array
                    targetCls = null;
                }
            } else {
                BeetlException be = new BeetlException(BeetlException.ARRAY_INDEX_ERROR, "数组指针必须是Number类型");
                be.pushToken(GrammarToken.createToken("[]", token.line));
                throw be;
            }
        } else if (node instanceof NativeMethodNode) {
            NativeMethodNode methodNode = (NativeMethodNode) node;
            String method = methodNode.method;
            Expression[] expList = methodNode.paras;
            this.checkPermit(ctx, targetCls, targetObj, method);
            Object[] args = expList.length == 0 ? ObjectUtil.EMPTY_OBJECT_ARRAY : new Object[expList.length];
            Class[] parameterType = new Class[args.length];
            for (int i = 0; i < expList.length; i++) {
                args[i] = expList[i].evaluate(ctx);
                parameterType[i] = args[i] == null ? null : args[i].getClass();
            }
            this.checkNull(targetCls, lastNode);
            ObjectMethodMatchConf mf = ObjectUtil.findMethod(targetCls, method, parameterType);
            if (mf == null) {
                BeetlException ex = new BeetlException(BeetlParserException.NATIVE_CALL_INVALID, "根据参数未找到匹配的方法" + method + BeetlUtil.getParameterDescription(parameterType));
                ex.pushToken(GrammarToken.createToken(lastNode.getName(), token.line));
                throw ex;
            }
            if (targetObj == null && !Modifier.isStatic(mf.method.getModifiers())) {
                BeetlException ex = new BeetlException(BeetlException.NULL);
                ex.pushToken(GrammarToken.createToken(lastNode.getName(), token.line));
                throw ex;
            }
            try {
                targetObj = ObjectUtil.invoke(targetObj, mf, args);
                if (targetObj != null) {
                    targetCls = targetObj.getClass();
                } else {
                    targetCls = null;
                }
            } catch (SecurityException e) {
                BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "不能调用方法", e);
                be.pushToken(GrammarToken.createToken(method, token.line));
                throw be;
            } catch (IllegalArgumentException e) {
                BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "错误的参数", e);
                be.pushToken(GrammarToken.createToken(method, token.line));
                throw be;
            } catch (IllegalAccessException e) {
                BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "无法访问方法", e);
                be.pushToken(GrammarToken.createToken(method, token.line));
                throw be;
            } catch (InvocationTargetException e) {
                BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "内部调用报错", e.getTargetException());
                be.pushToken(GrammarToken.createToken(method, token.line));
                throw be;
            }
        }
        lastNode = node;
    }
    return targetObj;
}
Also used : NativeArrayNode(org.beetl.core.statement.nat.NativeArrayNode) BeetlException(org.beetl.core.exception.BeetlException) NativeNode(org.beetl.core.statement.nat.NativeNode) InvocationTargetException(java.lang.reflect.InvocationTargetException) 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 48 with BeetlException

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

the class ProgramMetaData method getAjax.

public AjaxStatement getAjax(String anchor) {
    if (ajaxs == null) {
        BeetlException be = new BeetlException(BeetlException.AJAX_NOT_FOUND, "该模板文件没有发现任何ajax锚点");
        be.pushToken(new GrammarToken(anchor, 0, 0));
        throw be;
    }
    return ajaxs.get(anchor);
}
Also used : BeetlException(org.beetl.core.exception.BeetlException)

Example 49 with BeetlException

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

the class BeetlAntlrErrorStrategy method reportUnwantedToken.

protected void reportUnwantedToken(@NotNull Parser recognizer) {
    if (inErrorRecoveryMode(recognizer)) {
        return;
    }
    beginErrorCondition(recognizer);
    Token t = recognizer.getCurrentToken();
    String tokenName = getTokenErrorDisplay(t);
    IntervalSet expecting = getExpectedTokens(recognizer);
    String msg = "多余输入 " + tokenName + " 期望 " + expecting.toString(recognizer.getTokenNames());
    BeetlException exception = new BeetlParserException(BeetlException.PARSER_MISS_ERROR, msg);
    // exception.token = this.getGrammarToken(t);
    exception.pushToken(this.getGrammarToken(t));
    throw exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlParserException(org.beetl.core.exception.BeetlParserException) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) GrammarToken(org.beetl.core.statement.GrammarToken) Token(org.antlr.v4.runtime.Token)

Example 50 with BeetlException

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

the class BeetlAntlrErrorStrategy method reportFailedPredicate.

protected void reportFailedPredicate(@NotNull Parser recognizer, @NotNull FailedPredicateException e) {
    String ruleName = recognizer.getRuleNames()[recognizer.getContext().getRuleIndex()];
    BeetlException exception = new BeetlParserException(BeetlException.PARSER_PREDICATE_ERROR, ruleName, e);
    // exception.token = this.getGrammarToken(e.getOffendingToken());
    exception.pushToken(this.getGrammarToken(e.getOffendingToken()));
    throw exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlParserException(org.beetl.core.exception.BeetlParserException)

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