use of org.mvel2.integration.VariableResolverFactory 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.integration.VariableResolverFactory in project mvel by mikebrock.
the class ReflectiveAccessorOptimizer method compileConstructor.
@SuppressWarnings({ "WeakerAccess" })
public AccessorNode compileConstructor(char[] expression, Object ctx, VariableResolverFactory vars) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException {
String[] cnsRes = captureContructorAndResidual(expression, start, length);
List<char[]> constructorParms = parseMethodOrConstructor(cnsRes[0].toCharArray());
if (constructorParms != null) {
String s = new String(subset(expression, 0, ArrayTools.findFirst('(', start, length, expression)));
Class cls = ParseTools.findClass(vars, s, pCtx);
ExecutableStatement[] cStmts = new ExecutableStatement[constructorParms.size()];
for (int i = 0; i < constructorParms.size(); i++) {
cStmts[i] = (ExecutableStatement) subCompileExpression(constructorParms.get(i), pCtx);
}
Object[] parms = new Object[constructorParms.size()];
for (int i = 0; i < constructorParms.size(); i++) {
parms[i] = cStmts[i].getValue(ctx, vars);
}
Constructor cns = getBestConstructorCandidate(parms, cls, pCtx.isStrongTyping());
if (cns == null) {
StringBuilder error = new StringBuilder();
for (int i = 0; i < parms.length; i++) {
error.append(parms[i].getClass().getName());
if (i + 1 < parms.length)
error.append(", ");
}
throw new CompileException("unable to find constructor: " + cls.getName() + "(" + error.toString() + ")", this.expr, this.start);
}
for (int i = 0; i < parms.length; i++) {
//noinspection unchecked
parms[i] = convert(parms[i], cns.getParameterTypes()[i]);
}
AccessorNode ca = new ConstructorAccessor(cns, cStmts);
if (cnsRes.length > 1) {
ReflectiveAccessorOptimizer compiledOptimizer = new ReflectiveAccessorOptimizer(pCtx, cnsRes[1].toCharArray(), 0, cnsRes[1].length(), cns.newInstance(parms), ctx, vars);
compiledOptimizer.ingressType = cns.getDeclaringClass();
compiledOptimizer.setRootNode(ca);
compiledOptimizer.compileGetChain();
ca = compiledOptimizer.getRootNode();
this.val = compiledOptimizer.getResultOptPass();
}
return ca;
} else {
Constructor<?> cns = Class.forName(new String(expression), true, Thread.currentThread().getContextClassLoader()).getConstructor(EMPTYCLS);
AccessorNode ca = new ConstructorAccessor(cns, null);
if (cnsRes.length > 1) {
//noinspection NullArgumentToVariableArgMethod
ReflectiveAccessorOptimizer compiledOptimizer = new ReflectiveAccessorOptimizer(getCurrentThreadParserContext(), cnsRes[1].toCharArray(), 0, cnsRes[1].length(), cns.newInstance(null), ctx, vars);
compiledOptimizer.setRootNode(ca);
compiledOptimizer.compileGetChain();
ca = compiledOptimizer.getRootNode();
this.val = compiledOptimizer.getResultOptPass();
}
return ca;
}
}
use of org.mvel2.integration.VariableResolverFactory in project mvel by mikebrock.
the class DynamicFunctionAccessor method getValue.
public Object getValue(Object ctx, Object elCtx, VariableResolverFactory variableFactory) {
Object[] parms = null;
Function function = (Function) ctx;
if (parameters != null && parameters.length != 0) {
parms = new Object[parameters.length];
for (int i = 0; i < parms.length; i++) {
parms[i] = parameters[i].getValue(ctx, elCtx, variableFactory);
}
}
if (nextNode != null) {
return nextNode.getValue(function.call(ctx, elCtx, variableFactory, parms), elCtx, variableFactory);
} else {
return function.call(ctx, elCtx, variableFactory, parms);
}
}
use of org.mvel2.integration.VariableResolverFactory in project mvel by mikebrock.
the class NullSafe method getValue.
public Object getValue(Object ctx, Object elCtx, VariableResolverFactory variableFactory) {
if (ctx == null)
return null;
if (nextNode == null) {
final Accessor a = OptimizerFactory.getAccessorCompiler(OptimizerFactory.SAFE_REFLECTIVE).optimizeAccessor(pCtx, expr, start, offset, ctx, elCtx, variableFactory, true, ctx.getClass());
nextNode = new AccessorNode() {
public AccessorNode getNextNode() {
return null;
}
public AccessorNode setNextNode(AccessorNode accessorNode) {
return null;
}
public Object getValue(Object ctx, Object elCtx, VariableResolverFactory variableFactory) {
return a.getValue(ctx, elCtx, variableFactory);
}
public Object setValue(Object ctx, Object elCtx, VariableResolverFactory variableFactory, Object value) {
return a.setValue(ctx, elCtx, variableFactory, value);
}
public Class getKnownEgressType() {
return a.getKnownEgressType();
}
};
}
// else {
return nextNode.getValue(ctx, elCtx, variableFactory);
// }
}
use of org.mvel2.integration.VariableResolverFactory in project mvel by mikebrock.
the class CompiledNamedIncludeNode method eval.
public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
if (cPreExpression != null) {
MVEL.executeExpression(cPreExpression, ctx, factory);
}
if (next != null) {
String namedTemplate = MVEL.executeExpression(cIncludeExpression, ctx, factory, String.class);
CompiledTemplate ct = runtime.getNamedTemplateRegistry().getNamedTemplate(namedTemplate);
if (ct == null)
throw new TemplateError("named template does not exist: " + namedTemplate);
return next.eval(runtime, appender.append(String.valueOf(TemplateRuntime.execute(ct, ctx, factory, runtime.getNamedTemplateRegistry()))), ctx, factory);
// return next.eval(runtime,
// appender.append(String.valueOf(TemplateRuntime.execute(runtime.getNamedTemplateRegistry().getNamedTemplate(MVEL.executeExpression(cIncludeExpression, ctx, factory, String.class)), ctx, factory))), ctx, factory);
} else {
return appender.append(String.valueOf(TemplateRuntime.execute(runtime.getNamedTemplateRegistry().getNamedTemplate(MVEL.executeExpression(cIncludeExpression, ctx, factory, String.class)), ctx, factory, runtime.getNamedTemplateRegistry())));
}
}
Aggregations