Search in sources :

Example 31 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)

Example 32 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 33 with CompileException

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

Example 34 with CompileException

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

the class ASMAccessorOptimizer method compileAccessor.

private Accessor compileAccessor() {
    assert debug("<<INITIATE COMPILE>>");
    Object curr = ctx;
    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;
                }
                // check to see if a null safety is enabled on this property.
                if (fields == -1) {
                    if (curr == null) {
                        if (nullSafe) {
                            throw new OptimizationNotSupported();
                        }
                        break;
                    } else {
                        fields = 0;
                    }
                }
                first = false;
                if (nullSafe && cursor < end) {
                    assert debug("DUP");
                    mv.visitInsn(DUP);
                    Label j = new Label();
                    assert debug("IFNONNULL : jump");
                    mv.visitJumpInsn(IFNONNULL, j);
                    assert debug("ARETURN");
                    mv.visitInsn(ARETURN);
                    assert debug("LABEL:jump");
                    mv.visitLabel(j);
                }
            }
        } 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;
                }
                // check to see if a null safety is enabled on this property.
                if (fields == -1) {
                    if (curr == null) {
                        if (nullSafe) {
                            throw new OptimizationNotSupported();
                        }
                        break;
                    } else {
                        fields = 0;
                    }
                }
                first = false;
                if (nullSafe && cursor < end) {
                    assert debug("DUP");
                    mv.visitInsn(DUP);
                    Label j = new Label();
                    assert debug("IFNONNULL : jump");
                    mv.visitJumpInsn(IFNONNULL, j);
                    assert debug("ARETURN");
                    mv.visitInsn(ARETURN);
                    assert debug("LABEL:jump");
                    mv.visitLabel(j);
                }
            }
        }
        val = curr;
        _finishJIT();
        return _initializeAccessor();
    } catch (InvocationTargetException e) {
        throw new PropertyAccessException(new String(expr), expr, st, e);
    } catch (IllegalAccessException e) {
        throw new PropertyAccessException(new String(expr), expr, st, e);
    } catch (IndexOutOfBoundsException e) {
        throw new PropertyAccessException(new String(expr), expr, st, e);
    } catch (PropertyAccessException e) {
        throw new CompileException(e.getMessage(), expr, st, e);
    } catch (CompileException e) {
        throw e;
    } catch (NullPointerException e) {
        throw new PropertyAccessException(new String(expr), expr, st, e);
    } catch (OptimizationNotSupported e) {
        throw e;
    } catch (Exception e) {
        throw new CompileException(e.getMessage(), expr, st, e);
    }
}
Also used : Label(org.mvel2.asm.Label) OptimizationNotSupported(org.mvel2.optimizers.OptimizationNotSupported) IOException(java.io.IOException)

Example 35 with CompileException

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

the class Fold method getReducedValue.

public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) {
    ItemResolverFactory.ItemResolver itemR = new ItemResolverFactory.ItemResolver("$");
    ItemResolverFactory itemFactory = new ItemResolverFactory(itemR, new DefaultLocalVariableResolverFactory(factory));
    List list;
    if (constraintEx != null) {
        Object x = dataEx.getValue(ctx, thisValue, factory);
        if (!(x instanceof Collection))
            throw new CompileException("was expecting type: Collection; but found type: " + (x == null ? "null" : x.getClass().getName()), expr, start);
        list = new ArrayList(((Collection) x).size());
        for (Object o : (Collection) x) {
            itemR.value = o;
            if ((Boolean) constraintEx.getValue(ctx, thisValue, itemFactory)) {
                list.add(subEx.getValue(o, thisValue, itemFactory));
            }
        }
    } else {
        Object x = dataEx.getValue(ctx, thisValue, factory);
        if (!(x instanceof Collection))
            throw new CompileException("was expecting type: Collection; but found type: " + (x == null ? "null" : x.getClass().getName()), expr, start);
        list = new ArrayList(((Collection) x).size());
        for (Object o : (Collection) x) {
            list.add(subEx.getValue(itemR.value = o, thisValue, itemFactory));
        }
    }
    return list;
}
Also used : DefaultLocalVariableResolverFactory(org.mvel2.integration.impl.DefaultLocalVariableResolverFactory) ArrayList(java.util.ArrayList) Collection(java.util.Collection) CompileException(org.mvel2.CompileException) List(java.util.List) ArrayList(java.util.ArrayList) ItemResolverFactory(org.mvel2.integration.impl.ItemResolverFactory)

Aggregations

CompileException (org.mvel2.CompileException)25 Map (java.util.Map)9 List (java.util.List)8 IOException (java.io.IOException)7 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)7 ArrayList (java.util.ArrayList)6 ParserContext (org.mvel2.ParserContext)4 Function (org.mvel2.ast.Function)4 TypeDescriptor (org.mvel2.ast.TypeDescriptor)4 WeakHashMap (java.util.WeakHashMap)3 Proto (org.mvel2.ast.Proto)3 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)3 StringAppender (org.mvel2.util.StringAppender)3 HashMap (java.util.HashMap)2 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