Search in sources :

Example 11 with CompileException

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

the class NewObjectNode method getReducedValueAccelerated.

public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
    if (newObjectOptimizer == null) {
        if (egressType == null) {
            if (factory != null && factory.isResolveable(typeDescr.getClassName())) {
                try {
                    egressType = (Class) factory.getVariableResolver(typeDescr.getClassName()).getValue();
                    rewriteClassReferenceToFQCN(COMPILE_IMMEDIATE);
                    if (typeDescr.isArray()) {
                        try {
                            egressType = findClass(factory, repeatChar('[', typeDescr.getArrayLength()) + "L" + egressType.getName() + ";", pCtx);
                        } catch (Exception e) {
                        // for now, don't handle this.
                        }
                    }
                } catch (ClassCastException e) {
                    throw new CompileException("cannot construct object: " + typeDescr.getClassName() + " is not a class reference", expr, start, e);
                }
            }
        }
        if (typeDescr.isArray()) {
            return (newObjectOptimizer = new NewObjectArray(getBaseComponentType(egressType.getComponentType()), typeDescr.getCompiledArraySize())).getValue(ctx, thisValue, factory);
        }
        try {
            AccessorOptimizer optimizer = getThreadAccessorOptimizer();
            ParserContext pCtx = this.pCtx;
            if (pCtx == null) {
                pCtx = new ParserContext();
                pCtx.getParserConfiguration().setAllImports(getInjectedImports(factory));
            }
            newObjectOptimizer = optimizer.optimizeObjectCreation(pCtx, name, 0, name.length, ctx, thisValue, factory);
            /**
             * Check to see if the optimizer actually produced the object during optimization.  If so,
             * we return that value now.
             */
            if (optimizer.getResultOptPass() != null) {
                egressType = optimizer.getEgressType();
                return optimizer.getResultOptPass();
            }
        } catch (CompileException e) {
            throw ErrorUtil.rewriteIfNeeded(e, expr, start);
        } finally {
            OptimizerFactory.clearThreadAccessorOptimizer();
        }
    }
    return newObjectOptimizer.getValue(ctx, thisValue, factory);
}
Also used : AccessorOptimizer(org.mvel2.optimizers.AccessorOptimizer) OptimizerFactory.getThreadAccessorOptimizer(org.mvel2.optimizers.OptimizerFactory.getThreadAccessorOptimizer) CompileException(org.mvel2.CompileException) ParserContext(org.mvel2.ParserContext) CompileException(org.mvel2.CompileException)

Example 12 with CompileException

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

the class Stacklang method getReducedValue.

