use of org.mvel2.ParserContext in project mvel by mikebrock.
the class WithTests method testWithAndEnumInPackageImport.
public void testWithAndEnumInPackageImport() {
ParserConfiguration pconf = new ParserConfiguration();
pconf.addPackageImport(MyEnum.class.getPackage().getName());
ParserContext pCtx = new ParserContext(pconf);
pCtx.setStrongTyping(true);
pCtx.addInput("thing", Thing.class);
Thing thing = new Thing("xxx");
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("thing", thing);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression("with( thing ) { myEnum = MyEnum.FULL_DOCUMENTATION }", pCtx);
MVEL.executeExpression(stmt, null, vars);
assertEquals(MyEnum.FULL_DOCUMENTATION, thing.getMyEnum());
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class ASMAccessorOptimizer method optimizeCollection.
public Accessor optimizeCollection(ParserContext pCtx, Object o, Class type, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) {
this.expr = property;
this.cursor = this.start = start;
this.end = start + offset;
this.length = offset;
this.returnType = type;
this.compiledInputs = new ArrayList<ExecutableStatement>();
this.ctx = ctx;
this.thisRef = thisRef;
this.variableFactory = factory;
_initJIT();
literal = true;
_getAccessor(o, type);
_finishJIT();
try {
Accessor compiledAccessor = _initializeAccessor();
if (property != null && length > start) {
return new Union(compiledAccessor, property, start, length);
} else {
return compiledAccessor;
}
} catch (Exception e) {
throw new OptimizationFailure("could not optimize collection", e);
}
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class ASMAccessorOptimizer method optimizeObjectCreation.
public Accessor optimizeObjectCreation(ParserContext pCtx, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) {
_initJIT();
compiledInputs = new ArrayList<ExecutableStatement>();
this.start = cursor = start;
this.end = start + offset;
this.length = this.end - this.start;
this.ctx = ctx;
this.thisRef = thisRef;
this.variableFactory = factory;
this.pCtx = pCtx;
String[] cnsRes = captureContructorAndResidual(property, start, offset);
List<char[]> constructorParms = parseMethodOrConstructor(cnsRes[0].toCharArray());
try {
if (constructorParms != null) {
for (char[] constructorParm : constructorParms) {
compiledInputs.add((ExecutableStatement) subCompileExpression(constructorParm, pCtx));
}
Class cls = findClass(factory, new String(subset(property, 0, findFirst('(', start, length, property))), pCtx);
assert debug("NEW " + getInternalName(cls));
mv.visitTypeInsn(NEW, getInternalName(cls));
assert debug("DUP");
mv.visitInsn(DUP);
Object[] parms = new Object[constructorParms.size()];
int i = 0;
for (ExecutableStatement es : compiledInputs) {
parms[i++] = es.getValue(ctx, factory);
}
Constructor cns = getBestConstructorCandidate(parms, cls, pCtx.isStrongTyping());
if (cns == null) {
StringBuilder error = new StringBuilder();
for (int x = 0; x < parms.length; x++) {
error.append(parms[x].getClass().getName());
if (x + 1 < parms.length)
error.append(", ");
}
throw new CompileException("unable to find constructor: " + cls.getName() + "(" + error.toString() + ")", expr, st);
}
this.returnType = cns.getDeclaringClass();
Class tg;
for (i = 0; i < constructorParms.size(); i++) {
assert debug("ALOAD 0");
mv.visitVarInsn(ALOAD, 0);
assert debug("GETFIELD p" + i);
mv.visitFieldInsn(GETFIELD, className, "p" + i, "L" + NAMESPACE + "compiler/ExecutableStatement;");
assert debug("ALOAD 2");
mv.visitVarInsn(ALOAD, 2);
assert debug("ALOAD 3");
mv.visitVarInsn(ALOAD, 3);
assert debug("INVOKEINTERFACE " + NAMESPACE + "compiler/ExecutableStatement.getValue");
mv.visitMethodInsn(INVOKEINTERFACE, "" + NAMESPACE + "compiler/ExecutableStatement", "getValue", "(Ljava/lang/Object;L" + NAMESPACE + "integration/VariableResolverFactory;)Ljava/lang/Object;");
tg = cns.getParameterTypes()[i].isPrimitive() ? getWrapperClass(cns.getParameterTypes()[i]) : cns.getParameterTypes()[i];
if (parms[i] != null && !parms[i].getClass().isAssignableFrom(cns.getParameterTypes()[i])) {
ldcClassConstant(tg);
assert debug("INVOKESTATIC " + NAMESPACE + "DataConversion.convert");
mv.visitMethodInsn(INVOKESTATIC, "" + NAMESPACE + "DataConversion", "convert", "(Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;");
if (cns.getParameterTypes()[i].isPrimitive()) {
unwrapPrimitive(cns.getParameterTypes()[i]);
} else {
assert debug("CHECKCAST " + getInternalName(tg));
mv.visitTypeInsn(CHECKCAST, getInternalName(tg));
}
} else {
assert debug("CHECKCAST " + getInternalName(cns.getParameterTypes()[i]));
mv.visitTypeInsn(CHECKCAST, getInternalName(cns.getParameterTypes()[i]));
}
}
assert debug("INVOKESPECIAL " + getInternalName(cls) + ".<init> : " + getConstructorDescriptor(cns));
mv.visitMethodInsn(INVOKESPECIAL, getInternalName(cls), "<init>", getConstructorDescriptor(cns));
_finishJIT();
Accessor acc = _initializeAccessor();
if (cnsRes.length > 1 && cnsRes[1] != null && !cnsRes[1].trim().equals("")) {
return new Union(acc, cnsRes[1].toCharArray(), 0, cnsRes[1].length());
}
return acc;
} else {
Class cls = findClass(factory, new String(property), pCtx);
assert debug("NEW " + getInternalName(cls));
mv.visitTypeInsn(NEW, getInternalName(cls));
assert debug("DUP");
mv.visitInsn(DUP);
Constructor cns = cls.getConstructor(EMPTYCLS);
assert debug("INVOKESPECIAL <init>");
mv.visitMethodInsn(INVOKESPECIAL, getInternalName(cls), "<init>", getConstructorDescriptor(cns));
_finishJIT();
Accessor acc = _initializeAccessor();
if (cnsRes.length > 1 && cnsRes[1] != null && !cnsRes[1].trim().equals("")) {
return new Union(acc, cnsRes[1].toCharArray(), 0, cnsRes[1].length());
}
return acc;
}
} catch (ClassNotFoundException e) {
throw new CompileException("class or class reference not found: " + new String(property), property, st);
} catch (Exception e) {
throw new OptimizationFailure("could not optimize construtor: " + new String(property), e);
}
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class ArraysTests method testArrayLength.
public void testArrayLength() {
ParserContext context = new ParserContext();
context.setStrongTyping(true);
context.addInput("x", String[].class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression("x.length", context);
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class ArraysTests method testArrayCoercion1.
public void testArrayCoercion1() {
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addInput("bar", Bar.class);
Serializable s = compileSetExpression("bar.intarray[0]", ctx);
Foo foo = new Foo();
executeSetExpression(s, foo, "12");
assertEquals(12, foo.getBar().getIntarray()[0].intValue());
foo = new Foo();
executeSetExpression(s, foo, "13");
assertEquals(13, foo.getBar().getIntarray()[0].intValue());
OptimizerFactory.setDefaultOptimizer("ASM");
ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addInput("bar", Bar.class);
s = compileSetExpression("bar.intarray[0]", ctx);
foo = new Foo();
executeSetExpression(s, foo, "12");
assertEquals(12, foo.getBar().getIntarray()[0].intValue());
executeSetExpression(s, foo, "13");
assertEquals(13, foo.getBar().getIntarray()[0].intValue());
}
Aggregations