Search in sources :

Example 81 with CompileException

use of org.mule.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)

Example 82 with CompileException

use of org.mule.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 83 with CompileException

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

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) {
            rate = run / (time = (currentTimeMillis() - start) / 1000);
            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)

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