Search in sources :

Example 26 with CompileException

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

the class AbstractParser method _captureBlock.

private ASTNode _captureBlock(ASTNode node, final char[] expr, boolean cond, int type) {
    skipWhitespace();
    int startCond = 0;
    int endCond = 0;
    int blockStart;
    int blockEnd;
    String name;
    /**
     * Functions are a special case we handle differently from the rest of block parsing
     */
    switch(type) {
        case FUNCTION:
            {
                int st = cursor;
                captureToNextTokenJunction();
                if (cursor == end) {
                    throw new CompileException("unexpected end of statement", expr, st);
                }
                /**
         * Check to see if the name is legal.
         */
                if (isReservedWord(name = createStringTrimmed(expr, st, cursor - st)) || isNotValidNameorLabel(name))
                    throw new CompileException("illegal function name or use of reserved word", expr, cursor);
                if (pCtx == null)
                    pCtx = getParserContext();
                FunctionParser parser = new FunctionParser(name, cursor, end - cursor, expr, fields, pCtx, splitAccumulator);
                Function function = parser.parse();
                cursor = parser.getCursor();
                return lastNode = function;
            }
        case PROTO:
            if (ProtoParser.isUnresolvedWaiting()) {
                if (pCtx == null)
                    pCtx = getParserContext();
                ProtoParser.checkForPossibleUnresolvedViolations(expr, cursor, pCtx);
            }
            int st = cursor;
            captureToNextTokenJunction();
            if (isReservedWord(name = createStringTrimmed(expr, st, cursor - st)) || isNotValidNameorLabel(name))
                throw new CompileException("illegal prototype name or use of reserved word", expr, cursor);
            if (expr[cursor = nextNonBlank()] != '{') {
                throw new CompileException("expected '{' but found: " + expr[cursor], expr, cursor);
            }
            cursor = balancedCaptureWithLineAccounting(expr, st = cursor + 1, end, '{', pCtx);
            if (pCtx == null)
                pCtx = getParserContext();
            ProtoParser parser = new ProtoParser(expr, st, cursor, name, pCtx, fields, splitAccumulator);
            Proto proto = parser.parse();
            if (pCtx == null)
                pCtx = getParserContext();
            pCtx.addImport(proto);
            proto.setCursorPosition(st, cursor);
            cursor = parser.getCursor();
            ProtoParser.notifyForLateResolution(proto);
            return lastNode = proto;
        default:
            if (cond) {
                if (expr[cursor] != '(') {
                    throw new CompileException("expected '(' but encountered: " + expr[cursor], expr, cursor);
                }
                /**
           * This block is an: IF, FOREACH or WHILE node.
           */
                endCond = cursor = balancedCaptureWithLineAccounting(expr, startCond = cursor, end, '(', pCtx);
                startCond++;
                cursor++;
            }
    }
    skipWhitespace();
    if (cursor >= end) {
        throw new CompileException("unexpected end of statement", expr, end);
    } else if (expr[cursor] == '{') {
        blockEnd = cursor = balancedCaptureWithLineAccounting(expr, blockStart = cursor, end, '{', pCtx);
    } else {
        blockStart = cursor - 1;
        captureToEOSorEOL();
        blockEnd = cursor + 1;
    }
    if (type == ASTNode.BLOCK_IF) {
        IfNode ifNode = (IfNode) node;
        if (node != null) {
            if (!cond) {
                return ifNode.setElseBlock(expr, st = trimRight(blockStart + 1), trimLeft(blockEnd) - st, pCtx);
            } else {
                return ifNode.setElseIf((IfNode) createBlockToken(startCond, endCond, trimRight(blockStart + 1), trimLeft(blockEnd), type));
            }
        } else {
            return createBlockToken(startCond, endCond, blockStart + 1, blockEnd, type);
        }
    } else if (type == ASTNode.BLOCK_DO) {
        cursor++;
        skipWhitespace();
        st = cursor;
        captureToNextTokenJunction();
        if ("while".equals(name = new String(expr, st, cursor - st))) {
            skipWhitespace();
            startCond = cursor + 1;
            endCond = cursor = balancedCaptureWithLineAccounting(expr, cursor, end, '(', pCtx);
            return createBlockToken(startCond, endCond, trimRight(blockStart + 1), trimLeft(blockEnd), type);
        } else if ("until".equals(name)) {
            skipWhitespace();
            startCond = cursor + 1;
            endCond = cursor = balancedCaptureWithLineAccounting(expr, cursor, end, '(', pCtx);
            return createBlockToken(startCond, endCond, trimRight(blockStart + 1), trimLeft(blockEnd), ASTNode.BLOCK_DO_UNTIL);
        } else {
            throw new CompileException("expected 'while' or 'until' but encountered: " + name, expr, cursor);
        }
    } else // DON"T REMOVE THIS COMMENT!
    // else if (isFlag(ASTNode.BLOCK_FOREACH) || isFlag(ASTNode.BLOCK_WITH)) {
    {
        return createBlockToken(startCond, endCond, trimRight(blockStart + 1), trimLeft(blockEnd), type);
    }
}
Also used : FunctionParser(org.mvel2.util.FunctionParser) ProtoParser(org.mvel2.util.ProtoParser) CompileException(org.mvel2.CompileException)