@Override
public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) {
    ExecutionStack stack = (ExecutionStack) ctx;
    for (int i1 = 0, instructionListSize = instructionList.size(); i1 < instructionListSize; i1++) {
        Instruction instruction = instructionList.get(i1);
        System.out.println(stack.toString() + " >> " + instruction.opcode + ":" + instruction.expr);
        switch(instruction.opcode) {
            case Operator.STORE:
                if (instruction.cache == null) {
                    instruction.cache = factory.createVariable(instruction.expr, stack.peek());
                } else {
                    ((VariableResolver) instruction.cache).setValue(stack.peek());
                }
                break;
            case Operator.LOAD:
                if (instruction.cache == null) {
                    instruction.cache = factory.getVariableResolver(instruction.expr);
                }
                stack.push(((VariableResolver) instruction.cache).getValue());
                break;
            case Operator.GETFIELD:
                try {
                    if (stack.isEmpty() || !(stack.peek() instanceof Class)) {
                        throw new CompileException("getfield without class", expr, blockStart);
                    }
                    Field field;
                    if (instruction.cache == null) {
                        instruction.cache = field = ((Class) stack.pop()).getField(instruction.expr);
                    } else {
                        stack.discard();
                        field = (Field) instruction.cache;
                    }
                    stack.push(field.get(stack.pop()));
                } catch (Exception e) {
                    throw new CompileException("field access error", expr, blockStart, e);
                }
                break;
            case Operator.STOREFIELD:
                try {
                    if (stack.isEmpty() || !(stack.peek() instanceof Class)) {
                        throw new CompileException("storefield without class", expr, blockStart);
                    }
                    Class cls = (Class) stack.pop();
                    Object val = stack.pop();
                    cls.getField(instruction.expr).set(stack.pop(), val);
                    stack.push(val);
                } catch (Exception e) {
                    throw new CompileException("field access error", expr, blockStart, e);
                }
                break;
            case Operator.LDTYPE:
                try {
                    if (instruction.cache == null) {
                        instruction.cache = ParseTools.createClass(instruction.expr, pCtx);
                    }
                    stack.push(instruction.cache);
                } catch (ClassNotFoundException e) {
                    throw new CompileException("error", expr, blockStart, e);
                }
                break;
            case Operator.INVOKE:
                Object[] parms;
                ExecutionStack call = new ExecutionStack();
                while (!stack.isEmpty() && !(stack.peek() instanceof Class)) {
                    call.push(stack.pop());
                }
                if (stack.isEmpty()) {
                    throw new CompileException("invoke without class", expr, blockStart);
                }
                parms = new Object[call.size()];
                for (int i = 0; !call.isEmpty(); i++) parms[i] = call.pop();
                if ("<init>".equals(instruction.expr)) {
                    Constructor c;
                    if (instruction.cache == null) {
                        instruction.cache = c = ParseTools.getBestConstructorCandidate(parms, (Class) stack.pop(), false);
                    } else {
                        c = (Constructor) instruction.cache;
                    }
                    try {
                        stack.push(c.newInstance(parms));
                    } catch (Exception e) {
                        throw new CompileException("instantiation error", expr, blockStart, e);
                    }
                } else {
                    Method m;
                    if (instruction.cache == null) {
                        Class cls = (Class) stack.pop();
                        instruction.cache = m = ParseTools.getBestCandidate(parms, instruction.expr, cls, cls.getDeclaredMethods(), false);
                    } else {
                        stack.discard();
                        m = (Method) instruction.cache;
                    }
                    try {
                        stack.push(m.invoke(stack.isEmpty() ? null : stack.pop(), parms));
                    } catch (Exception e) {
                        throw new CompileException("invokation error", expr, blockStart, e);
                    }
                }
                break;
            case Operator.PUSH:
                if (instruction.cache == null) {
                    instruction.cache = MVEL.eval(instruction.expr, ctx, factory);
                }
                stack.push(instruction.cache);
                break;
            case Operator.POP:
                stack.pop();
                break;
            case Operator.DUP:
                stack.dup();
                break;
            case Operator.LABEL:
                break;
            case Operator.JUMPIF:
                if (!stack.popBoolean())
                    continue;
            case Operator.JUMP:
                if (instruction.cache != null) {
                    i1 = (Integer) instruction.cache;
                } else {
                    for (int i2 = 0; i2 < instructionList.size(); i2++) {
                        Instruction ins = instructionList.get(i2);
                        if (ins.opcode == Operator.LABEL && instruction.expr.equals(ins.expr)) {
                            instruction.cache = i1 = i2;
                            break;
                        }
                    }
                }
                break;
            case Operator.EQUAL:
                stack.push(stack.pop().equals(stack.pop()));
                break;
            case Operator.NEQUAL:
                stack.push(!stack.pop().equals(stack.pop()));
                break;
            case Operator.REDUCE:
                stack.op();
                break;
            case Operator.XSWAP:
                stack.xswap2();
                break;
            case Operator.SWAP:
                Object o = stack.pop();
                Object o2 = stack.pop();
                stack.push(o);
                stack.push(o2);
                break;
        }
    }
    return stack.pop();
}
Also used : Constructor(java.lang.reflect.Constructor) ExecutionStack(org.mvel2.util.ExecutionStack) Method(java.lang.reflect.Method) CompileException(org.mvel2.CompileException) Field(java.lang.reflect.Field) CompileException(org.mvel2.CompileException) VariableResolver(org.mvel2.integration.VariableResolver)

Example 13 with CompileException

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

the class Strsim method getReducedValue.

public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) {
    try {
        String i = String.valueOf(soundslike.getReducedValue(ctx, thisValue, factory));
        if (i == null)
            throw new ClassCastException();
        String x = (String) stmt.getReducedValue(ctx, thisValue, factory);
        if (x == null)
            throw new CompileException("not a string: " + stmt.getName(), stmt.getExpr(), getStart());
        return similarity(i, x);
    } catch (ClassCastException e) {
        throw new CompileException("not a string: " + soundslike.getName(), soundslike.getExpr(), soundslike.getStart());
    }
}
Also used : CompileException(org.mvel2.CompileException)

