Search in sources :

Example 1 with InlineCastExpr

use of org.drools.javaparser.ast.drlx.expr.InlineCastExpr in project drools by kiegroup.

the class ExpressionTyper method flattenScope.

private static List<Node> flattenScope(Expression expressionWithScope) {
    List<Node> res = new ArrayList<>();
    if (expressionWithScope instanceof FieldAccessExpr) {
        FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) expressionWithScope;
        res.addAll(flattenScope(fieldAccessExpr.getScope()));
        res.add(fieldAccessExpr.getName());
    } else if (expressionWithScope instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) expressionWithScope;
        if (methodCallExpr.getScope().isPresent()) {
            res.addAll(flattenScope(methodCallExpr.getScope().get()));
        }
        res.add(methodCallExpr.setScope(null));
    } else if (expressionWithScope instanceof InlineCastExpr && ((InlineCastExpr) expressionWithScope).getExpression() instanceof FieldAccessExpr) {
        InlineCastExpr inlineCastExpr = (InlineCastExpr) expressionWithScope;
        Expression internalScope = ((FieldAccessExpr) inlineCastExpr.getExpression()).getScope();
        res.addAll(flattenScope((internalScope)));
        res.add(expressionWithScope);
    } else {
        res.add(expressionWithScope);
    }
    return res;
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Node(org.drools.javaparser.ast.Node) ArrayList(java.util.ArrayList) InlineCastExpr(org.drools.javaparser.ast.drlx.expr.InlineCastExpr) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 2 with InlineCastExpr

use of org.drools.javaparser.ast.drlx.expr.InlineCastExpr in project drools by kiegroup.

the class ExpressionTyper method toTypedExpressionFromMethodCallOrField.

private TypedExpressionResult toTypedExpressionFromMethodCallOrField(Expression drlxExpr) {
    final List<Node> childrenNodes = flattenScope(drlxExpr);
    final Node firstChild = childrenNodes.get(0);
    boolean isInLineCast = firstChild instanceof InlineCastExpr;
    final Class<?> originalTypeCursor;
    final Node firstNode;
    if (isInLineCast) {
        InlineCastExpr inlineCast = (InlineCastExpr) firstChild;
        originalTypeCursor = originalTypeCursorFromInlineCast(inlineCast);
        firstNode = inlineCast.getExpression();
    } else {
        originalTypeCursor = patternType;
        firstNode = firstChild;
    }
    final Optional<TypedExpressionCursor> teCursor = processFirstNode(drlxExpr, childrenNodes, firstNode, isInLineCast, originalTypeCursor);
    Expression previous;
    Class<?> typeCursor;
    if (!teCursor.isPresent()) {
        return new TypedExpressionResult(Optional.empty(), context);
    } else {
        previous = teCursor.get().expressionCursor;
        typeCursor = teCursor.get().typeCursor;
    }
    List<Node> childrenWithoutFirst = childrenNodes.subList(1, childrenNodes.size());
    for (Node part : childrenWithoutFirst) {
        if (typeCursor.isEnum()) {
            previous = drlxExpr;
        } else if (part instanceof SimpleName) {
            String field = part.toString();
            TypedExpression expression = nameExprToMethodCallExpr(field, typeCursor, previous);
            typeCursor = expression.getType();
            previous = expression.getExpression();
        } else if (part instanceof MethodCallExpr) {
            TypedExpressionCursor typedExpr = methodCallExpr((MethodCallExpr) part, typeCursor, previous);
            typeCursor = typedExpr.typeCursor;
            previous = typedExpr.expressionCursor;
        } else if (part instanceof InlineCastExpr && ((InlineCastExpr) part).getExpression() instanceof FieldAccessExpr) {
            InlineCastExpr inlineCastExprPart = (InlineCastExpr) part;
            final FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) inlineCastExprPart.getExpression();
            final TypedExpression toMethodCallExpr = nameExprToMethodCallExpr(fieldAccessExpr.getNameAsString(), typeCursor, previous);
            final Class<?> castClass = getClassFromType(ruleContext.getTypeResolver(), inlineCastExprPart.getType());
            previous = addCastToExpression(castClass, toMethodCallExpr.getExpression(), false);
        } else {
            throw new UnsupportedOperationException();
        }
    }
    return new TypedExpressionResult(of(new TypedExpression().setExpression(previous).setType(typeCursor)), context);
}
Also used : Node(org.drools.javaparser.ast.Node) SimpleName(org.drools.javaparser.ast.expr.SimpleName) InlineCastExpr(org.drools.javaparser.ast.drlx.expr.InlineCastExpr) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

Node (org.drools.javaparser.ast.Node)2 InlineCastExpr (org.drools.javaparser.ast.drlx.expr.InlineCastExpr)2 NullSafeFieldAccessExpr (org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr)2 Expression (org.drools.javaparser.ast.expr.Expression)2 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)2 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)2 DrlxParseUtil.nameExprToMethodCallExpr (org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr)2 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)2 ArrayList (java.util.ArrayList)1 SimpleName (org.drools.javaparser.ast.expr.SimpleName)1