use of org.mvel2.asm.Type in project mvel by mikebrock.
the class PropertyVerifier method getMethod.
/**
* Process method
*
* @param ctx - the ingress type
* @param name - the property component
* @return known egress type.
*/
private Class getMethod(Class ctx, String name) {
int st = cursor;
/**
* Check to see if this is the first element in the statement.
*/
if (first) {
first = false;
methodCall = true;
/**
* It's the first element in the statement, therefore we check to see if there is a static import of a
* native Java method or an MVEL function.
*/
if (pCtx.hasImport(name)) {
Method m = pCtx.getStaticImport(name).getMethod();
/**
* Replace the method parameters.
*/
ctx = m.getDeclaringClass();
name = m.getName();
} else if (pCtx.hasFunction(name)) {
resolvedExternally = false;
Function f = pCtx.getFunction(name);
f.checkArgumentCount(parseParameterList((((cursor = balancedCapture(expr, cursor, end, '(')) - st) > 1 ? ParseTools.subset(expr, st + 1, cursor - st - 1) : new char[0]), 0, -1).size());
return f.getEgressType();
} else if (pCtx.hasVarOrInput("this")) {
if (pCtx.isStrictTypeEnforcement()) {
recordTypeParmsForProperty("this");
}
ctx = pCtx.getVarOrInputType("this");
resolvedExternally = false;
}
}
/**
* Get the arguments for the method.
*/
String tk;
if (cursor < end && expr[cursor] == '(' && ((cursor = balancedCapture(expr, cursor, end, '(')) - st) > 1) {
tk = new String(expr, st + 1, cursor - st - 1);
} else {
tk = "";
}
cursor++;
/**
* Parse out the arguments list.
*/
Class[] args;
List<char[]> subtokens = parseParameterList(tk.toCharArray(), 0, -1);
if (subtokens.size() == 0) {
args = new Class[0];
subtokens = Collections.emptyList();
} else {
// ParserContext subCtx = pCtx.createSubcontext();
args = new Class[subtokens.size()];
/**
* Subcompile all the arguments to determine their known types.
*/
// ExpressionCompiler compiler;
List<ErrorDetail> errors = pCtx.getErrorList().isEmpty() ? pCtx.getErrorList() : new ArrayList<ErrorDetail>(pCtx.getErrorList());
CompileException rethrow = null;
for (int i = 0; i < subtokens.size(); i++) {
try {
args[i] = MVEL.analyze(subtokens.get(i), pCtx);
if ("null".equals(String.valueOf(subtokens.get(i)))) {
args[i] = NullType.class;
}
} catch (CompileException e) {
rethrow = ErrorUtil.rewriteIfNeeded(e, expr, this.st);
}
if (errors.size() < pCtx.getErrorList().size()) {
for (ErrorDetail detail : pCtx.getErrorList()) {
if (!errors.contains(detail)) {
detail.setExpr(expr);
detail.setCursor(new String(expr).substring(this.st).indexOf(new String(subtokens.get(i))) + this.st);
detail.setColumn(0);
detail.setLineNumber(0);
detail.calcRowAndColumn();
}
}
}
if (rethrow != null) {
throw rethrow;
}
}
}
/**
* If the target object is an instance of java.lang.Class itself then do not
* adjust the Class scope target.
*/
Method m;
if ((m = getBestCandidate(args, name, ctx, ctx.getMethods(), pCtx.isStrongTyping())) == null) {
if ((m = getBestCandidate(args, name, ctx, ctx.getDeclaredMethods(), pCtx.isStrongTyping())) == null) {
StringAppender errorBuild = new StringAppender();
for (int i = 0; i < args.length; i++) {
errorBuild.append(args[i] != null ? args[i].getName() : null);
if (i < args.length - 1)
errorBuild.append(", ");
}
if (("size".equals(name) || "length".equals(name)) && args.length == 0 && ctx.isArray()) {
return Integer.class;
}
if (pCtx.isStrictTypeEnforcement()) {
throw new CompileException("unable to resolve method using strict-mode: " + ctx.getName() + "." + name + "(" + errorBuild.toString() + ")", expr, tkStart);
}
return Object.class;
}
}
/**
* If we're in strict mode, we look for generic type information.
*/
if (pCtx.isStrictTypeEnforcement() && m.getGenericReturnType() != null) {
Map<String, Class> typeArgs = new HashMap<String, Class>();
Type[] gpt = m.getGenericParameterTypes();
Class z;
ParameterizedType pt;
for (int i = 0; i < gpt.length; i++) {
if (gpt[i] instanceof ParameterizedType) {
pt = (ParameterizedType) gpt[i];
if ((z = pCtx.getImport(new String(subtokens.get(i)))) != null) {
/**
* We record the value of the type parameter to our typeArgs Map.
*/
if (pt.getRawType().equals(Class.class)) {
/**
* If this is an instance of Class, we deal with the special parameterization case.
*/
typeArgs.put(pt.getActualTypeArguments()[0].toString(), z);
} else {
typeArgs.put(gpt[i].toString(), z);
}
}
}
}
if (pCtx.isStrictTypeEnforcement() && ctx.getTypeParameters().length != 0 && pCtx.getLastTypeParameters() != null && pCtx.getLastTypeParameters().length == ctx.getTypeParameters().length) {
TypeVariable[] typeVariables = ctx.getTypeParameters();
for (int i = 0; i < typeVariables.length; i++) {
typeArgs.put(typeVariables[i].getName(), (Class) pCtx.getLastTypeParameters()[i]);
}
}
/**
* Get the return type argument
*/
Type parametricReturnType = m.getGenericReturnType();
String returnTypeArg = parametricReturnType.toString();
// push return type parameters onto parser context, only if this is a parametric type
if (parametricReturnType instanceof ParameterizedType) {
pCtx.setLastTypeParameters(((ParameterizedType) parametricReturnType).getActualTypeArguments());
}
if (paramTypes != null && paramTypes.containsKey(returnTypeArg)) {
/**
* If the paramTypes Map contains the known type, return that type.
*/
return (Class) paramTypes.get(returnTypeArg);
} else if (typeArgs.containsKey(returnTypeArg)) {
/**
* If the generic type was declared as part of the method, it will be in this
* Map.
*/
return typeArgs.get(returnTypeArg);
}
}
if (!Modifier.isPublic(m.getModifiers())) {
StringAppender errorBuild = new StringAppender();
for (int i = 0; i < args.length; i++) {
errorBuild.append(args[i] != null ? args[i].getName() : null);
if (i < args.length - 1)
errorBuild.append(", ");
}
String scope = Modifier.toString(m.getModifiers());
if (scope.trim().equals(""))
scope = "<package local>";
addFatalError("the referenced method is not accessible: " + ctx.getName() + "." + name + "(" + errorBuild.toString() + ")" + " (scope: " + scope + "; required: public", this.tkStart);
}
return m.getReturnType();
}
use of org.mvel2.asm.Type 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.asm.Type 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.asm.Type in project mvel by mikebrock.
the class ReflectiveAccessorOptimizer 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.rootNode = this.currNode = null;
this.expr = property;
this.start = start;
this.first = true;
this.length = start + offset;
this.ctx = ctx;
this.thisRef = thisRef;
this.variableFactory = factory;
this.ingressType = ingressType;
char[] root = null;
int split = findLastUnion();
PropertyVerifier verifier = new PropertyVerifier(property, this.pCtx = pCtx);
if (split != -1) {
root = subset(property, 0, split++);
// todo: must use the property verifier.
property = subset(property, split, property.length - split);
}
if (root != null) {
this.length = end = (this.expr = root).length;
compileGetChain();
ctx = this.val;
}
if (ctx == null) {
throw new PropertyAccessException("could not access property: " + new String(property, this.start, length) + "; parent is null: " + new String(expr), expr, this.start);
}
try {
this.length = end = (this.expr = property).length;
int st;
this.cursor = st = 0;
skipWhitespace();
if (collection) {
st = cursor;
if (cursor == end)
throw new PropertyAccessException("unterminated '['", expr, this.start);
if (scanTo(']'))
throw new PropertyAccessException("unterminated '['", expr, this.start);
String ex = new String(property, st, cursor - st);
if (ctx instanceof Map) {
if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(Map.class)) {
propHandlerSet(ex, ctx, Map.class, value);
} else {
// noinspection unchecked
((Map) ctx).put(eval(ex, ctx, variableFactory), convert(value, returnType = verifier.analyze()));
addAccessorNode(new MapAccessorNest(ex, returnType));
}
return rootNode;
} else if (ctx instanceof List) {
if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(List.class)) {
propHandlerSet(ex, ctx, List.class, value);
} else {
// noinspection unchecked
((List) ctx).set(eval(ex, ctx, variableFactory, Integer.class), convert(value, returnType = verifier.analyze()));
addAccessorNode(new ListAccessorNest(ex, returnType));
}
return rootNode;
} else if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(ctx.getClass())) {
propHandlerSet(ex, ctx, ctx.getClass(), value);
return rootNode;
} else if (ctx.getClass().isArray()) {
if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(Array.class)) {
propHandlerSet(ex, ctx, Array.class, value);
} else {
// noinspection unchecked
Array.set(ctx, eval(ex, ctx, variableFactory, Integer.class), convert(value, getBaseComponentType(ctx.getClass())));
addAccessorNode(new ArrayAccessorNest(ex));
}
return rootNode;
} else {
throw new PropertyAccessException("cannot bind to collection property: " + new String(property) + ": not a recognized collection type: " + ctx.getClass(), expr, this.st);
}
} else if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(ctx.getClass())) {
propHandlerSet(new String(property), ctx, ctx.getClass(), value);
return rootNode;
}
String tk = new String(property, 0, length);
if (hasSetListeners()) {
notifySetListeners(ctx, tk, variableFactory, value);
addAccessorNode(new Notify(tk));
}
Member member = getFieldOrWriteAccessor(ctx.getClass(), tk, value == null ? null : ingressType);
if (member instanceof Field) {
Field fld = (Field) member;
if (value != null && !fld.getType().isAssignableFrom(value.getClass())) {
if (!canConvert(fld.getType(), value.getClass())) {
throw new CompileException("cannot convert type: " + value.getClass() + ": to " + fld.getType(), this.expr, this.start);
}
fld.set(ctx, convert(value, fld.getType()));
addAccessorNode(new DynamicFieldAccessor(fld));
} else if (value == null && fld.getType().isPrimitive()) {
fld.set(ctx, PropertyTools.getPrimitiveInitialValue(fld.getType()));
addAccessorNode(new FieldAccessor(fld));
} else {
fld.set(ctx, value);
addAccessorNode(new FieldAccessor(fld));
}
} else if (member != null) {
Method meth = (Method) member;
if (value != null && !meth.getParameterTypes()[0].isAssignableFrom(value.getClass())) {
if (!canConvert(meth.getParameterTypes()[0], value.getClass())) {
throw new CompileException("cannot convert type: " + value.getClass() + ": to " + meth.getParameterTypes()[0], this.expr, this.start);
}
meth.invoke(ctx, convert(value, meth.getParameterTypes()[0]));
} else if (value == null && meth.getParameterTypes()[0].isPrimitive()) {
meth.invoke(ctx, PropertyTools.getPrimitiveInitialValue(meth.getParameterTypes()[0]));
} else {
meth.invoke(ctx, value);
}
addAccessorNode(new SetterAccessor(meth));
} else if (ctx instanceof Map) {
// noinspection unchecked
((Map) ctx).put(tk, value);
addAccessorNode(new MapAccessor(tk));
} else {
throw new PropertyAccessException("could not access property (" + tk + ") in: " + ingressType.getName(), this.expr, this.start);
}
} catch (InvocationTargetException e) {
throw new PropertyAccessException("could not access property: " + new String(property), this.expr, st, e);
} catch (IllegalAccessException e) {
throw new PropertyAccessException("could not access property: " + new String(property), this.expr, st, e);
} catch (IllegalArgumentException e) {
throw new PropertyAccessException("error binding property: " + new String(property) + " (value <<" + value + ">>::" + (value == null ? "null" : value.getClass().getCanonicalName()) + ")", this.expr, st, e);
}
return rootNode;
}
use of org.mvel2.asm.Type in project mvel by mikebrock.
the class ReflectiveAccessorOptimizer method getMethod.
/**
* Find an appropriate method, execute it, and return it's response.
*
* @param ctx -
* @param name -
* @return -
* @throws Exception -
*/
@SuppressWarnings({ "unchecked" })
private Object getMethod(Object ctx, String name) throws Exception {
int st = cursor;
String tk = cursor != end && expr[cursor] == '(' && ((cursor = balancedCapture(expr, cursor, '(')) - st) > 1 ? new String(expr, st + 1, cursor - st - 1) : "";
cursor++;
Object[] args;
Class[] argTypes;
ExecutableStatement[] es;
if (tk.length() == 0) {
args = ParseTools.EMPTY_OBJ_ARR;
argTypes = ParseTools.EMPTY_CLS_ARR;
es = null;
} else {
List<char[]> subtokens = parseParameterList(tk.toCharArray(), 0, -1);
es = new ExecutableStatement[subtokens.size()];
args = new Object[subtokens.size()];
argTypes = new Class[subtokens.size()];
for (int i = 0; i < subtokens.size(); i++) {
try {
args[i] = (es[i] = (ExecutableStatement) subCompileExpression(subtokens.get(i), pCtx)).getValue(this.thisRef, thisRef, variableFactory);
} catch (CompileException e) {
throw ErrorUtil.rewriteIfNeeded(e, this.expr, this.start);
}
if (es[i].isExplicitCast())
argTypes[i] = es[i].getKnownEgressType();
}
if (pCtx.isStrictTypeEnforcement()) {
for (int i = 0; i < args.length; i++) {
argTypes[i] = es[i].getKnownEgressType();
}
} else {
for (int i = 0; i < args.length; i++) {
if (argTypes[i] != null)
continue;
if (es[i].getKnownEgressType() == Object.class) {
argTypes[i] = args[i] == null ? null : args[i].getClass();
} else {
argTypes[i] = es[i].getKnownEgressType();
}
}
}
}
if (first && variableFactory != null && variableFactory.isResolveable(name)) {
Object ptr = variableFactory.getVariableResolver(name).getValue();
if (ptr instanceof Method) {
ctx = ((Method) ptr).getDeclaringClass();
name = ((Method) ptr).getName();
} else if (ptr instanceof MethodStub) {
ctx = ((MethodStub) ptr).getClassReference();
name = ((MethodStub) ptr).getMethodName();
} else if (ptr instanceof Function) {
Function func = (Function) ptr;
if (!name.equals(func.getName())) {
getBeanProperty(ctx, name);
addAccessorNode(new DynamicFunctionAccessor(es));
} else {
addAccessorNode(new FunctionAccessor((Function) ptr, es));
}
return ((Function) ptr).call(ctx, thisRef, variableFactory, args);
} else {
throw new OptimizationFailure("attempt to optimize a method call for a reference that does not point to a method: " + name + " (reference is type: " + (ctx != null ? ctx.getClass().getName() : null) + ")");
}
first = false;
}
if (ctx == null) {
throw new PropertyAccessException("null pointer or function not found: " + name, this.expr, this.start);
}
boolean classTarget = false;
Class<?> cls = currType != null ? currType : ((classTarget = ctx instanceof Class) ? (Class<?>) ctx : ctx.getClass());
currType = null;
Method m;
Class[] parameterTypes = null;
if ((m = getBestCandidate(argTypes, name, cls, cls.getMethods(), false, classTarget)) != null) {
parameterTypes = m.getParameterTypes();
}
if (m == null && classTarget) {
/**
* If we didn't find anything, maybe we're looking for the actual java.lang.Class methods.
*/
if ((m = getBestCandidate(argTypes, name, cls, Class.class.getMethods(), false)) != null) {
parameterTypes = m.getParameterTypes();
}
}
if (m == null) {
StringAppender errorBuild = new StringAppender();
if ("size".equals(name) && args.length == 0 && cls.isArray()) {
addAccessorNode(new ArrayLength());
return getLength(ctx);
}
for (int i = 0; i < args.length; i++) {
errorBuild.append(args[i] != null ? args[i].getClass().getName() : null);
if (i < args.length - 1)
errorBuild.append(", ");
}
throw new PropertyAccessException("unable to resolve method: " + cls.getName() + "." + name + "(" + errorBuild.toString() + ") [arglength=" + args.length + "]", this.expr, this.st);
} else {
if (es != null) {
ExecutableStatement cExpr;
for (int i = 0; i < es.length; i++) {
cExpr = (ExecutableStatement) es[i];
if (cExpr.getKnownIngressType() == null) {
cExpr.setKnownIngressType(parameterTypes[i]);
cExpr.computeTypeConversionRule();
}
if (!cExpr.isConvertableIngressEgress()) {
args[i] = convert(args[i], parameterTypes[i]);
}
}
} else {
/**
* Coerce any types if required.
*/
for (int i = 0; i < args.length; i++) args[i] = convert(args[i], parameterTypes[i]);
}
Object o = getWidenedTarget(m).invoke(ctx, args);
if (hasNullMethodHandler()) {
addAccessorNode(new MethodAccessorNH(getWidenedTarget(m), (ExecutableStatement[]) es, getNullMethodHandler()));
if (o == null)
o = getNullMethodHandler().getProperty(m.getName(), ctx, variableFactory);
} else {
addAccessorNode(new MethodAccessor(getWidenedTarget(m), (ExecutableStatement[]) es));
}
/**
* return the response.
*/
return o;
}
}
Aggregations