use of org.mvel2.compiler.ExecutableStatement in project mvel by mikebrock.
the class CoreConfidenceTests method testTestIntToLong.
public void testTestIntToLong() {
String s = "1+(long)a";
ParserContext pc = new ParserContext();
pc.addInput("a", Integer.class);
ExpressionCompiler compiler = new ExpressionCompiler(s, pc);
CompiledExpression expr = compiler.compile();
Map vars = new HashMap();
vars.put("a", 1);
Object r = ((ExecutableStatement) expr).getValue(null, new MapVariableResolverFactory(vars));
assertEquals(new Long(2), r);
}
use of org.mvel2.compiler.ExecutableStatement in project stargate-core by tuplejump.
the class AggregateFunction method init.
@Override
public void init(Options options) {
this.options = options;
int k = 0;
positions = new HashMap<>();
aggregateFields = new String[aggregates.length];
for (AggregateFactory aggregateFactory : aggregates) {
String field = aggregateFactory.getField();
if (field != null) {
aggregateFields[k] = field;
positions.put(field, k++);
}
}
ParserConfiguration parserConfig = getParserConfiguration();
if (groupBy != null) {
this.groupByExpressions = new ExecutableStatement[groupBy.length];
this.simpleExpressions = new boolean[groupBy.length];
groupByFields = new ArrayList<>();
for (int i = 0; i < groupBy.length; i++) {
String groupByField = groupBy[i];
String groupByCol = getGroupBy(groupByField);
boolean isSimpleExpression = options.types.containsKey(getColumnName(groupByCol));
ParserContext parserContext = new ParserContext(parserConfig);
groupByExpressions[i] = (ExecutableStatement) MVEL.compileExpression(groupByField, parserContext);
if (isSimpleExpression) {
simpleExpressions[i] = true;
int pos = k++;
positions.put(groupByCol, pos);
groupByFields.add(groupByCol);
} else {
simpleExpressions[i] = false;
Set<String> keys = parserContext.getInputs().keySet();
for (String key : keys) {
int pos = k++;
boolean canResolve = options.types.containsKey(getColumnName(key));
if (canResolve) {
String groupByColField = getGroupBy(key);
positions.put(key, pos);
groupByFields.add(groupByColField);
}
}
}
}
}
group = new Group(options, aggregates, groupBy, groupByExpressions);
}
use of org.mvel2.compiler.ExecutableStatement in project mvel by mikebrock.
the class ASMAccessorOptimizer method optimizeSetAccessor.
public Accessor optimizeSetAccessor(ParserContext pCtx, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory, boolean rootThisRef, Object value, Class ingressType) {
this.expr = property;
this.start = this.cursor = start;
this.end = start + offset;
this.length = start + offset;
this.first = true;
this.ingressType = ingressType;
compiledInputs = new ArrayList<ExecutableStatement>();
this.pCtx = pCtx;
this.ctx = ctx;
this.thisRef = thisRef;
this.variableFactory = factory;
char[] root = null;
PropertyVerifier verifier = new PropertyVerifier(property, this.pCtx = pCtx);
int split = findLastUnion();
if (split != -1) {
root = subset(property, 0, split);
}
AccessorNode rootAccessor = null;
_initJIT2();
if (root != null) {
int _length = this.length;
int _end = this.end;
char[] _expr = this.expr;
this.length = end = (this.expr = root).length;
// run the compiler but don't finish building.
deferFinish = true;
noinit = true;
compileAccessor();
ctx = this.val;
this.expr = _expr;
this.cursor = start + root.length + 1;
this.length = _length - root.length - 1;
this.end = this.cursor + this.length;
} else {
assert debug("ALOAD 1");
mv.visitVarInsn(ALOAD, 1);
}
try {
skipWhitespace();
if (collection) {
int st = cursor;
whiteSpaceSkip();
if (st == end)
throw new PropertyAccessException("unterminated '['", expr, start);
if (scanTo(']'))
throw new PropertyAccessException("unterminated '['", expr, start);
String ex = new String(expr, st, cursor - st);
assert debug("CHECKCAST " + ctx.getClass().getName());
mv.visitTypeInsn(CHECKCAST, getInternalName(ctx.getClass()));
if (ctx instanceof Map) {
if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(Map.class)) {
propHandlerByteCodePut(ex, ctx, Map.class, value);
} else {
//noinspection unchecked
((Map) ctx).put(eval(ex, ctx, variableFactory), convert(value, returnType = verifier.analyze()));
writeLiteralOrSubexpression(subCompileExpression(ex.toCharArray(), pCtx));
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
if (value != null & returnType != value.getClass()) {
dataConversion(returnType);
checkcast(returnType);
}
assert debug("INVOKEINTERFACE Map.put");
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
assert debug("POP");
mv.visitInsn(POP);
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
}
} else if (ctx instanceof List) {
if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(List.class)) {
propHandlerByteCodePut(ex, ctx, List.class, value);
} else {
//noinspection unchecked
((List) ctx).set(eval(ex, ctx, variableFactory, Integer.class), convert(value, returnType = verifier.analyze()));
writeLiteralOrSubexpression(subCompileExpression(ex.toCharArray(), pCtx));
unwrapPrimitive(int.class);
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
if (value != null & !value.getClass().isAssignableFrom(returnType)) {
dataConversion(returnType);
checkcast(returnType);
}
assert debug("INVOKEINTERFACE List.set");
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "set", "(ILjava/lang/Object;)Ljava/lang/Object;");
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
}
} else if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(ctx.getClass())) {
propHandlerByteCodePut(ex, ctx, ctx.getClass(), value);
} else if (ctx.getClass().isArray()) {
if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(Array.class)) {
propHandlerByteCodePut(ex, ctx, Array.class, value);
} else {
Class type = getBaseComponentType(ctx.getClass());
Object idx = eval(ex, ctx, variableFactory);
writeLiteralOrSubexpression(subCompileExpression(ex.toCharArray(), pCtx), int.class);
if (!(idx instanceof Integer)) {
dataConversion(Integer.class);
idx = DataConversion.convert(idx, Integer.class);
unwrapPrimitive(int.class);
}
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
if (type.isPrimitive())
unwrapPrimitive(type);
else if (!type.equals(value.getClass())) {
dataConversion(type);
}
arrayStore(type);
//noinspection unchecked
Array.set(ctx, (Integer) idx, convert(value, type));
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
}
} else {
throw new PropertyAccessException("cannot bind to collection property: " + new String(expr) + ": not a recognized collection type: " + ctx.getClass(), expr, start);
}
deferFinish = false;
noinit = false;
_finishJIT();
try {
deferFinish = false;
return _initializeAccessor();
} catch (Exception e) {
throw new CompileException("could not generate accessor", expr, start, e);
}
}
String tk = new String(expr, this.cursor, this.length);
Member member = getFieldOrWriteAccessor(ctx.getClass(), tk, value == null ? null : ingressType);
if (GlobalListenerFactory.hasSetListeners()) {
mv.visitVarInsn(ALOAD, 1);
mv.visitLdcInsn(tk);
mv.visitVarInsn(ALOAD, 3);
mv.visitVarInsn(ALOAD, 4);
mv.visitMethodInsn(INVOKESTATIC, NAMESPACE + "integration/GlobalListenerFactory", "notifySetListeners", "(Ljava/lang/Object;Ljava/lang/String;L" + NAMESPACE + "integration/VariableResolverFactory;Ljava/lang/Object;)V");
GlobalListenerFactory.notifySetListeners(ctx, tk, variableFactory, value);
}
if (member instanceof Field) {
checkcast(ctx.getClass());
Field fld = (Field) member;
Label jmp = null;
Label jmp2 = new Label();
if (fld.getType().isPrimitive()) {
assert debug("ASTORE 5");
mv.visitVarInsn(ASTORE, 5);
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
if (value == null)
value = PropertyTools.getPrimitiveInitialValue(fld.getType());
jmp = new Label();
assert debug("IFNOTNULL jmp");
mv.visitJumpInsn(IFNONNULL, jmp);
assert debug("ALOAD 5");
mv.visitVarInsn(ALOAD, 5);
assert debug("ICONST_0");
mv.visitInsn(ICONST_0);
assert debug("PUTFIELD " + getInternalName(fld.getDeclaringClass()) + "." + tk);
mv.visitFieldInsn(PUTFIELD, getInternalName(fld.getDeclaringClass()), tk, getDescriptor(fld.getType()));
assert debug("GOTO jmp2");
mv.visitJumpInsn(GOTO, jmp2);
assert debug("jmp:");
mv.visitLabel(jmp);
assert debug("ALOAD 5");
mv.visitVarInsn(ALOAD, 5);
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
unwrapPrimitive(fld.getType());
} else {
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
checkcast(fld.getType());
}
if (jmp == null && value != null && !fld.getType().isAssignableFrom(value.getClass())) {
if (!canConvert(fld.getType(), value.getClass())) {
throw new CompileException("cannot convert type: " + value.getClass() + ": to " + fld.getType(), expr, start);
}
dataConversion(fld.getType());
fld.set(ctx, convert(value, fld.getType()));
} else {
fld.set(ctx, value);
}
assert debug("PUTFIELD " + getInternalName(fld.getDeclaringClass()) + "." + tk);
mv.visitFieldInsn(PUTFIELD, getInternalName(fld.getDeclaringClass()), tk, getDescriptor(fld.getType()));
assert debug("jmp2:");
mv.visitLabel(jmp2);
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
} else if (member != null) {
assert debug("CHECKCAST " + getInternalName(ctx.getClass()));
mv.visitTypeInsn(CHECKCAST, getInternalName(ctx.getClass()));
Method meth = (Method) member;
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
Class targetType = meth.getParameterTypes()[0];
Label jmp = null;
Label jmp2 = new Label();
if (value != null && !targetType.isAssignableFrom(value.getClass())) {
if (!canConvert(targetType, value.getClass())) {
throw new CompileException("cannot convert type: " + value.getClass() + ": to " + meth.getParameterTypes()[0], expr, start);
}
dataConversion(getWrapperClass(targetType));
if (targetType.isPrimitive()) {
unwrapPrimitive(targetType);
} else
checkcast(targetType);
meth.invoke(ctx, convert(value, meth.getParameterTypes()[0]));
} else {
if (targetType.isPrimitive()) {
if (value == null)
value = PropertyTools.getPrimitiveInitialValue(targetType);
jmp = new Label();
assert debug("IFNOTNULL jmp");
mv.visitJumpInsn(IFNONNULL, jmp);
assert debug("ICONST_0");
mv.visitInsn(ICONST_0);
assert debug("INVOKEVIRTUAL " + getInternalName(meth.getDeclaringClass()) + "." + meth.getName());
mv.visitMethodInsn(INVOKEVIRTUAL, getInternalName(meth.getDeclaringClass()), meth.getName(), getMethodDescriptor(meth));
assert debug("GOTO jmp2");
mv.visitJumpInsn(GOTO, jmp2);
assert debug("jmp:");
mv.visitLabel(jmp);
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
unwrapPrimitive(targetType);
} else {
checkcast(targetType);
}
meth.invoke(ctx, value);
}
assert debug("INVOKEVIRTUAL " + getInternalName(meth.getDeclaringClass()) + "." + meth.getName());
mv.visitMethodInsn(INVOKEVIRTUAL, getInternalName(meth.getDeclaringClass()), meth.getName(), getMethodDescriptor(meth));
assert debug("jmp2:");
mv.visitLabel(jmp2);
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
} else if (ctx instanceof Map) {
assert debug("CHECKCAST " + getInternalName(ctx.getClass()));
mv.visitTypeInsn(CHECKCAST, getInternalName(ctx.getClass()));
assert debug("LDC '" + tk + "'");
mv.visitLdcInsn(tk);
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
assert debug("INVOKEVIRTUAL java/util/HashMap.put");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/HashMap", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
assert debug("ALOAD 4");
mv.visitVarInsn(ALOAD, 4);
//noinspection unchecked
((Map) ctx).put(tk, value);
} else {
throw new PropertyAccessException("could not access property (" + tk + ") in: " + ingressType.getName(), expr, start);
}
} catch (InvocationTargetException e) {
throw new PropertyAccessException("could not access property", expr, start, e);
} catch (IllegalAccessException e) {
throw new PropertyAccessException("could not access property", expr, start, e);
}
try {
deferFinish = false;
noinit = false;
_finishJIT();
return _initializeAccessor();
} catch (Exception e) {
throw new CompileException("could not generate accessor", expr, start, e);
}
}
use of org.mvel2.compiler.ExecutableStatement in project mvel by mikebrock.
the class ASMAccessorOptimizer method getCollectionProperty.
private Object getCollectionProperty(Object ctx, String prop) throws IllegalAccessException, InvocationTargetException {
if (prop.trim().length() > 0) {
ctx = getBeanProperty(ctx, prop);
first = false;
}
if (ctx == null)
return null;
assert debug("\n ** ENTER -> {collection:<<" + prop + ">>; ctx=" + ctx + "}");
if (first) {
assert debug("ALOAD 1");
mv.visitVarInsn(ALOAD, 1);
}
int start = ++cursor;
skipWhitespace();
if (cursor == end)
throw new CompileException("unterminated '['", expr, st);
if (scanTo(']'))
throw new CompileException("unterminated '['", expr, st);
String tk = new String(expr, start, cursor - start);
assert debug("{collection token: [" + tk + "]}");
ExecutableStatement compiled = (ExecutableStatement) subCompileExpression(tk.toCharArray(), pCtx);
Object item = compiled.getValue(ctx, variableFactory);
++cursor;
if (ctx instanceof Map) {
assert debug("CHECKCAST java/util/Map");
mv.visitTypeInsn(CHECKCAST, "java/util/Map");
Class c = writeLiteralOrSubexpression(compiled);
if (c != null && c.isPrimitive()) {
wrapPrimitive(c);
}
assert debug("INVOKEINTERFACE: get");
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
return ((Map) ctx).get(item);
} else if (ctx instanceof List) {
assert debug("CHECKCAST java/util/List");
mv.visitTypeInsn(CHECKCAST, "java/util/List");
writeLiteralOrSubexpression(compiled, int.class);
assert debug("INVOKEINTERFACE: java/util/List.get");
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;");
return ((List) ctx).get(convert(item, Integer.class));
} else if (ctx.getClass().isArray()) {
assert debug("CHECKCAST " + getDescriptor(ctx.getClass()));
mv.visitTypeInsn(CHECKCAST, getDescriptor(ctx.getClass()));
writeLiteralOrSubexpression(compiled, int.class, item.getClass());
Class cls = getBaseComponentType(ctx.getClass());
if (cls.isPrimitive()) {
if (cls == int.class) {
assert debug("IALOAD");
mv.visitInsn(IALOAD);
} else if (cls == char.class) {
assert debug("CALOAD");
mv.visitInsn(CALOAD);
} else if (cls == boolean.class) {
assert debug("BALOAD");
mv.visitInsn(BALOAD);
} else if (cls == double.class) {
assert debug("DALOAD");
mv.visitInsn(DALOAD);
} else if (cls == float.class) {
assert debug("FALOAD");
mv.visitInsn(FALOAD);
} else if (cls == short.class) {
assert debug("SALOAD");
mv.visitInsn(SALOAD);
} else if (cls == long.class) {
assert debug("LALOAD");
mv.visitInsn(LALOAD);
} else if (cls == byte.class) {
assert debug("BALOAD");
mv.visitInsn(BALOAD);
}
wrapPrimitive(cls);
} else {
assert debug("AALOAD");
mv.visitInsn(AALOAD);
}
return Array.get(ctx, convert(item, Integer.class));
} else if (ctx instanceof CharSequence) {
assert debug("CHECKCAST java/lang/CharSequence");
mv.visitTypeInsn(CHECKCAST, "java/lang/CharSequence");
if (item instanceof Integer) {
intPush((Integer) item);
assert debug("INVOKEINTERFACE java/lang/CharSequence.charAt");
mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/CharSequence", "charAt", "(I)C");
wrapPrimitive(char.class);
return ((CharSequence) ctx).charAt((Integer) item);
} else {
writeLiteralOrSubexpression(compiled, Integer.class);
unwrapPrimitive(int.class);
assert debug("INVOKEINTERFACE java/lang/CharSequence.charAt");
mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/CharSequence", "charAt", "(I)C");
wrapPrimitive(char.class);
return ((CharSequence) ctx).charAt(convert(item, Integer.class));
}
} else {
TypeDescriptor tDescr = new TypeDescriptor(expr, this.start, length, 0);
if (tDescr.isArray()) {
try {
Class cls = getClassReference((Class) ctx, tDescr, variableFactory, pCtx);
// rootNode = new StaticReferenceAccessor(cls);
ldcClassConstant(cls);
return cls;
} catch (Exception e) {
//fall through
}
}
throw new CompileException("illegal use of []: unknown type: " + (ctx == null ? null : ctx.getClass().getName()), expr, st);
}
}
use of org.mvel2.compiler.ExecutableStatement in project mvel by mikebrock.
the class ForEachNode method getReducedValue.
public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) {
ItemResolverFactory.ItemResolver itemR = new ItemResolverFactory.ItemResolver(item);
ItemResolverFactory itemFactory = new ItemResolverFactory(itemR, new DefaultLocalVariableResolverFactory(factory));
Object iterCond = MVEL.eval(expr, start, offset, thisValue, factory);
if (itemType != null && itemType.isArray())
enforceTypeSafety(itemType, getBaseComponentType(iterCond.getClass()));
this.compiledBlock = (ExecutableStatement) subCompileExpression(expr, blockStart, blockOffset);
Object v;
if (iterCond instanceof Iterable) {
for (Object o : (Iterable) iterCond) {
itemR.setValue(o);
v = compiledBlock.getValue(ctx, thisValue, itemFactory);
if (itemFactory.tiltFlag())
return v;
}
} else if (iterCond != null && iterCond.getClass().isArray()) {
int len = Array.getLength(iterCond);
for (int i = 0; i < len; i++) {
itemR.setValue(Array.get(iterCond, i));
v = compiledBlock.getValue(ctx, thisValue, itemFactory);
if (itemFactory.tiltFlag())
return v;
}
} else if (iterCond instanceof CharSequence) {
for (Object o : iterCond.toString().toCharArray()) {
itemR.setValue(o);
v = compiledBlock.getValue(ctx, thisValue, itemFactory);
if (itemFactory.tiltFlag())
return v;
}
} else if (iterCond instanceof Integer) {
int max = (Integer) iterCond + 1;
for (int i = 1; i != max; i++) {
itemR.setValue(i);
v = compiledBlock.getValue(ctx, thisValue, itemFactory);
if (itemFactory.tiltFlag())
return v;
}
} else {
throw new CompileException("non-iterable type: " + (iterCond != null ? iterCond.getClass().getName() : "null"), expr, start);
}
return null;
}
Aggregations