Search in sources :

Example 26 with CompileException

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

the class ProtoParser method parse.

public Proto parse() {
    Proto proto = new Proto(protoName, pCtx);
    Mainloop: while (cursor < endOffset) {
        cursor = ParseTools.skipWhitespace(expr, cursor);
        int start = cursor;
        if (tk2 == null) {
            while (cursor < endOffset && isIdentifierPart(expr[cursor])) cursor++;
            if (cursor > start) {
                tk1 = new String(expr, start, cursor - start);
                if ("def".equals(tk1) || "function".equals(tk1)) {
                    cursor++;
                    cursor = ParseTools.skipWhitespace(expr, cursor);
                    start = cursor;
                    while (cursor < endOffset && isIdentifierPart(expr[cursor])) cursor++;
                    if (start == cursor) {
                        throw new CompileException("attempt to declare an anonymous function as a prototype member", expr, start);
                    }
                    FunctionParser parser = new FunctionParser(new String(expr, start, cursor - start), cursor, endOffset, expr, 0, pCtx, null);
                    proto.declareReceiver(parser.getName(), parser.parse());
                    cursor = parser.getCursor() + 1;
                    tk1 = null;
                    continue;
                }
            }
            cursor = ParseTools.skipWhitespace(expr, cursor);
        }
        if (cursor > endOffset) {
            throw new CompileException("unexpected end of statement in proto declaration: " + protoName, expr, start);
        }
        switch(expr[cursor]) {
            case ';':
                cursor++;
                calculateDecl();
                if (interpreted && type == DeferredTypeResolve.class) {
                    /**
                     * If this type could not be immediately resolved, it may be a look-ahead case, so
                     * we defer resolution of the type until later and place it in the wait queue.
                     */
                    enqueueReceiverForLateResolution(deferredName, proto.declareReceiver(name, Proto.ReceiverType.DEFERRED, null), null);
                } else {
                    proto.declareReceiver(name, type, null);
                }
                break;
            case '=':
                cursor++;
                cursor = ParseTools.skipWhitespace(expr, cursor);
                start = cursor;
                Loop: while (cursor < endOffset) {
                    switch(expr[cursor]) {
                        case '{':
                        case '[':
                        case '(':
                        case '\'':
                        case '"':
                            cursor = balancedCaptureWithLineAccounting(expr, cursor, endOffset, expr[cursor], pCtx);
                            break;
                        case ';':
                            break Loop;
                    }
                    cursor++;
                }
                calculateDecl();
                String initString = new String(expr, start, cursor++ - start);
                if (interpreted && type == DeferredTypeResolve.class) {
                    enqueueReceiverForLateResolution(deferredName, proto.declareReceiver(name, Proto.ReceiverType.DEFERRED, null), initString);
                } else {
                    proto.declareReceiver(name, type, (ExecutableStatement) subCompileExpression(initString, pCtx));
                }
                break;
            default:
                start = cursor;
                while (cursor < endOffset && isIdentifierPart(expr[cursor])) cursor++;
                if (cursor > start) {
                    tk2 = new String(expr, start, cursor - start);
                }
        }
    }
    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));
    }
    return proto;
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) Proto(org.mvel2.ast.Proto) EndOfStatement(org.mvel2.ast.EndOfStatement) CompileException(org.mvel2.CompileException)

Example 27 with CompileException

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

the class ProtoParser method calculateDecl.

private void calculateDecl() {
    if (tk2 != null) {
        try {
            if (pCtx.hasProtoImport(tk1)) {
                type = Proto.class;
            } else {
                type = ParseTools.findClass(null, tk1, pCtx);
            }
            name = tk2;
        } catch (ClassNotFoundException e) {
            if (interpreted) {
                type = DeferredTypeResolve.class;
                deferredName = tk1;
                name = tk2;
            } else {
                throw new CompileException("could not resolve class: " + tk1, expr, cursor, e);
            }
        }
    } else {
        type = Object.class;
        name = tk1;
    }
    tk1 = null;
    tk2 = null;
}
Also used : CompileException(org.mvel2.CompileException)

Example 28 with CompileException

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

the class TypesAndInferenceTests method testSetAccessorOverloadedEqualsStrictMode2.

public void testSetAccessorOverloadedEqualsStrictMode2() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("foo", Foo.class);
    try {
        CompiledExpression expr = new ExpressionCompiler("foo.aValue = 'bar'", ctx).compile();
    } catch (CompileException e) {
        assertTrue(false);
    }
}
Also used : CompileException(org.mvel2.CompileException) ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 29 with CompileException

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

the class Fuzzer method main.

