use of org.mule.mvel2.compiler.Accessor in project mvel by mvel.
the class ASMAccessorOptimizer method optimizeCollection.
public Accessor optimizeCollection(ParserContext pCtx, Object o, Class type, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) {
this.expr = property;
this.cursor = this.start = start;
this.end = start + offset;
this.length = offset;
this.returnType = type;
this.compiledInputs = new ArrayList<ExecutableStatement>();
this.ctx = ctx;
this.thisRef = thisRef;
this.variableFactory = factory;
this.pCtx = pCtx;
_initJIT();
literal = true;
_getAccessor(o, type);
_finishJIT();
try {
Accessor compiledAccessor = _initializeAccessor();
if (property != null && length > start) {
return new Union(pCtx, compiledAccessor, property, start, length);
} else {
return compiledAccessor;
}
} catch (Exception e) {
throw new OptimizationFailure("could not optimize collection", e);
}
}
use of org.mule.mvel2.compiler.Accessor in project mvel by mvel.
the class ReflectiveAccessorOptimizer method _getAccessor.
private Accessor _getAccessor(Object o, Class type) {
if (o instanceof List) {
Accessor[] a = new Accessor[((List) o).size()];
int i = 0;
for (Object item : (List) o) {
a[i++] = _getAccessor(item, type);
}
returnType = List.class;
return new ListCreator(a);
} else if (o instanceof Map) {
Accessor[] k = new Accessor[((Map) o).size()];
Accessor[] v = new Accessor[k.length];
int i = 0;
for (Object item : ((Map) o).keySet()) {
// key
k[i] = _getAccessor(item, type);
// value
v[i++] = _getAccessor(((Map) o).get(item), type);
}
returnType = Map.class;
return new MapCreator(k, v);
} else if (o instanceof Object[]) {
Accessor[] a = new Accessor[((Object[]) o).length];
int i = 0;
int dim = 0;
if (type != null) {
String nm = type.getName();
while (nm.charAt(dim) == '[') dim++;
} else {
type = Object[].class;
dim = 1;
}
try {
Class base = getBaseComponentType(type);
Class cls = dim > 1 ? findClass(null, repeatChar('[', dim - 1) + "L" + base.getName() + ";", pCtx) : type;
for (Object item : (Object[]) o) {
expectType(pCtx, a[i++] = _getAccessor(item, cls), base, true);
}
return new ArrayCreator(a, getSubComponentType(type));
} catch (ClassNotFoundException e) {
throw new RuntimeException("this error should never throw:" + getBaseComponentType(type).getName(), e);
}
} else {
if (returnType == null)
returnType = Object.class;
if (type.isArray()) {
return new ExprValueAccessor((String) o, type, ctx, variableFactory, pCtx);
} else {
return new ExprValueAccessor((String) o, Object.class, ctx, variableFactory, pCtx);
}
}
}
use of org.mule.mvel2.compiler.Accessor in project mule by mulesoft.
the class AbstractVarAssignmentDataTypePropagatorTestCase method compileMelExpression.
private CompiledExpression compileMelExpression(String expression, PrivilegedEvent testEvent, PrivilegedEvent.Builder builder) {
final ParserConfiguration parserConfiguration = MVELExpressionLanguage.createParserConfiguration(Collections.EMPTY_MAP);
final MVELExpressionLanguageContext context = createMvelExpressionLanguageContext(testEvent, builder, parserConfiguration);
CompiledExpression compiledExpression = (CompiledExpression) compileExpression(expression, new ParserContext(parserConfiguration));
// Expression must be executed, otherwise the variable accessor is not properly configured
MVEL.executeExpression(compiledExpression, context);
return compiledExpression;
}
use of org.mule.mvel2.compiler.Accessor in project mule by mulesoft.
the class AbstractVariableExpressionDataTypeResolver method getDataType.
@Override
protected DataType getDataType(PrivilegedEvent event, ASTNode node) {
final Accessor accessor = node.getAccessor();
if (accessor instanceof VariableAccessor) {
VariableAccessor variableAccessor = (VariableAccessor) accessor;
if (variableAccessor.getProperty().equals(propertyName)) {
final AccessorNode nextNode = variableAccessor.getNextNode();
String propertyName = null;
if (nextNode instanceof MapAccessorNest) {
final MapAccessorNest mapAccesorNest = (MapAccessorNest) nextNode;
if (mapAccesorNest.getProperty().isLiteralOnly()) {
propertyName = (String) ((ExecutableLiteral) mapAccesorNest.getProperty()).getLiteral();
}
} else if (nextNode instanceof MapAccessor) {
propertyName = (String) ((MapAccessor) nextNode).getProperty();
}
if (propertyName != null) {
return getVariableDataType(event, propertyName);
}
}
}
return null;
}
Aggregations