use of org.mvel2.ast.TypeDescriptor 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.ast.TypeDescriptor in project mvel by mikebrock.
the class PropertyAccessor method getCollectionPropertyAO.
private Object getCollectionPropertyAO(Object ctx, String prop) throws Exception {
if (prop.length() != 0) {
ctx = getBeanProperty(ctx, prop);
}
if (ctx == null)
return null;
int _start = ++cursor;
whiteSpaceSkip();
if (cursor == end || scanTo(']'))
throw new PropertyAccessException("unterminated '['", property, cursor);
prop = new String(property, _start, cursor++ - _start);
if (ctx instanceof Map) {
if (hasPropertyHandler(Map.class))
return getPropertyHandler(Map.class).getProperty(prop, ctx, variableFactory);
else
return ((Map) ctx).get(eval(prop, ctx, variableFactory));
} else if (ctx instanceof List) {
if (hasPropertyHandler(List.class))
return getPropertyHandler(List.class).getProperty(prop, ctx, variableFactory);
else
return ((List) ctx).get((Integer) eval(prop, ctx, variableFactory));
} else if (ctx instanceof Collection) {
if (hasPropertyHandler(Collection.class))
return getPropertyHandler(Collection.class).getProperty(prop, ctx, variableFactory);
else {
int count = (Integer) eval(prop, ctx, variableFactory);
if (count > ((Collection) ctx).size())
throw new PropertyAccessException("index [" + count + "] out of bounds on collections", property, cursor);
Iterator iter = ((Collection) ctx).iterator();
for (int i = 0; i < count; i++) iter.next();
return iter.next();
}
} else if (ctx.getClass().isArray()) {
if (hasPropertyHandler(Array.class))
return getPropertyHandler(Array.class).getProperty(prop, ctx, variableFactory);
return Array.get(ctx, (Integer) eval(prop, ctx, variableFactory));
} else if (ctx instanceof CharSequence) {
if (hasPropertyHandler(CharSequence.class))
return getPropertyHandler(CharSequence.class).getProperty(prop, ctx, variableFactory);
else
return ((CharSequence) ctx).charAt((Integer) eval(prop, ctx, variableFactory));
} else {
try {
return getClassReference(getCurrentThreadParserContext(), (Class) ctx, new TypeDescriptor(property, start, end - start, 0));
} catch (Exception e) {
throw new PropertyAccessException("illegal use of []: unknown type: " + (ctx == null ? null : ctx.getClass().getName()), property, st);
}
}
}
use of org.mvel2.ast.TypeDescriptor in project mvel by mikebrock.
the class PropertyAccessor method getCollectionProperty.
/**
* Handle accessing a property embedded in a collections, map, or array
*
* @param ctx -
* @param prop -
* @return -
* @throws Exception -
*/
private Object getCollectionProperty(Object ctx, String prop) throws Exception {
if (prop.length() != 0) {
ctx = getBeanProperty(ctx, prop);
}
if (ctx == null)
return null;
int _start = ++cursor;
whiteSpaceSkip();
if (cursor == end || scanTo(']'))
throw new PropertyAccessException("unterminated '['", property, cursor);
prop = new String(property, _start, cursor++ - _start);
if (ctx instanceof Map) {
return ((Map) ctx).get(eval(prop, ctx, variableFactory));
} else if (ctx instanceof List) {
return ((List) ctx).get((Integer) eval(prop, ctx, variableFactory));
} else if (ctx instanceof Collection) {
int count = (Integer) eval(prop, ctx, variableFactory);
if (count > ((Collection) ctx).size())
throw new PropertyAccessException("index [" + count + "] out of bounds on collections", property, cursor);
Iterator iter = ((Collection) ctx).iterator();
for (int i = 0; i < count; i++) iter.next();
return iter.next();
} else if (ctx.getClass().isArray()) {
return Array.get(ctx, (Integer) eval(prop, ctx, variableFactory));
} else if (ctx instanceof CharSequence) {
return ((CharSequence) ctx).charAt((Integer) eval(prop, ctx, variableFactory));
} else {
// TypeDescriptor td = new TypeDescriptor(property, 0);
try {
return getClassReference(getCurrentThreadParserContext(), (Class) ctx, new TypeDescriptor(property, start, length, 0));
} catch (Exception e) {
throw new PropertyAccessException("illegal use of []: unknown type: " + (ctx == null ? null : ctx.getClass().getName()), property, st, e);
}
}
}
Aggregations