Example 14 with CompileException

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

the class AbstractParser method arithmeticFunctionReduction.

/**
 * Reduce the current operations on the stack.
 *
 * @param operator the operator
 * @return a stack control code
 */
protected int arithmeticFunctionReduction(int operator) {
    ASTNode tk;
    int operator2;
    /**
     * If the next token is an operator, we check to see if it has a higher
     * precdence.
     */
    if ((tk = nextToken()) != null) {
        if (isArithmeticOperator(operator2 = tk.getOperator()) && PTABLE[operator2] > PTABLE[operator]) {
            stk.xswap();
            /**
             * The current arith. operator is of higher precedence the last.
             */
            tk = nextToken();
            /**
             * Check to see if we're compiling or executing interpretively.  If we're compiling, we really
             * need to stop if this is not a literal.
             */
            if (compileMode && !tk.isLiteral()) {
                splitAccumulator.push(tk, new OperatorNode(operator2, expr, st, pCtx));
                return OP_OVERFLOW;
            }
            dStack.push(operator = operator2, tk.getReducedValue(ctx, ctx, variableFactory));
            while (true) {
                // look ahead again
                if ((tk = nextToken()) != null && (operator2 = tk.getOperator()) != -1 && operator2 != END_OF_STMT && PTABLE[operator2] > PTABLE[operator]) {
                    if (dStack.isReduceable()) {
                        stk.copyx2(dStack);
                    }
                    /**
                     * This operator is of higher precedence, or the same level precedence.  push to the RHS.
                     */
                    dStack.push(operator = operator2, nextToken().getReducedValue(ctx, ctx, variableFactory));
                    continue;
                } else if (tk != null && operator2 != -1 && operator2 != END_OF_STMT) {
                    if (PTABLE[operator2] == PTABLE[operator]) {
                        if (!dStack.isEmpty())
                            dreduce();
                        else {
                            while (stk.isReduceable()) {
                                stk.xswap_op();
                            }
                        }
                        /**
                         * This operator is of the same level precedence.  push to the RHS.
                         */
                        dStack.push(operator = operator2, nextToken().getReducedValue(ctx, ctx, variableFactory));
                        continue;
                    } else {
                        /**
                         * The operator doesn't have higher precedence. Therfore reduce the LHS.
                         */
                        while (dStack.size() > 1) {
                            dreduce();
                        }
                        operator = tk.getOperator();
                        // Reduce the lesser or equal precedence operations.
                        while (stk.size() != 1 && stk.peek2() instanceof Integer && ((operator2 = (Integer) stk.peek2()) < PTABLE.length) && PTABLE[operator2] >= PTABLE[operator]) {
                            stk.xswap_op();
                        }
                    }
                } else {
                    if (dStack.size() > 1) {
                        dreduce();
                    }
                    if (stk.isReduceable())
                        stk.xswap();
                    break;
                }
                if ((tk = nextToken()) != null) {
                    switch(operator) {
                        case AND:
                            {
                                if (!(stk.peekBoolean()))
                                    return OP_TERMINATE;
                                else {
                                    splitAccumulator.add(tk);
                                    return AND;
                                }
                            }
                        case OR:
                            {
                                if ((stk.peekBoolean()))
                                    return OP_TERMINATE;
                                else {
                                    splitAccumulator.add(tk);
                                    return OR;
                                }
                            }
                        default:
                            stk.push(operator, tk.getReducedValue(ctx, ctx, variableFactory));
                    }
                }
            }
        } else if (!tk.isOperator()) {
            throw new CompileException("unexpected token: " + tk.getName(), expr, st);
        } else {
            reduce();
            splitAccumulator.push(tk);
        }
    }
    // keep XSWAPing and reducing, until there is nothing left.
    if (stk.isReduceable()) {
        while (true) {
            reduce();
            if (stk.isReduceable()) {
                stk.xswap();
            } else {
                break;
            }
        }
    }
    return OP_RESET_FRAME;
}
Also used : ASTNode(org.mvel2.ast.ASTNode) CompileException(org.mvel2.CompileException) OperatorNode(org.mvel2.ast.OperatorNode)

Example 15 with CompileException

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

the class AbstractParser method procTypedNode.