Example 27 with CompileException

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

the class PropertyVerifier method getMethod.

/**
   * Process method
   *
   * @param ctx  - the ingress type
   * @param name - the property component
   * @return known egress type.
   */
private Class getMethod(Class ctx, String name) {
    int st = cursor;
    /**
     * Check to see if this is the first element in the statement.
     */
    if (first) {
        first = false;
        methodCall = true;
        /**
       * It's the first element in the statement, therefore we check to see if there is a static import of a
       * native Java method or an MVEL function.
       */
        if (pCtx.hasImport(name)) {
            Method m = pCtx.getStaticImport(name).getMethod();
            /**
         * Replace the method parameters.
         */
            ctx = m.getDeclaringClass();
            name = m.getName();
        } else if (pCtx.hasFunction(name)) {
            resolvedExternally = false;
            Function f = pCtx.getFunction(name);
            f.checkArgumentCount(parseParameterList((((cursor = balancedCapture(expr, cursor, end, '(')) - st) > 1 ? ParseTools.subset(expr, st + 1, cursor - st - 1) : new char[0]), 0, -1).size());
            return f.getEgressType();
        } else if (pCtx.hasVarOrInput("this")) {
            if (pCtx.isStrictTypeEnforcement()) {
                recordTypeParmsForProperty("this");
            }
            ctx = pCtx.getVarOrInputType("this");
            resolvedExternally = false;
        }
    }
    /**
     * Get the arguments for the method.
     */
    String tk;
    if (cursor < end && expr[cursor] == '(' && ((cursor = balancedCapture(expr, cursor, end, '(')) - st) > 1) {
        tk = new String(expr, st + 1, cursor - st - 1);
    } else {
        tk = "";
    }
    cursor++;
    /**
     * Parse out the arguments list.
     */
    Class[] args;
    List<char[]> subtokens = parseParameterList(tk.toCharArray(), 0, -1);
    if (subtokens.size() == 0) {
        args = new Class[0];
        subtokens = Collections.emptyList();
    } else {
        //   ParserContext subCtx = pCtx.createSubcontext();
        args = new Class[subtokens.size()];
        /**
       *  Subcompile all the arguments to determine their known types.
       */
        //  ExpressionCompiler compiler;
        List<ErrorDetail> errors = pCtx.getErrorList().isEmpty() ? pCtx.getErrorList() : new ArrayList<ErrorDetail>(pCtx.getErrorList());
        CompileException rethrow = null;
        for (int i = 0; i < subtokens.size(); i++) {
            try {
                args[i] = MVEL.analyze(subtokens.get(i), pCtx);
                if ("null".equals(String.valueOf(subtokens.get(i)))) {
                    args[i] = NullType.class;
                }
            } catch (CompileException e) {
                rethrow = ErrorUtil.rewriteIfNeeded(e, expr, this.st);
            }
            if (errors.size() < pCtx.getErrorList().size()) {
                for (ErrorDetail detail : pCtx.getErrorList()) {
                    if (!errors.contains(detail)) {
                        detail.setExpr(expr);
                        detail.setCursor(new String(expr).substring(this.st).indexOf(new String(subtokens.get(i))) + this.st);
                        detail.setColumn(0);
                        detail.setLineNumber(0);
                        detail.calcRowAndColumn();
                    }
                }
            }
            if (rethrow != null) {
                throw rethrow;
            }
        }
    }
    /**
     * If the target object is an instance of java.lang.Class itself then do not
     * adjust the Class scope target.
     */
    Method m;
    if ((m = getBestCandidate(args, name, ctx, ctx.getMethods(), pCtx.isStrongTyping())) == null) {
        if ((m = getBestCandidate(args, name, ctx, ctx.getDeclaredMethods(), pCtx.isStrongTyping())) == null) {
            StringAppender errorBuild = new StringAppender();
            for (int i = 0; i < args.length; i++) {
                errorBuild.append(args[i] != null ? args[i].getName() : null);
                if (i < args.length - 1)
                    errorBuild.append(", ");
            }
            if (("size".equals(name) || "length".equals(name)) && args.length == 0 && ctx.isArray()) {
                return Integer.class;
            }
            if (pCtx.isStrictTypeEnforcement()) {
                throw new CompileException("unable to resolve method using strict-mode: " + ctx.getName() + "." + name + "(" + errorBuild.toString() + ")", expr, tkStart);
            }
            return Object.class;
        }
    }
    /**
     * If we're in strict mode, we look for generic type information.
     */
    if (pCtx.isStrictTypeEnforcement() && m.getGenericReturnType() != null) {
        Map<String, Class> typeArgs = new HashMap<String, Class>();
        Type[] gpt = m.getGenericParameterTypes();
        Class z;
        ParameterizedType pt;
        for (int i = 0; i < gpt.length; i++) {
            if (gpt[i] instanceof ParameterizedType) {
                pt = (ParameterizedType) gpt[i];
                if ((z = pCtx.getImport(new String(subtokens.get(i)))) != null) {
                    /**
             * We record the value of the type parameter to our typeArgs Map.
             */
                    if (pt.getRawType().equals(Class.class)) {
                        /**
               * If this is an instance of Class, we deal with the special parameterization case.
               */
                        typeArgs.put(pt.getActualTypeArguments()[0].toString(), z);
                    } else {
                        typeArgs.put(gpt[i].toString(), z);
                    }
                }
            }
        }
        if (pCtx.isStrictTypeEnforcement() && ctx.getTypeParameters().length != 0 && pCtx.getLastTypeParameters() != null && pCtx.getLastTypeParameters().length == ctx.getTypeParameters().length) {
            TypeVariable[] typeVariables = ctx.getTypeParameters();
            for (int i = 0; i < typeVariables.length; i++) {
                typeArgs.put(typeVariables[i].getName(), (Class) pCtx.getLastTypeParameters()[i]);
            }
        }
        /**
       * Get the return type argument
       */
        Type parametricReturnType = m.getGenericReturnType();
        String returnTypeArg = parametricReturnType.toString();
        //push return type parameters onto parser context, only if this is a parametric type
        if (parametricReturnType instanceof ParameterizedType) {
            pCtx.setLastTypeParameters(((ParameterizedType) parametricReturnType).getActualTypeArguments());
        }
        if (paramTypes != null && paramTypes.containsKey(returnTypeArg)) {
            /**
         * If the paramTypes Map contains the known type, return that type.
         */
            return (Class) paramTypes.get(returnTypeArg);
        } else if (typeArgs.containsKey(returnTypeArg)) {
            /**
         * If the generic type was declared as part of the method, it will be in this
         * Map.
         */
            return typeArgs.get(returnTypeArg);
        }
    }
    if (!Modifier.isPublic(m.getModifiers())) {
        StringAppender errorBuild = new StringAppender();
        for (int i = 0; i < args.length; i++) {
            errorBuild.append(args[i] != null ? args[i].getName() : null);
            if (i < args.length - 1)
                errorBuild.append(", ");
        }
        String scope = Modifier.toString(m.getModifiers());
        if (scope.trim().equals(""))
            scope = "<package local>";
        addFatalError("the referenced method is not accessible: " + ctx.getName() + "." + name + "(" + errorBuild.toString() + ")" + " (scope: " + scope + "; required: public", this.tkStart);
    }
    return m.getReturnType();
}
Also used : Function(org.mvel2.ast.Function) NullType(org.mvel2.util.NullType) StringAppender(org.mvel2.util.StringAppender)

