Search in sources :

Example 46 with Expression

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;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr)

Example 47 with Expression

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));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) Expression(com.github.javaparser.ast.expr.Expression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) HashMap(java.util.HashMap) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) MapCreationLiteralExpressionKeyValuePair(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpressionKeyValuePair) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) NullSafeMethodCallExpr(org.drools.mvel.parser.ast.expr.NullSafeMethodCallExpr) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr)

Example 48 with Expression

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);
            }
        }
    }
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) BigDecimalArgumentCoercion(org.drools.mvelcompiler.util.BigDecimalArgumentCoercion)

Example 49 with Expression

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();
}
Also used : DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) Expression(com.github.javaparser.ast.expr.Expression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseUtil.transformDrlNameExprToNameExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.transformDrlNameExprToNameExpr) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Example 50 with Expression

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;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) DrlxParseUtil.isThisExpression(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isThisExpression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) ClassUtil.toRawClass(org.drools.modelcompiler.util.ClassUtil.toRawClass) UnificationTypedExpression(org.drools.modelcompiler.builder.generator.UnificationTypedExpression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)429 Test (org.junit.Test)166 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)149 NameExpr (com.github.javaparser.ast.expr.NameExpr)135 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)94 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)90 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)84 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)73 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)71 NodeList (com.github.javaparser.ast.NodeList)66 DrlxParser.parseExpression (org.drools.mvel.parser.DrlxParser.parseExpression)61 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)53 CompilationUnit (com.github.javaparser.ast.CompilationUnit)51 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)50 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)49 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)49 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)46 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)44 List (java.util.List)44 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)43