/**
 * Process the current typed node
 *
 * @param decl node is a declaration or not
 * @return and ast node
 */
private ASTNode procTypedNode(boolean decl) {
    while (true) {
        if (lastNode.getLiteralValue() instanceof String) {
            char[] tmp = ((String) lastNode.getLiteralValue()).toCharArray();
            TypeDescriptor tDescr = new TypeDescriptor(tmp, 0, tmp.length, 0);
            try {
                lastNode.setLiteralValue(getClassReference(pCtx, tDescr));
                lastNode.discard();
            } catch (Exception e) {
            // fall through;
            }
        }
        if (lastNode.isLiteral() && lastNode.getLiteralValue() instanceof Class) {
            lastNode.discard();
            captureToEOS();
            if (decl) {
                splitAccumulator.add(new DeclTypedVarNode(new String(expr, st, cursor - st), expr, st, cursor - st, (Class) lastNode.getLiteralValue(), fields | ASTNode.ASSIGN, pCtx));
            } else {
                captureToEOS();
                splitAccumulator.add(new TypedVarNode(expr, st, cursor - st - 1, fields | ASTNode.ASSIGN, (Class) lastNode.getLiteralValue(), pCtx));
            }
        } else if (lastNode instanceof Proto) {
            captureToEOS();
            if (decl) {
                splitAccumulator.add(new DeclProtoVarNode(new String(expr, st, cursor - st), (Proto) lastNode, fields | ASTNode.ASSIGN, pCtx));
            } else {
                splitAccumulator.add(new ProtoVarNode(expr, st, cursor - st, fields | ASTNode.ASSIGN, (Proto) lastNode, pCtx));
            }
        } else // this redundant looking code is needed to work with the interpreter and MVELSH properly.
        if ((fields & ASTNode.COMPILE_IMMEDIATE) == 0) {
            if (stk.peek() instanceof Class) {
                captureToEOS();
                if (decl) {
                    splitAccumulator.add(new DeclTypedVarNode(new String(expr, st, cursor - st), expr, st, cursor - st, (Class) stk.pop(), fields | ASTNode.ASSIGN, pCtx));
                } else {
                    splitAccumulator.add(new TypedVarNode(expr, st, cursor - st, fields | ASTNode.ASSIGN, (Class) stk.pop(), pCtx));
                }
            } else if (stk.peek() instanceof Proto) {
                captureToEOS();
                if (decl) {
                    splitAccumulator.add(new DeclProtoVarNode(new String(expr, st, cursor - st), (Proto) stk.pop(), fields | ASTNode.ASSIGN, pCtx));
                } else {
                    splitAccumulator.add(new ProtoVarNode(expr, st, cursor - st, fields | ASTNode.ASSIGN, (Proto) stk.pop(), pCtx));
                }
            } else {
                throw new CompileException("unknown class or illegal statement: " + lastNode.getLiteralValue(), expr, cursor);
            }
        } else {
            throw new CompileException("unknown class or illegal statement: " + lastNode.getLiteralValue(), expr, cursor);
        }
        skipWhitespace();
        if (cursor < end && expr[cursor] == ',') {
            st = ++cursor;
            splitAccumulator.add(new EndOfStatement(pCtx));
        } else {
            return (ASTNode) splitAccumulator.pop();
        }
    }
}
Also used : ProtoVarNode(org.mvel2.ast.ProtoVarNode) DeclProtoVarNode(org.mvel2.ast.DeclProtoVarNode) EndOfStatement(org.mvel2.ast.EndOfStatement) CompileException(org.mvel2.CompileException) RedundantCodeException(org.mvel2.ast.RedundantCodeException) TypeDescriptor(org.mvel2.ast.TypeDescriptor) Proto(org.mvel2.ast.Proto) DeclProtoVarNode(org.mvel2.ast.DeclProtoVarNode) IndexedDeclTypedVarNode(org.mvel2.ast.IndexedDeclTypedVarNode) DeclTypedVarNode(org.mvel2.ast.DeclTypedVarNode) ASTNode(org.mvel2.ast.ASTNode) CompileException(org.mvel2.CompileException) IndexedDeclTypedVarNode(org.mvel2.ast.IndexedDeclTypedVarNode) TypedVarNode(org.mvel2.ast.TypedVarNode) DeclTypedVarNode(org.mvel2.ast.DeclTypedVarNode)

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