Search in sources :

Example 46 with CompileException

use of org.mule.mvel2.CompileException in project mvel by mvel.

the class PropertyAccessTests method infiniteLoop.

private void infiniteLoop() {
    try {
        Serializable compiled = MVEL.compileExpression("a['b']['c']");
        Map<String, Object> vars = Collections.singletonMap("a", (Object) Collections.emptyMap());
        MVEL.executeExpression(compiled, vars);
        fail("expected exception");
    } catch (CompileException t) {
    }
}
Also used : Serializable(java.io.Serializable) CompileException(org.mvel2.CompileException)

Example 47 with CompileException

use of org.mule.mvel2.CompileException in project mvel by mvel.

the class ReflectiveAccessorOptimizer method optimizeObjectCreation.

public Accessor optimizeObjectCreation(ParserContext pCtx, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) {
    this.length = start + offset;
    this.cursor = this.start = start;
    this.pCtx = pCtx;
    try {
        return compileConstructor(property, ctx, factory);
    } catch (CompileException e) {
        throw ErrorUtil.rewriteIfNeeded(e, property, this.start);
    } catch (ClassNotFoundException e) {
        throw new CompileException("could not resolve class: " + e.getMessage(), property, this.start, e);
    } catch (Exception e) {
        throw new CompileException("could not create constructor: " + e.getMessage(), property, this.start, e);
    }
}
Also used : CompileException(org.mvel2.CompileException) CompileException(org.mvel2.CompileException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PropertyAccessException(org.mvel2.PropertyAccessException)

Example 48 with CompileException

use of org.mule.mvel2.CompileException in project mvel by mvel.

the class CollectionParser method parseCollection.

private Object parseCollection(boolean subcompile) {
    if (end - start == 0) {
        if (type == LIST)
            return new ArrayList();
        else
            return EMPTY_ARRAY;
    }
    Map<Object, Object> map = null;
    List<Object> list = null;
    int st = start;
    if (type != -1) {
        switch(type) {
            case ARRAY:
            case LIST:
                list = new ArrayList<Object>();
                break;
            case MAP:
                map = new HashMap<Object, Object>();
                break;
        }
    }
    Object curr = null;
    int newType = -1;
    for (; cursor < end; cursor++) {
        switch(property[cursor]) {
            case '{':
                if (newType == -1) {
                    newType = ARRAY;
                }
            case '[':
                if (cursor > start && isIdentifierPart(property[cursor - 1]))
                    continue;
                if (newType == -1) {
                    newType = LIST;
                }
                /**
                 * Sub-parse nested collections.
                 */
                Object o = new CollectionParser(newType).parseCollection(property, (st = cursor) + 1, (cursor = balancedCapture(property, st, end, property[st])) - st - 1, subcompile, colType, pCtx);
                if (type == MAP) {
                    map.put(curr, o);
                } else {
                    list.add(curr = o);
                }
                cursor = skipWhitespace(property, ++cursor);
                if ((st = cursor) < end && property[cursor] == ',') {
                    st = cursor + 1;
                } else if (cursor < end) {
                    if (ParseTools.opLookup(property[cursor]) == -1) {
                        throw new CompileException("unterminated collection element", property, cursor);
                    }
                }
                continue;
            case '(':
                cursor = balancedCapture(property, cursor, end, '(');
                break;
            case '\"':
            case '\'':
                cursor = balancedCapture(property, cursor, end, property[cursor]);
                break;
            case ',':
                if (type != MAP) {
                    list.add(new String(property, st, cursor - st).trim());
                } else {
                    map.put(curr, createStringTrimmed(property, st, cursor - st));
                }
                if (subcompile) {
                    subCompile(st, cursor - st);
                }
                st = cursor + 1;
                break;
            case ':':
                if (type != MAP) {
                    map = new HashMap<Object, Object>();
                    type = MAP;
                }
                curr = createStringTrimmed(property, st, cursor - st);
                if (subcompile) {
                    subCompile(st, cursor - st);
                }
                st = cursor + 1;
                break;
            case '.':
                cursor++;
                cursor = skipWhitespace(property, cursor);
                if (cursor != end && property[cursor] == '{') {
                    cursor = balancedCapture(property, cursor, '{');
                }
                break;
        }
    }
    if (st < end && isWhitespace(property[st])) {
        st = skipWhitespace(property, st);
    }
    if (st < end) {
        if (cursor < (end - 1))
            cursor++;
        if (type == MAP) {
            map.put(curr, createStringTrimmed(property, st, cursor - st));
        } else {
            if (cursor < end)
                cursor++;
            list.add(createStringTrimmed(property, st, cursor - st));
        }
        if (subcompile)
            subCompile(st, cursor - st);
    }
    switch(type) {
        case MAP:
            return map;
        case ARRAY:
            return list.toArray();
        default:
            return list;
    }
}
Also used : ArrayList(java.util.ArrayList) CompileException(org.mvel2.CompileException)

Example 49 with CompileException

use of org.mule.mvel2.CompileException in project mvel by mvel.

the class CompilerTools method finalizePayload.

/**
 * Finalize the payload, by reducing any stack-based-operations to dedicated nodes where possible.
 *
 * @param astLinkedList          - AST to be optimized.
 * @param secondPassOptimization - perform a second pass optimization to optimize boolean expressions.
 * @param pCtx                    - The parser context
 * @return optimized AST
 */
public static ASTLinkedList finalizePayload(ASTLinkedList astLinkedList, boolean secondPassOptimization, ParserContext pCtx) {
    ASTLinkedList optimizedAst = new ASTLinkedList();
    ASTNode tk, tkOp, tkOp2;
    /**
     * Re-process the AST and optimize it.
     */
    while (astLinkedList.hasMoreNodes()) {
        if ((tk = astLinkedList.nextNode()).getFields() == -1) {
            optimizedAst.addTokenNode(tk);
        } else if (astLinkedList.hasMoreNodes()) {
            if ((tkOp = astLinkedList.nextNode()).getFields() == -1) {
                optimizedAst.addTokenNode(tk, tkOp);
            } else if (tkOp.isOperator() && tkOp.getOperator() < 21) {
                int op = tkOp.getOperator();
                int op2;
                if (op == -1) {
                    throw new CompileException("illegal use of operator: " + tkOp.getName(), tkOp.getExpr(), tk.getStart());
                }
                ASTNode tk2 = astLinkedList.nextNode();
                BinaryOperation bo;
                if (tk.getEgressType() == Integer.class && tk2.getEgressType() == Integer.class) {
                    bo = boOptimize(op, tk, tk2, pCtx);
                } else {
                    /**
                     * Let's see if we can simply the expression more.
                     */
                    bo = null;
                    boolean inv = tkOp.isOperator(Operator.SUB);
                    boolean reduc = isReductionOpportunity(tkOp, tk2);
                    boolean p_inv = false;
                    while (reduc) {
                        ASTNode oper = astLinkedList.nextNode();
                        ASTNode rightNode = astLinkedList.nextNode();
                        if (rightNode == null)
                            break;
                        Object val = new BinaryOperation(oper.getOperator(), inv ? new LiteralNode(signNumber(tk2.getLiteralValue()), pCtx) : tk2, rightNode, pCtx).getReducedValueAccelerated(null, null, null);
                        if (!astLinkedList.hasMoreNodes() && BlankLiteral.INSTANCE.equals(val)) {
                            optimizedAst.addTokenNode(tk);
                            continue;
                        }
                        reduc = astLinkedList.hasMoreNodes() && (reducacbleOperator(astLinkedList.peekNode().getOperator())) && astLinkedList.peekNext().isLiteral();
                        if (inv)
                            p_inv = true;
                        inv = false;
                        if (!reduc) {
                            bo = new BinaryOperation(tkOp.getOperator(), tk, new LiteralNode(p_inv ? signNumber(val) : val, pCtx), pCtx);
                        } else {
                            tk2 = new LiteralNode(val, pCtx);
                        }
                    }
                    if (bo == null)
                        bo = new BinaryOperation(op, tk, tk2, pCtx);
                }
                tkOp2 = null;
                /**
                 * If we have a chain of math/comparitive operators then we fill them into the tree
                 * right here.
                 */
                while (astLinkedList.hasMoreNodes() && (tkOp2 = astLinkedList.nextNode()).isOperator() && tkOp2.getFields() != -1 && (op2 = tkOp2.getOperator()) != -1 && op2 < 21) {
                    if (PTABLE[op2] > PTABLE[op]) {
                        // bo.setRightMost(new BinaryOperation(op2, bo.getRightMost(), astLinkedList.nextNode(), pCtx));
                        bo.setRightMost(boOptimize(op2, bo.getRightMost(), astLinkedList.nextNode(), pCtx));
                    } else if (bo.getOperation() != op2 && PTABLE[op] == PTABLE[op2]) {
                        if (PTABLE[bo.getOperation()] == PTABLE[op2]) {
                            // bo = new BinaryOperation(op2, bo, astLinkedList.nextNode(), pCtx);
                            bo = boOptimize(op2, bo, astLinkedList.nextNode(), pCtx);
                        } else {
                            tk2 = astLinkedList.nextNode();
                            if (isIntOptimizationviolation(bo, tk2)) {
                                bo = new BinaryOperation(bo.getOperation(), bo.getLeft(), bo.getRight(), pCtx);
                            }
                            bo.setRight(new BinaryOperation(op2, bo.getRight(), tk2, pCtx));
                        }
                    } else if (PTABLE[bo.getOperation()] >= PTABLE[op2]) {
                        bo = new BinaryOperation(op2, bo, astLinkedList.nextNode(), pCtx);
                    } else {
                        tk2 = astLinkedList.nextNode();
                        if (isIntOptimizationviolation(bo, tk2)) {
                            bo = new BinaryOperation(bo.getOperation(), bo.getLeft(), bo.getRight(), pCtx);
                        }
                        bo.setRight(new BinaryOperation(op2, bo.getRight(), tk2, pCtx));
                    }
                    op = op2;
                    tkOp = tkOp2;
                }
                if (tkOp2 != null && tkOp2 != tkOp) {
                    optimizeOperator(tkOp2.getOperator(), bo, tkOp2, astLinkedList, optimizedAst, pCtx);
                } else {
                    optimizedAst.addTokenNode(bo);
                }
            } else if (tkOp.isOperator()) {
                optimizeOperator(tkOp.getOperator(), tk, tkOp, astLinkedList, optimizedAst, pCtx);
            } else if (!tkOp.isAssignment() && !tkOp.isOperator() && tk.getLiteralValue() instanceof Class) {
                optimizedAst.addTokenNode(new DeclTypedVarNode(tkOp.getName(), tkOp.getExpr(), tkOp.getStart(), tk.getOffset(), (Class) tk.getLiteralValue(), 0, pCtx));
            } else if (tkOp.isAssignment() && tk.getLiteralValue() instanceof Class) {
                tk.discard();
                optimizedAst.addTokenNode(tkOp);
            } else if (astLinkedList.hasMoreNodes() && tkOp.getLiteralValue() instanceof Class && astLinkedList.peekNode().isAssignment()) {
                tkOp.discard();
                optimizedAst.addTokenNode(tk, astLinkedList.nextNode());
            } else {
                astLinkedList.back();
                optimizedAst.addTokenNode(tk);
            }
        } else {
            optimizedAst.addTokenNode(tk);
        }
    }
    if (secondPassOptimization) {
        /**
         * Perform a second pass optimization for boolean conditions.
         */
        (astLinkedList = optimizedAst).reset();
        optimizedAst = new ASTLinkedList();
        while (astLinkedList.hasMoreNodes()) {
            if ((tk = astLinkedList.nextNode()).getFields() == -1) {
                optimizedAst.addTokenNode(tk);
            } else if (astLinkedList.hasMoreNodes()) {
                if ((tkOp = astLinkedList.nextNode()).getFields() == -1) {
                    optimizedAst.addTokenNode(tk, tkOp);
                } else if (tkOp.isOperator() && (tkOp.getOperator() == Operator.AND || tkOp.getOperator() == Operator.OR)) {
                    tkOp2 = null;
                    BooleanNode bool;
                    if (tkOp.getOperator() == Operator.AND) {
                        bool = new And(tk, astLinkedList.nextNode(), pCtx.isStrongTyping(), pCtx);
                    } else {
                        bool = new Or(tk, astLinkedList.nextNode(), pCtx.isStrongTyping(), pCtx);
                    }
                    while (astLinkedList.hasMoreNodes() && (tkOp2 = astLinkedList.nextNode()).isOperator() && (tkOp2.isOperator(Operator.AND) || tkOp2.isOperator(Operator.OR))) {
                        if ((tkOp = tkOp2).getOperator() == Operator.AND) {
                            bool.setRightMost(new And(bool.getRightMost(), astLinkedList.nextNode(), pCtx.isStrongTyping(), pCtx));
                        } else {
                            bool = new Or(bool, astLinkedList.nextNode(), pCtx.isStrongTyping(), pCtx);
                        }
                    }
                    optimizedAst.addTokenNode(bool);
                    if (tkOp2 != null && tkOp2 != tkOp) {
                        optimizedAst.addTokenNode(tkOp2);
                    }
                } else {
                    optimizedAst.addTokenNode(tk, tkOp);
                }
            } else {
                optimizedAst.addTokenNode(tk);
            }
        }
    }
    return optimizedAst;
}
Also used : Or(org.mvel2.ast.Or) BinaryOperation(org.mvel2.ast.BinaryOperation) BooleanNode(org.mvel2.ast.BooleanNode) LiteralNode(org.mvel2.ast.LiteralNode) And(org.mvel2.ast.And) DeclTypedVarNode(org.mvel2.ast.DeclTypedVarNode) ASTNode(org.mvel2.ast.ASTNode) CompileException(org.mvel2.CompileException)

Example 50 with CompileException

use of org.mule.mvel2.CompileException in project mvel by mvel.

the class ParseTools method isNumber.

public static boolean isNumber(char[] val, int start, int offset) {
    char c;
    boolean f = true;
    int i = start;
    int end = start + offset;
    if (offset > 1) {
        switch(val[start]) {
            case '-':
                if (val[start + 1] == '-')
                    i++;
            case '~':
                i++;
        }
    }
    for (; i < end; i++) {
        if (!isDigit(c = val[i])) {
            if (f && c == '.') {
                f = false;
            } else if (offset != 1 && i == start + offset - 1) {
                switch(c) {
                    case 'l':
                    case 'L':
                    case 'f':
                    case 'F':
                    case 'd':
                    case 'D':
                    case 'I':
                    case 'B':
                        return true;
                    case '.':
                        throw new CompileException("invalid number literal: " + new String(val), val, start);
                }
                return false;
            } else if (i == start + 1 && c == 'x' && val[start] == '0') {
                for (i++; i < end; i++) {
                    if (!isDigit(c = val[i])) {
                        if ((c < 'A' || c > 'F') && (c < 'a' || c > 'f')) {
                            if (i == offset - 1) {
                                switch(c) {
                                    case 'l':
                                    case 'L':
                                    case 'I':
                                    case 'B':
                                        return true;
                                }
                            }
                            return false;
                        }
                    }
                }
                return offset - 2 > 0;
            } else if (i != start && (i + 1) < end && (c == 'E' || c == 'e')) {
                if (val[++i] == '-' || val[i] == '+')
                    i++;
            } else {
                if (i != start)
                    throw new CompileException("invalid number literal: " + new String(val, start, offset), val, start);
                return false;
            }
        }
    }
    return end > start;
}
Also used : CompileException(org.mvel2.CompileException)

Aggregations

CompileException (org.mvel2.CompileException)83 ParserContext (org.mvel2.ParserContext)14 Map (java.util.Map)13 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)13 PropertyAccessException (org.mvel2.PropertyAccessException)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)11 List (java.util.List)11 ArrayList (java.util.ArrayList)10 IOException (java.io.IOException)9 Method (java.lang.reflect.Method)7 TypeDescriptor (org.mvel2.ast.TypeDescriptor)7 Field (java.lang.reflect.Field)6 HashMap (java.util.HashMap)6 EndOfStatement (org.mvel2.ast.EndOfStatement)6 Proto (org.mvel2.ast.Proto)6 Constructor (java.lang.reflect.Constructor)5 Member (java.lang.reflect.Member)5 ASTNode (org.mvel2.ast.ASTNode)5 LiteralNode (org.mvel2.ast.LiteralNode)5 Collection (java.util.Collection)4