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);
}
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);
}
}
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.");
}
}
}
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);
}
}
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;
}
Aggregations