use of org.mvel2.compiler.Accessor in project drools by kiegroup.
the class ConditionAnalyzer method analyzeNodeAccessor.
private Expression analyzeNodeAccessor(Accessor accessor, ASTNode node) {
AccessorNode accessorNode;
if (accessor instanceof DynamicGetAccessor) {
accessorNode = (AccessorNode) ((DynamicGetAccessor) accessor).getSafeAccessor();
} else if (accessor instanceof AccessorNode) {
accessorNode = (AccessorNode) accessor;
} else if (accessor instanceof CompiledExpression) {
return analyzeNode(((CompiledExpression) accessor).getFirstNode());
} else if (accessor instanceof ListCreator) {
return analyzeListCreation(((ListCreator) accessor));
} else if (accessor instanceof ArrayCreator) {
return analyzeArrayCreation(((ArrayCreator) accessor));
} else {
throw new RuntimeException("Unknown accessor type: " + accessor);
}
if (accessorNode instanceof VariableAccessor) {
if (isStaticAccessor(accessorNode)) {
while (accessorNode instanceof VariableAccessor) {
accessorNode = accessorNode.getNextNode();
}
} else {
return analyzeNodeAccessor(accessorNode, node);
}
}
while (accessorNode instanceof StaticReferenceAccessor) {
StaticReferenceAccessor staticReferenceAccessor = ((StaticReferenceAccessor) accessorNode);
Object literal = staticReferenceAccessor.getLiteral();
accessorNode = accessorNode.getNextNode();
if (accessorNode == null) {
return new FixedExpression(literal.getClass(), literal);
}
}
return analyzeExpressionNode(accessorNode, node, null);
}
use of org.mvel2.compiler.Accessor in project drools by kiegroup.
the class ConditionAnalyzer method getArrayCreationExpression.
private ArrayCreationExpression getArrayCreationExpression(Class<?> arrayType, Class<?> type, Accessor[] accessors) {
ArrayCreationExpression arrayExpression = new ArrayCreationExpression(arrayType);
for (Accessor accessor : accessors) {
ExecutableStatement statement = ((ExprValueAccessor) accessor).getStmt();
arrayExpression.addItem(statementToExpression(statement, type));
}
return arrayExpression;
}
use of org.mvel2.compiler.Accessor in project mvel by mikebrock.
the class ReflectiveAccessorOptimizer method optimizeCollection.
public Accessor optimizeCollection(ParserContext pCtx, Object o, Class type, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) {
this.start = this.cursor = start;
this.length = start + offset;
this.returnType = type;
this.ctx = ctx;
this.variableFactory = factory;
this.pCtx = pCtx;
Accessor root = _getAccessor(o, returnType);
if (property != null && length > start) {
return new Union(root, property, cursor, offset);
} else {
return root;
}
}
use of org.mvel2.compiler.Accessor 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.compiler.Accessor in project mvel by mikebrock.
the class ReflectiveAccessorOptimizer method get.
public static Object get(String expression, Object ctx) {
int hash = createSignatureHash(expression, ctx);
Accessor accessor = REFLECTIVE_ACCESSOR_CACHE.get(hash);
if (accessor != null) {
return accessor.getValue(ctx, null, null);
} else {
REFLECTIVE_ACCESSOR_CACHE.put(hash, accessor = new ReflectiveAccessorOptimizer().optimizeAccessor(getCurrentThreadParserContext(), expression.toCharArray(), 0, expression.length(), ctx, null, null, false, null));
return accessor.getValue(ctx, null, null);
}
}
Aggregations