Example 28 with CompileException

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

the class InlineCollectionNode method execGraph.

private Object execGraph(Object o, Class type, Object ctx, VariableResolverFactory factory) {
    if (o instanceof List) {
        ArrayList list = new ArrayList(((List) o).size());
        for (Object item : (List) o) {
            list.add(execGraph(item, type, ctx, factory));
        }
        return list;
    } else if (o instanceof Map) {
        HashMap map = new HashMap();
        for (Object item : ((Map) o).keySet()) {
            map.put(execGraph(item, type, ctx, factory), execGraph(((Map) o).get(item), type, ctx, factory));
        }
        return map;
    } else if (o instanceof Object[]) {
        int dim = 0;
        if (type != null) {
            String nm = type.getName();
            while (nm.charAt(dim) == '[') dim++;
        } else {
            type = Object[].class;
            dim = 1;
        }
        Object newArray = Array.newInstance(getSubComponentType(type), ((Object[]) o).length);
        try {
            Class cls = dim > 1 ? findClass(null, repeatChar('[', dim - 1) + "L" + getBaseComponentType(type).getName() + ";", AbstractParser.getCurrentThreadParserContext()) : type;
            int c = 0;
            for (Object item : (Object[]) o) {
                Array.set(newArray, c++, execGraph(item, cls, ctx, factory));
            }
            return newArray;
        } catch (IllegalArgumentException e) {
            throw new CompileException("type mismatch in array", expr, start, e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("this error should never throw:" + getBaseComponentType(type).getName(), e);
        }
    } else {
        if (type.isArray()) {
            return MVEL.eval((String) o, ctx, factory, getBaseComponentType(type));
        } else {
            return MVEL.eval((String) o, ctx, factory);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompileException(org.mvel2.CompileException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with CompileException

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

the class NewObjectNode method getReducedValue.

public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) {
    try {
        if (typeDescr.isArray()) {
            Class cls = findClass(factory, typeDescr.getClassName(), null);
            int[] s = new int[typeDescr.getArrayLength()];
            ArraySize[] arraySize = typeDescr.getArraySize();
            for (int i = 0; i < s.length; i++) {
                s[i] = convert(eval(arraySize[i].value, ctx, factory), Integer.class);
            }
            return newInstance(cls, s);
        } else {
            String[] cnsRes = captureContructorAndResidual(name, 0, name.length);
            List<char[]> constructorParms = parseMethodOrConstructor(cnsRes[0].toCharArray());
            if (constructorParms != null) {
                Class cls = findClass(factory, new String(subset(name, 0, findFirst('(', 0, name.length, name))).trim(), null);
                Object[] parms = new Object[constructorParms.size()];
                for (int i = 0; i < constructorParms.size(); i++) {
                    parms[i] = eval(constructorParms.get(i), ctx, factory);
                }
                Constructor cns = getBestConstructorCandidate(parms, cls, false);
                if (cns == null)
                    throw new CompileException("unable to find constructor for: " + cls.getName(), expr, start);
                for (int i = 0; i < parms.length; i++) {
                    //noinspection unchecked
                    parms[i] = convert(parms[i], cns.getParameterTypes()[i]);
                }
                if (cnsRes.length > 1) {
                    return PropertyAccessor.get(cnsRes[1], cns.newInstance(parms), factory, thisValue);
                } else {
                    return cns.newInstance(parms);
                }
            } else {
                Constructor<?> cns = Class.forName(typeDescr.getClassName(), true, currentThread().getContextClassLoader()).getConstructor(EMPTYCLS);
                if (cnsRes.length > 1) {
                    return PropertyAccessor.get(cnsRes[1], cns.newInstance(), factory, thisValue);
                } else {
                    return cns.newInstance();
                }
            }
        }
    } catch (CompileException e) {
        throw e;
    } catch (ClassNotFoundException e) {
        throw new CompileException("unable to resolve class: " + e.getMessage(), expr, start, e);
    } catch (NoSuchMethodException e) {
        throw new CompileException("cannot resolve constructor: " + e.getMessage(), expr, start, e);
    } catch (Exception e) {
        throw new CompileException("could not instantiate class: " + e.getMessage(), expr, start, e);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) CompileException(org.mvel2.CompileException) CompileException(org.mvel2.CompileException)

Example 30 with CompileException

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

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() + ";", null);
                        } 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 = 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)

Aggregations

CompileException (org.mvel2.CompileException)23 List (java.util.List)8 Map (java.util.Map)8 IOException (java.io.IOException)7 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)7 ArrayList (java.util.ArrayList)6 Function (org.mvel2.ast.Function)4 TypeDescriptor (org.mvel2.ast.TypeDescriptor)4 WeakHashMap (java.util.WeakHashMap)3 ParserContext (org.mvel2.ParserContext)3 Proto (org.mvel2.ast.Proto)3 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)3 StringAppender (org.mvel2.util.StringAppender)3 Label (org.mvel2.asm.Label)2 ASTNode (org.mvel2.ast.ASTNode)2 EndOfStatement (org.mvel2.ast.EndOfStatement)2 CompiledExpression (org.mvel2.compiler.CompiledExpression)2 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)2 ItemResolverFactory (org.mvel2.integration.impl.ItemResolverFactory)2 ExecutionStack (org.mvel2.util.ExecutionStack)2