use of org.mvel2.util.MethodStub in project mvel by mikebrock.
the class TypesAndInferenceTests method testStrictTypingCompilation2.
public void testStrictTypingCompilation2() throws Exception {
ParserContext ctx = new ParserContext();
// noinspection RedundantArrayCreation
ctx.addImport("getRuntime", new MethodStub(Runtime.class.getMethod("getRuntime", new Class[] {})));
ctx.setStrictTypeEnforcement(true);
ExpressionCompiler compiler = new ExpressionCompiler("getRuntime()");
StaticMethodImportResolverFactory si = new StaticMethodImportResolverFactory(ctx);
Serializable expression = compiler.compile(ctx);
serializationTest(expression);
assertTrue(executeExpression(expression, si) instanceof Runtime);
}
use of org.mvel2.util.MethodStub in project mvel by mvel.
the class ReflectiveAccessorOptimizer method getMethod.
private Object getMethod(Object ctx, String name, Object[] args, Class[] argTypes, ExecutableStatement[] es) throws Exception {
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 FunctionInstance) {
FunctionInstance func = (FunctionInstance) ptr;
if (!name.equals(func.getFunction().getName())) {
getBeanProperty(ctx, name);
addAccessorNode(new DynamicFunctionAccessor(es));
} else {
addAccessorNode(new FunctionAccessor(func, es));
}
return func.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 && currType == null) {
throw new PropertyAccessException("null pointer or function not found: " + name, this.expr, this.start, pCtx);
}
boolean classTarget = false;
Class<?> cls = currType != null ? currType : ((classTarget = ctx instanceof Class) ? (Class<?>) ctx : ctx.getClass());
currType = null;
Method m;
Class[] parameterTypes = null;
/**
* Try to find an instance method from the class target.
*/
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 we didn't find anything and the declared class is different from the actual one try also with the actual one
if (m == null && ctx != null && cls != ctx.getClass() && !(ctx instanceof Class)) {
cls = ctx.getClass();
if ((m = getBestCandidate(argTypes, name, cls, cls.getMethods(), false, classTarget)) != 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, pCtx);
}
if (es != null) {
ExecutableStatement cExpr;
for (int i = 0; i < es.length; i++) {
cExpr = es[i];
if (cExpr.getKnownIngressType() == null) {
cExpr.setKnownIngressType(paramTypeVarArgsSafe(parameterTypes, i, m.isVarArgs()));
cExpr.computeTypeConversionRule();
}
if (!cExpr.isConvertableIngressEgress()) {
args[i] = convert(args[i], paramTypeVarArgsSafe(parameterTypes, i, m.isVarArgs()));
}
}
} else {
/**
* Coerce any types if required.
*/
for (int i = 0; i < args.length; i++) args[i] = convert(args[i], paramTypeVarArgsSafe(parameterTypes, i, m.isVarArgs()));
}
Method method = getWidenedTarget(cls, m);
Object o = ctx != null ? method.invoke(ctx, normalizeArgsForVarArgs(parameterTypes, args, m.isVarArgs())) : null;
if (hasNullMethodHandler()) {
addAccessorNode(new MethodAccessorNH(method, (ExecutableStatement[]) es, getNullMethodHandler()));
if (o == null)
o = getNullMethodHandler().getProperty(m.getName(), ctx, variableFactory);
} else {
addAccessorNode(new MethodAccessor(method, (ExecutableStatement[]) es));
}
/**
* return the response.
*/
currType = toNonPrimitiveType(method.getReturnType());
return o;
}
use of org.mvel2.util.MethodStub in project mvel by mvel.
the class ASMConsistencyTest method testNullArgConvert.
public void testNullArgConvert() {
// change DynamicOptimizer props,make sure of using asm optimize
int oldThreashold = DynamicOptimizer.tenuringThreshold;
long oldTimeSpan = DynamicOptimizer.timeSpan;
DynamicOptimizer.tenuringThreshold = 1;
DynamicOptimizer.timeSpan = 1000 * 60 * 60L;
Map<String, Object> imports = new HashMap<>(2);
imports.put("isNull", new MethodStub(TestFunction.class, "isNull"));
Serializable expr = MVEL.compileExpression("isNull(var1)", imports);
Map<String, Object> inputVars = new HashMap<>(2);
inputVars.put("var1", "someStr");
// trigger asm optimize,tenuringThreshold is 1
for (int i = 0; i < 3; i++) {
MVEL.executeExpression(expr, inputVars);
}
// use AsmAccessor
inputVars.put("var1", null);
assertTrue((boolean) MVEL.executeExpression(expr, inputVars));
// revert the props
DynamicOptimizer.tenuringThreshold = oldThreashold;
DynamicOptimizer.timeSpan = oldTimeSpan;
}
Aggregations