Search in sources :

Example 21 with CompileException

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

the class CoreConfidenceTests method testStrictTypingCompilation.

public void testStrictTypingCompilation() {
    // OptimizerFactory.setDefaultOptimizer(OptimizerFactory.DYNAMIC);
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ExpressionCompiler compiler = new ExpressionCompiler("a.foo;\nb.foo;\n x = 5", ctx);
    try {
        compiler.compile();
    } catch (CompileException e) {
        e.printStackTrace();
        return;
    }
    assertTrue(false);
// OptimizerFactory.setDefaultOptimizer(OptimizerFactory.DYNAMIC);
}
Also used : CompileException(org.mvel2.CompileException) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext)

Example 22 with CompileException

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

the class ReflectiveAccessorOptimizer method compileGetChain.

private Accessor compileGetChain() {
    Object curr = ctx;
    cursor = start;
    try {
        if (!MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING) {
            while (cursor < end) {
                switch(nextSubToken()) {
                    case BEAN:
                        curr = getBeanProperty(curr, capture());
                        break;
                    case METH:
                        curr = getMethod(curr, capture());
                        break;
                    case COL:
                        curr = getCollectionProperty(curr, capture());
                        break;
                    case WITH:
                        curr = getWithProperty(curr);
                        break;
                    case DONE:
                        break;
                }
                first = false;
                if (curr != null)
                    returnType = curr.getClass();
                if (cursor < end) {
                    if (nullSafe) {
                        int os = expr[cursor] == '.' ? 1 : 0;
                        addAccessorNode(new NullSafe(expr, cursor + os, length - cursor - os, pCtx));
                        if (curr == null)
                            break;
                    }
                    if (curr == null)
                        throw new NullPointerException();
                }
                staticAccess = false;
            }
        } else {
            while (cursor < end) {
                switch(nextSubToken()) {
                    case BEAN:
                        curr = getBeanPropertyAO(curr, capture());
                        break;
                    case METH:
                        curr = getMethod(curr, capture());
                        break;
                    case COL:
                        curr = getCollectionPropertyAO(curr, capture());
                        break;
                    case WITH:
                        curr = getWithProperty(curr);
                        break;
                    case DONE:
                        break;
                }
                first = false;
                if (curr != null)
                    returnType = curr.getClass();
                if (cursor < end) {
                    if (nullSafe) {
                        int os = expr[cursor] == '.' ? 1 : 0;
                        addAccessorNode(new NullSafe(expr, cursor + os, length - cursor - os, pCtx));
                        if (curr == null)
                            break;
                    }
                    if (curr == null)
                        throw new NullPointerException();
                }
                staticAccess = false;
            }
        }
        val = curr;
        return rootNode;
    } catch (InvocationTargetException e) {
        if (MVEL.INVOKED_METHOD_EXCEPTIONS_BUBBLE) {
            if (e.getTargetException() instanceof RuntimeException) {
                throw (RuntimeException) e.getTargetException();
            } else {
                throw new RuntimeException(e);
            }
        }
        throw new PropertyAccessException(new String(expr, start, length) + ": " + e.getTargetException().getMessage(), this.expr, this.st, e, pCtx);
    } catch (IllegalAccessException e) {
        throw new PropertyAccessException(new String(expr, start, length) + ": " + e.getMessage(), this.expr, this.st, e, pCtx);
    } catch (IndexOutOfBoundsException e) {
        throw new PropertyAccessException(new String(expr, start, length) + ": array index out of bounds.", this.expr, this.st, e, pCtx);
    } catch (CompileException e) {
        throw e;
    } catch (NullPointerException e) {
        throw new PropertyAccessException("null pointer: " + new String(expr, start, length), this.expr, this.st, e, pCtx);
    } catch (Exception e) {
        e.printStackTrace();
        throw new CompileException(e.getMessage(), this.expr, st, e);
    }
}
Also used : PropertyAccessException(org.mvel2.PropertyAccessException) CompileException(org.mvel2.CompileException) NullSafe(org.mvel2.optimizers.impl.refl.nodes.NullSafe) InvocationTargetException(java.lang.reflect.InvocationTargetException) CompileException(org.mvel2.CompileException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PropertyAccessException(org.mvel2.PropertyAccessException)

Example 23 with CompileException

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

the class TemplateCompiler method compileFrom.

public Node compileFrom(Node root, ExecutionStack stack) {
    line = 1;
    Node n = root;
    if (root == null) {
        n = root = new TextNode(0, 0);
    }
    IfNode last;
    Integer opcode;
    String name;
    int x;
    try {
        while (cursor < length) {
            switch(template[cursor]) {
                case '\n':
                    line++;
                    colStart = cursor + 1;
                    break;
                case '@':
                case '$':
                    if (isNext(template[cursor])) {
                        start = ++cursor;
                        (n = markTextNode(n)).setEnd(n.getEnd() + 1);
                        start = lastTextRangeEnding = ++cursor;
                        continue;
                    }
                    if ((x = captureOrbToken()) != -1) {
                        start = x;
                        switch((opcode = OPCODES.get(name = new String(capture()))) == null ? 0 : opcode) {
                            case Opcodes.IF:
                                /**
                                 * Capture any residual text node, and push the if statement on the nesting stack.
                                 */
                                stack.push(n = markTextNode(n).next = codeCache ? new CompiledIfNode(start, name, template, captureOrbInternal(), start, parserContext) : new IfNode(start, name, template, captureOrbInternal(), start));
                                n.setTerminus(new TerminalNode());
                                break;
                            case Opcodes.ELSE:
                                if (!stack.isEmpty() && stack.peek() instanceof IfNode) {
                                    markTextNode(n).next = (last = (IfNode) stack.pop()).getTerminus();
                                    last.demarcate(last.getTerminus(), template);
                                    last.next = n = codeCache ? new CompiledIfNode(start, name, template, captureOrbInternal(), start, parserContext) : new IfNode(start, name, template, captureOrbInternal(), start);
                                    n.setTerminus(last.getTerminus());
                                    stack.push(n);
                                }
                                break;
                            case Opcodes.FOREACH:
                                stack.push(n = markTextNode(n).next = codeCache ? new CompiledForEachNode(start, name, template, captureOrbInternal(), start, parserContext) : new ForEachNode(start, name, template, captureOrbInternal(), start));
                                n.setTerminus(new TerminalNode());
                                break;
                            case Opcodes.INCLUDE_FILE:
                                n = markTextNode(n).next = codeCache ? new CompiledIncludeNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new IncludeNode(start, name, template, captureOrbInternal(), start = cursor + 1);
                                break;
                            case Opcodes.INCLUDE_NAMED:
                                n = markTextNode(n).next = codeCache ? new CompiledNamedIncludeNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new NamedIncludeNode(start, name, template, captureOrbInternal(), start = cursor + 1);
                                break;
                            case Opcodes.CODE:
                                n = markTextNode(n).next = codeCache ? new CompiledCodeNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new CodeNode(start, name, template, captureOrbInternal(), start = cursor + 1);
                                break;
                            case Opcodes.EVAL:
                                n = markTextNode(n).next = codeCache ? new CompiledEvalNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new EvalNode(start, name, template, captureOrbInternal(), start = cursor + 1);
                                break;
                            case Opcodes.COMMENT:
                                n = markTextNode(n).next = new CommentNode(start, name, template, captureOrbInternal(), start = cursor + 1);
                                break;
                            case Opcodes.DECLARE:
                                stack.push(n = markTextNode(n).next = codeCache ? new CompiledDeclareNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new DeclareNode(start, name, template, captureOrbInternal(), start = cursor + 1));
                                n.setTerminus(new TerminalNode());
                                break;
                            case Opcodes.END:
                                n = markTextNode(n);
                                Node end = (Node) stack.pop();
                                Node terminal = end.getTerminus();
                                terminal.setCStart(captureOrbInternal());
                                terminal.setEnd((lastTextRangeEnding = start) - 1);
                                terminal.calculateContents(template);
                                if (end.demarcate(terminal, template))
                                    n = n.next = terminal;
                                else
                                    n = terminal;
                                break;
                            default:
                                if (name.length() == 0) {
                                    n = markTextNode(n).next = codeCache ? new CompiledExpressionNode(start, name, template, captureOrbInternal(), start = cursor + 1, parserContext) : new ExpressionNode(start, name, template, captureOrbInternal(), start = cursor + 1);
                                } else if (customNodes != null && customNodes.containsKey(name)) {
                                    Class<? extends Node> customNode = customNodes.get(name);
                                    try {
                                        (n = markTextNode(n).next = (customNode.newInstance())).setBegin(start);
                                        n.setName(name);
                                        n.setCStart(captureOrbInternal());
                                        n.setCEnd(start = cursor + 1);
                                        n.setEnd(n.getCEnd());
                                        n.setContents(subset(template, n.getCStart(), n.getCEnd() - n.getCStart() - 1));
                                        if (n.isOpenNode()) {
                                            stack.push(n);
                                        }
                                    } catch (InstantiationException e) {
                                        throw new RuntimeException("unable to instantiate custom node class: " + customNode.getName());
                                    } catch (IllegalAccessException e) {
                                        throw new RuntimeException("unable to instantiate custom node class: " + customNode.getName());
                                    }
                                } else {
                                    throw new RuntimeException("unknown token type: " + name);
                                }
                        }
                    }
                    break;
            }
            cursor++;
        }
    } catch (RuntimeException e) {
        CompileException ce = new CompileException(e.getMessage(), template, cursor, e);
        ce.setExpr(template);
        if (e instanceof CompileException) {
            CompileException ce2 = (CompileException) e;
            if (ce2.getCursor() != -1) {
                ce.setCursor(ce2.getCursor());
                if (ce2.getColumn() == -1)
                    ce.setColumn(ce.getCursor() - colStart);
                else
                    ce.setColumn(ce2.getColumn());
            }
        }
        ce.setLineNumber(line);
        throw ce;
    }
    if (!stack.isEmpty()) {
        CompileException ce = new CompileException("unclosed @" + ((Node) stack.peek()).getName() + "{} block. expected @end{}", template, cursor);
        ce.setColumn(cursor - colStart);
        ce.setLineNumber(line);
        throw ce;
    }
    if (start < template.length) {
        n = n.next = new TextNode(start, template.length);
    }
    n.next = new EndNode();
    n = root;
    do {
        if (n.getLength() != 0) {
            break;
        }
    } while ((n = n.getNext()) != null);
    if (n != null && n.getLength() == template.length - 1) {
        if (n instanceof ExpressionNode) {
            return codeCache ? new CompiledTerminalExpressionNode(n, parserContext) : new TerminalExpressionNode(n);
        } else {
            return n;
        }
    }
    return root;
}
Also used : CompileException(org.mvel2.CompileException)

Example 24 with CompileException

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

the class TemplateCompiler method captureOrbInternal.

private int captureOrbInternal() {
    try {
        ParserContext pCtx = new ParserContext();
        cursor = balancedCaptureWithLineAccounting(template, start = cursor, length, '{', pCtx);
        line += pCtx.getLineCount();
        int ret = start + 1;
        start = cursor + 1;
        return ret;
    } catch (CompileException e) {
        e.setLineNumber(line);
        e.setColumn((cursor - colStart) + 1);
        throw e;
    }
}
Also used : CompileException(org.mvel2.CompileException) ParserContext(org.mvel2.ParserContext)

Example 25 with CompileException

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

the class FunctionParser method parse.

public Function parse() {
    int start = cursor;
    int startCond = 0;
    int endCond = 0;
    int blockStart;
    int blockEnd;
    int end = cursor + length;
    cursor = ParseTools.captureToNextTokenJunction(expr, cursor, end, pCtx);
    if (expr[cursor = ParseTools.nextNonBlank(expr, cursor)] == '(') {
        /**
         * If we discover an opening bracket after the function name, we check to see
         * if this function accepts parameters.
         */
        endCond = cursor = balancedCaptureWithLineAccounting(expr, startCond = cursor, end, '(', pCtx);
        startCond++;
        cursor++;
        cursor = ParseTools.skipWhitespace(expr, cursor);
        if (cursor >= end) {
            throw new CompileException("incomplete statement", expr, cursor);
        } else if (expr[cursor] == '{') {
            blockEnd = cursor = balancedCaptureWithLineAccounting(expr, blockStart = cursor, end, '{', pCtx);
        } else {
            blockStart = cursor - 1;
            cursor = ParseTools.captureToEOS(expr, cursor, end, pCtx);
            blockEnd = cursor;
        }
    } else {
        /**
         * This function has not parameters.
         */
        if (expr[cursor] == '{') {
            /**
             * This function is bracketed.  We capture the entire range in the brackets.
             */
            blockEnd = cursor = balancedCaptureWithLineAccounting(expr, blockStart = cursor, end, '{', pCtx);
        } else {
            /**
             * This is a single statement function declaration.  We only capture the statement.
             */
            blockStart = cursor - 1;
            cursor = ParseTools.captureToEOS(expr, cursor, end, pCtx);
            blockEnd = cursor;
        }
    }
    /**
     * Trim any whitespace from the captured block range.
     */
    blockStart = ParseTools.trimRight(expr, blockStart + 1);
    blockEnd = ParseTools.trimLeft(expr, start, blockEnd);
    cursor++;
    /**
     * Check if the function is manually terminated.
     */
    if (splitAccumulator != null && ParseTools.isStatementNotManuallyTerminated(expr, cursor)) {
        /**
         * Add an EndOfStatement to the split accumulator in the parser.
         */
        splitAccumulator.add(new EndOfStatement(pCtx));
    }
    /**
     * Produce the funciton node.
     */
    return new Function(name, expr, startCond, endCond - startCond, blockStart, blockEnd - blockStart, fields, pCtx);
}
Also used : Function(org.mvel2.ast.Function) EndOfStatement(org.mvel2.ast.EndOfStatement) 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