use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class ExpressionTyper method resolveCreationLiteralNameExpr.
private Expression resolveCreationLiteralNameExpr(java.lang.reflect.Type originalTypeCursor, Expression expression) {
Expression result = expression;
if (result instanceof DrlNameExpr) {
TypedExpressionCursor typedExpressionCursor = drlNameExpr(null, (DrlNameExpr) result, false, originalTypeCursor).orElseThrow(() -> new RuntimeException("Cannot find field: " + expression));
result = typedExpressionCursor.expressionCursor;
}
return result;
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class ExpressionTyper method mapCreationLiteral.
private Optional<TypedExpressionCursor> mapCreationLiteral(MapCreationLiteralExpression mapCreationLiteralExpression, java.lang.reflect.Type originalTypeCursor) {
ClassOrInterfaceType hashMapType = (ClassOrInterfaceType) parseType(HashMap.class.getCanonicalName());
BlockStmt initializationStmt = new BlockStmt();
InitializerDeclaration body = new InitializerDeclaration(false, initializationStmt);
ObjectCreationExpr newHashMapExpr = new ObjectCreationExpr(null, hashMapType, nodeList(), nodeList(), nodeList(body));
for (Expression e : mapCreationLiteralExpression.getExpressions()) {
MapCreationLiteralExpressionKeyValuePair expr = (MapCreationLiteralExpressionKeyValuePair) e;
Expression key = resolveCreationLiteralNameExpr(originalTypeCursor, expr.getKey());
Expression value = resolveCreationLiteralNameExpr(originalTypeCursor, expr.getValue());
initializationStmt.addStatement(new MethodCallExpr(null, "put", nodeList(key, value)));
}
return of(new TypedExpressionCursor(newHashMapExpr, HashMap.class));
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class ExpressionTyper method promoteBigDecimalParameters.
private void promoteBigDecimalParameters(MethodCallExpr methodCallExpr, Class[] argsType, Class<?>[] actualArgumentTypes) {
if (actualArgumentTypes.length == argsType.length && actualArgumentTypes.length == methodCallExpr.getArguments().size()) {
for (int i = 0; i < argsType.length; i++) {
Class<?> argumentType = argsType[i];
Class<?> actualArgumentType = actualArgumentTypes[i];
Expression argumentExpression = methodCallExpr.getArgument(i);
if (argumentType != actualArgumentType) {
Expression coercedExpression = new BigDecimalArgumentCoercion().coercedArgument(argumentType, actualArgumentType, argumentExpression);
methodCallExpr.setArgument(i, coercedExpression);
}
}
}
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class ExpressionTyper method nameExpr.
private Optional<TypedExpression> nameExpr(String name, Class<?> typeCursor) {
TypedExpression expression = nameExprToMethodCallExpr(name, typeCursor, null, ruleContext);
if (expression != null) {
context.addReactOnProperties(name);
Expression plusThis = prepend(new NameExpr(THIS_PLACEHOLDER), expression.getExpression());
return of(new TypedExpression(plusThis, expression.getType(), name));
}
Optional<DeclarationSpec> decl = ruleContext.getDeclarationById(name);
if (decl.isPresent()) {
// then drlxExpr is a single NameExpr referring to a binding, e.g.: "$p1".
context.addUsedDeclarations(name);
decl.get().getBoundVariable().ifPresent(context::addReactOnProperties);
return of(new TypedExpression(new NameExpr(name), decl.get().getDeclarationClass()));
}
if (ruleContext.getQueryParameters().stream().anyMatch(qp -> qp.getName().equals(name))) {
// then drlxExpr is a single NameExpr referring to a query parameter, e.g.: "$p1".
context.addUsedDeclarations(name);
return of(new TypedExpression(new NameExpr(name)));
} else if (packageModel.getGlobals().containsKey(name)) {
Expression plusThis = new NameExpr(name);
context.addUsedDeclarations(name);
return of(new TypedExpression(plusThis, packageModel.getGlobals().get(name)));
} else if (isPositional || ruleContext.isQuery()) {
String unificationVariable = ruleContext.getOrCreateUnificationId(name);
expression = new UnificationTypedExpression(unificationVariable, typeCursor, name);
return of(expression);
}
return empty();
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class ExpressionTyper method parseNodeArguments.
private Class[] parseNodeArguments(NodeWithArguments<?> methodCallExpr) {
Class[] argsType = new Class[methodCallExpr.getArguments().size()];
context.setRegisterPropertyReactivity(false);
try {
for (int i = 0; i < methodCallExpr.getArguments().size(); i++) {
Expression arg = methodCallExpr.getArgument(i);
TypedExpressionResult typedArgumentResult = toTypedExpressionFromMethodCallOrField(arg);
Optional<TypedExpression> optTypedArgumentExpression = typedArgumentResult.getTypedExpression();
if (optTypedArgumentExpression.isPresent()) {
TypedExpression typedArgumentExpression = optTypedArgumentExpression.get();
argsType[i] = toRawClass(typedArgumentExpression.getType());
methodCallExpr.setArgument(i, typedArgumentExpression.getExpression());
} else {
argsType[i] = Object.class;
methodCallExpr.setArgument(i, arg);
}
}
} finally {
context.setRegisterPropertyReactivity(true);
}
return argsType;
}
Aggregations