use of org.mvel2.compiler.AccessorNode in project drools by kiegroup.
the class MVELConditionEvaluator method isEvaluated.
private static boolean isEvaluated(ASTNode node) {
node = unwrapSubstatement(node);
if (node == null) {
return true;
}
if (node instanceof Contains) {
return ((Contains) node).getFirstStatement().getAccessor() != null;
}
if (node instanceof BooleanNode) {
return isEvaluated(((BooleanNode) node).getLeft()) && isEvaluated(((BooleanNode) node).getRight());
}
Accessor accessor = node.getAccessor();
if (accessor == null) {
return node instanceof LiteralNode;
}
if (accessor instanceof AccessorNode) {
AccessorNode nextNode = ((AccessorNode) accessor).getNextNode();
if (nextNode instanceof MethodAccessor && ((MethodAccessor) nextNode).getParms() != null) {
for (ExecutableStatement param : ((MethodAccessor) nextNode).getParms()) {
if (!isFullyEvaluated(param)) {
return false;
}
}
}
}
return true;
}
use of org.mvel2.compiler.AccessorNode in project mvel by mvel.
the class ReflectiveAccessorOptimizer method compileConstructor.
@SuppressWarnings({ "WeakerAccess" })
private 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], paramTypeVarArgsSafe(cns.getParameterTypes(), i, cns.isVarArgs()));
}
parms = normalizeArgsForVarArgs(cns.getParameterTypes(), parms, cns.isVarArgs());
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 {
ClassLoader classLoader = pCtx != null ? pCtx.getClassLoader() : currentThread().getContextClassLoader();
Constructor<?> cns = Class.forName(new String(expression), true, classLoader).getConstructor(EMPTYCLS);
AccessorNode ca = new ConstructorAccessor(cns, null);
if (cnsRes.length > 1) {
// noinspection NullArgumentToVariableArgMethod
ReflectiveAccessorOptimizer compiledOptimizer = new ReflectiveAccessorOptimizer(pCtx, 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.compiler.AccessorNode in project mvel by mvel.
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.compiler.AccessorNode in project drools by kiegroup.
the class ConditionAnalyzer method analyzeAccessorInvocation.
private Invocation analyzeAccessorInvocation(AccessorNode accessorNode, ASTNode containingNode, Invocation formerInvocation, Class<?> variableType) {
if (accessorNode instanceof GetterAccessor) {
return new MethodInvocation(((GetterAccessor) accessorNode).getMethod(), variableType == null ? conditionClass : variableType.getName());
}
if (accessorNode instanceof MethodAccessor) {
MethodAccessor methodAccessor = (MethodAccessor) accessorNode;
Method method = methodAccessor.getMethod();
MethodInvocation invocation = new MethodInvocation(method);
boolean isVarArgs = method.isVarArgs();
readInvocationParams(invocation, methodAccessor.getParms(), methodAccessor.getParameterTypes(), isVarArgs);
return invocation;
}
if (accessorNode instanceof ConstructorAccessor) {
ConstructorAccessor constructorAccessor = (ConstructorAccessor) accessorNode;
Constructor constructor = constructorAccessor.getConstructor();
ConstructorInvocation invocation = new ConstructorInvocation(constructor);
readInvocationParams(invocation, constructorAccessor.getParameters(), constructorAccessor.getParameterTypes(), constructor.isVarArgs());
return invocation;
}
if (accessorNode instanceof ArrayAccessor) {
ArrayAccessor arrayAccessor = (ArrayAccessor) accessorNode;
return new ArrayAccessInvocation(formerInvocation != null ? formerInvocation.getReturnType() : Object[].class, new FixedExpression(int.class, arrayAccessor.getIndex()));
}
if (accessorNode instanceof ArrayAccessorNest) {
ArrayAccessorNest arrayAccessorNest = (ArrayAccessorNest) accessorNode;
ExecutableAccessor index = (ExecutableAccessor) arrayAccessorNest.getIndex();
return new ArrayAccessInvocation(formerInvocation != null ? formerInvocation.getReturnType() : Object[].class, analyzeNode(index.getNode()));
}
if (accessorNode instanceof ArrayLength) {
return new ArrayLengthInvocation();
}
if (accessorNode instanceof ListAccessor) {
Class<?> listType = getListType(formerInvocation);
ListAccessor listAccessor = (ListAccessor) accessorNode;
return new ListAccessInvocation(listType, new FixedExpression(int.class, listAccessor.getIndex()));
}
if (accessorNode instanceof ListAccessorNest) {
Class<?> listType = getListType(formerInvocation);
ListAccessorNest listAccessorNest = (ListAccessorNest) accessorNode;
ExecutableAccessor index = (ExecutableAccessor) listAccessorNest.getIndex();
return new ListAccessInvocation(listType, analyzeNode(index.getNode()));
}
if (accessorNode instanceof MapAccessor) {
MapAccessor mapAccessor = (MapAccessor) accessorNode;
return new MapAccessInvocation(Object.class, Object.class, new FixedExpression(Object.class, mapAccessor.getProperty()));
}
if (accessorNode instanceof MapAccessorNest) {
Class<?> keyType = Object.class;
Class<?> valueType = Object.class;
Type[] generics = getGenerics(formerInvocation);
if (generics != null && generics.length == 2 && generics[0] instanceof Class) {
keyType = (Class<?>) generics[0];
if (generics[1] instanceof Class)
valueType = (Class<?>) generics[1];
}
MapAccessorNest mapAccessor = (MapAccessorNest) accessorNode;
ExecutableStatement statement = mapAccessor.getProperty();
if (statement instanceof ExecutableLiteral) {
return new MapAccessInvocation(keyType, valueType, new FixedExpression(keyType, ((ExecutableLiteral) statement).getLiteral()));
} else {
return new MapAccessInvocation(keyType, valueType, analyzeNode(((ExecutableAccessor) statement).getNode()));
}
}
if (accessorNode instanceof FieldAccessor) {
return new FieldAccessInvocation(((FieldAccessor) accessorNode).getField());
}
if (accessorNode instanceof StaticVarAccessor) {
Field field = ((StaticVarAccessor) accessorNode).getField();
return new FieldAccessInvocation(field);
}
if (accessorNode instanceof ThisValueAccessor) {
return new ThisInvocation(accessorNode.getNextNode() == null ? containingNode.getEgressType() : Object.class);
}
throw new RuntimeException("Unknown AccessorNode type: " + accessorNode.getClass().getName());
}
use of org.mvel2.compiler.AccessorNode in project drools by kiegroup.
the class ModifyInterceptor method extractMethod.
private Method extractMethod(WithNode.ParmValuePair parmValuePair) {
Serializable setExpression = parmValuePair.getSetExpression();
if (setExpression != null) {
SetterAccessor setterAccessor = (SetterAccessor) ((CompiledAccExpression) setExpression).getAccessor();
return setterAccessor.getMethod();
} else {
ExecutableAccessor accessor = (ExecutableAccessor) parmValuePair.getStatement();
AccessorNode accessorNode = (AccessorNode) accessor.getNode().getAccessor();
MethodAccessor methodAccessor = (MethodAccessor) accessorNode.getNextNode();
return methodAccessor.getMethod();
}
}
Aggregations