public static void main(String[] args) throws IOException {
    DecimalFormat df = new DecimalFormat("###,###.##");
    StringAppender append = new StringAppender();
    int len;
    long start = currentTimeMillis();
    long time;
    double rate;
    int seed;
    boolean flip = false;
    Random rand = new Random(System.currentTimeMillis());
    Random rand1 = new Random(System.currentTimeMillis() + 1);
    Random rand2 = new Random(rand1.nextInt());
    Random rand3 = new Random(rand.nextInt(SALTS.length - 1));
    Random rand4 = new Random(rand3.nextInt());
    for (int run = 0; run < MAX; run++) {
        len = (int) (random() * 500) + 10;
        append.reset();
        for (int i = 0; i < len; i++) {
            append.append(CHAR_TABLE[((SALTS[((rand.nextInt(1000)) + 1) % SALTS.length]) * ((flip = !flip) ? rand1.nextInt(1000) : rand2.nextInt(1000)) + 1) % CHAR_TABLE.length]);
            SALTS[rand3.nextInt(SALTS.length - 1)] ^= rand4.nextInt(1000) + 1;
        }
        try {
            MVEL.eval(append.toString());
        } catch (UnresolveablePropertyException e) {
        // ignore
        } catch (CompileException e) {
        // ignore
        } catch (ArithmeticException e) {
        // ignore
        } catch (ScriptRuntimeException e) {
        // ignore
        } catch (Exception e) {
            System.out.println("untrapped error!\n---\n" + append.toString() + "\n---\n");
            System.out.flush();
            e.printStackTrace();
            System.err.flush();
        }
        if (run % 25000 == 0 && run != 0) {
            long l = time = (currentTimeMillis() - start) / 1000;
            if (l == 0) {
                l = 1;
            }
            rate = run / l;
            System.out.println("Run: " + df.format(run) + " times; " + df.format(time) + "secs; " + df.format(rate) + " avg. per second.");
        }
    }
}
Also used : Random(java.util.Random) ScriptRuntimeException(org.mvel2.ScriptRuntimeException) DecimalFormat(java.text.DecimalFormat) StringAppender(org.mvel2.util.StringAppender) CompileException(org.mvel2.CompileException) UnresolveablePropertyException(org.mvel2.UnresolveablePropertyException) UnresolveablePropertyException(org.mvel2.UnresolveablePropertyException) ScriptRuntimeException(org.mvel2.ScriptRuntimeException) IOException(java.io.IOException) CompileException(org.mvel2.CompileException)

Example 30 with CompileException

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

the class ASMAccessorOptimizer method writeLiteralOrSubexpression.

private Class writeLiteralOrSubexpression(Object stmt, Class desiredTarget, Class knownIngressType) {
    if (stmt instanceof ExecutableLiteral) {
        Object literalValue = ((ExecutableLiteral) stmt).getLiteral();
        // Handle the case when the literal is null MVEL-312
        if (literalValue == null) {
            mv.visitInsn(ACONST_NULL);
            return null;
        }
        Class type = literalValue == null ? desiredTarget : literalValue.getClass();
        assert debug("*** type:" + type + ";desired:" + desiredTarget);
        if (type == Integer.class && desiredTarget == int.class) {
            intPush(((ExecutableLiteral) stmt).getInteger32());
            type = int.class;
        } else if (desiredTarget != null && desiredTarget != type) {
            assert debug("*** Converting because desiredType(" + desiredTarget.getClass() + ") is not: " + type);
            if (!DataConversion.canConvert(type, desiredTarget)) {
                throw new CompileException("was expecting type: " + desiredTarget.getName() + "; but found type: " + type.getName(), expr, st);
            }
            writeOutLiteralWrapped(convert(literalValue, desiredTarget));
        } else {
            writeOutLiteralWrapped(literalValue);
        }
        return type;
    } else {
        literal = false;
        addSubstatement((ExecutableStatement) stmt);
        Class type;
        if (knownIngressType == null) {
            type = ((ExecutableStatement) stmt).getKnownEgressType();
        } else {
            type = knownIngressType;
        }
        if (desiredTarget != null && type != desiredTarget) {
            if (desiredTarget.isPrimitive()) {
                if (type == null)
                    throw new OptimizationFailure("cannot optimize expression: " + new String(expr) + ": cannot determine ingress type for primitive output");
                checkcast(type);
                unwrapPrimitive(desiredTarget);
            }
        }
        return type;
    }
}
Also used : ExecutableLiteral(org.mvel2.compiler.ExecutableLiteral) OptimizationFailure(org.mvel2.OptimizationFailure) 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