use of org.mule.mvel2.compiler.ExecutableLiteral in project mule by mulesoft.
the class AbstractVariableEnricherDataTypePropagator method doPropagate.
@Override
protected boolean doPropagate(PrivilegedEvent event, PrivilegedEvent.Builder builder, TypedValue typedValue, ASTNode node) {
if (isAssignmentNode(node)) {
CompiledAccExpression compiledAccExpression = getCompiledAccExpression(node);
if (compiledAccExpression.getAccessor() instanceof VariableAccessor) {
VariableAccessor variableAccessor = (VariableAccessor) compiledAccExpression.getAccessor();
if (variableAccessor.getProperty().equals(propertyName)) {
final AccessorNode nextNode = variableAccessor.getNextNode();
if (nextNode.getNextNode() == null) {
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 && containsVariable(event, propertyName)) {
propertyName = getUnescapedPropertyName(propertyName);
addVariable(event, builder, typedValue, propertyName);
return true;
}
}
}
}
}
return false;
}
use of org.mule.mvel2.compiler.ExecutableLiteral 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.mule.mvel2.compiler.ExecutableLiteral 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.mule.mvel2.compiler.ExecutableLiteral in project drools by kiegroup.
the class ConditionAnalyzer method analyzeNode.
private Expression analyzeNode(ASTNode node) {
node = analyzeRegEx(analyzeSubstatement(node));
if (node instanceof LiteralNode) {
LiteralNode literalNode = (LiteralNode) node;
return new FixedExpression(literalNode.getEgressType(), literalNode.getLiteralValue());
}
if (node instanceof BinaryOperation) {
BinaryOperation op = (BinaryOperation) node;
return new AritmeticExpression(analyzeNode(op.getLeft()), AritmeticOperator.fromMvelOpCode(op), analyzeNode(op.getRight()));
}
if (node instanceof TypeCast) {
ExecutableStatement statement = ((TypeCast) node).getStatement();
if (statement instanceof ExecutableAccessor) {
ExecutableAccessor accessor = (ExecutableAccessor) statement;
return new CastExpression(node.getEgressType(), analyzeNode(accessor.getNode()));
} else {
ExecutableLiteral literal = (ExecutableLiteral) statement;
return new CastExpression(node.getEgressType(), new FixedExpression(literal.getLiteral()));
}
}
if (node instanceof Union) {
ASTNode main = ((Union) node).getMain();
Accessor accessor = node.getAccessor();
EvaluatedExpression expression = new EvaluatedExpression();
expression.firstExpression = analyzeNode(main);
if (accessor instanceof DynamicGetAccessor) {
AccessorNode accessorNode = (AccessorNode) ((DynamicGetAccessor) accessor).getSafeAccessor();
expression.addInvocation(analyzeAccessorInvocation(accessorNode, node, null, null));
} else if (accessor instanceof AccessorNode) {
AccessorNode accessorNode = (AccessorNode) accessor;
while (accessorNode != null) {
expression.addInvocation(analyzeAccessorInvocation(accessorNode, node, null, null));
accessorNode = accessorNode.getNextNode();
}
} else {
throw new RuntimeException("Unexpected accessor: " + accessor);
}
return expression;
}
if (node instanceof Sign) {
ExecutableStatement statement = getFieldValue(Sign.class, "stmt", (Sign) node);
if (statement instanceof ExecutableAccessor) {
ExecutableAccessor accessor = (ExecutableAccessor) statement;
return new AritmeticExpression(new FixedExpression(0), AritmeticOperator.SUB, analyzeNode(accessor.getNode()));
} else {
ExecutableLiteral literal = (ExecutableLiteral) statement;
return new AritmeticExpression(new FixedExpression(0), AritmeticOperator.SUB, new FixedExpression(literal.getLiteral()));
}
}
Accessor accessor = node.getAccessor();
if (accessor instanceof IndexedVariableAccessor) {
String variableName = node.getName();
int dot = variableName.indexOf('.');
if (dot > 0) {
variableName = variableName.substring(0, dot);
}
Class<?> variableType = getVariableType(variableName);
return new VariableExpression(variableName, analyzeExpressionNode(((AccessorNode) accessor).getNextNode(), node, variableType), variableType != null ? variableType : node.getEgressType());
}
if (accessor == null && node instanceof NewObjectNode) {
accessor = ((NewObjectNode) node).getNewObjectOptimizer();
}
if (accessor instanceof VariableAccessor) {
VariableAccessor variableAccessor = (VariableAccessor) accessor;
AccessorNode accessorNode = variableAccessor.getNextNode();
if (accessorNode == null || !isStaticAccessor(accessorNode)) {
String variableName = (String) (variableAccessor.getProperty());
Class<?> variableType = getVariableType(variableName);
if (variableType != null) {
return new VariableExpression(variableName, analyzeExpressionNode(accessorNode, node, variableType), variableType);
} else {
if (node.getLiteralValue() instanceof ParserContext) {
ParserContext pCtx = (ParserContext) node.getLiteralValue();
// it's not a variable but a method invocation on this
Class<?> thisClass = pCtx.getInputs().get("this");
try {
return new EvaluatedExpression(new MethodInvocation(thisClass.getMethod(variableName)));
} catch (NoSuchMethodException e) {
if (node.getEgressType() == Class.class) {
// there's no method on this with the given name, check if it is a class literal
Class<?> classLiteral = pCtx.getParserConfiguration().getImport(variableName);
if (classLiteral != null) {
return new FixedExpression(Class.class, classLiteral);
}
}
throw new RuntimeException(e);
}
}
}
}
}
if (accessor == null) {
throw new RuntimeException("Null accessor on node: " + node);
}
return analyzeNodeAccessor(accessor, node);
}
use of org.mule.mvel2.compiler.ExecutableLiteral 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