Search in sources :

Example 11 with FieldAccessExpr

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

the class ExpressionTyper method processFirstNode.

private Optional<TypedExpressionCursor> processFirstNode(Expression drlxExpr, List<Node> childNodes, Node firstNode, boolean isInLineCast, Class<?> originalTypeCursor) {
    final Optional<TypedExpressionCursor> result;
    if (firstNode instanceof ThisExpr || (firstNode instanceof NameExpr && firstNode.toString().equals(bindingId))) {
        result = of(thisExpr(drlxExpr, childNodes, isInLineCast, originalTypeCursor));
    } else if (firstNode instanceof NameExpr) {
        result = nameExpr(drlxExpr, (NameExpr) firstNode, isInLineCast, originalTypeCursor);
    } else if (firstNode instanceof FieldAccessExpr && ((FieldAccessExpr) firstNode).getScope() instanceof ThisExpr) {
        result = of(fieldAccessExpr((FieldAccessExpr) firstNode, originalTypeCursor));
    } else if (firstNode instanceof MethodCallExpr) {
        result = of(methodCallExpr((MethodCallExpr) firstNode, originalTypeCursor, new NameExpr("_this")));
    } else if (firstNode instanceof StringLiteralExpr) {
        result = of(stringLiteralExpr((StringLiteralExpr) firstNode));
    } else if (firstNode instanceof EnclosedExpr) {
        result = processFirstNode(drlxExpr, childNodes, ((EnclosedExpr) firstNode).getInner(), isInLineCast, originalTypeCursor);
    } else if (firstNode instanceof CastExpr) {
        result = castExpr((CastExpr) firstNode, drlxExpr, childNodes, isInLineCast, originalTypeCursor);
    } else if (firstNode instanceof ArrayCreationExpr) {
        result = of(arrayCreationExpr(((ArrayCreationExpr) firstNode)));
    } else if (firstNode instanceof BinaryExpr) {
        result = of(binaryExpr((BinaryExpr) firstNode));
    } else {
        result = of(new TypedExpressionCursor((Expression) firstNode, getExpressionType(ruleContext, ruleContext.getTypeResolver(), (Expression) firstNode, context.getUsedDeclarations())));
    }
    result.ifPresent(te -> {
        if (drlxExpr instanceof NullSafeFieldAccessExpr) {
            extractPrefixExpressions((NullSafeFieldAccessExpr) drlxExpr, te.expressionCursor);
        }
    });
    return result.map(te -> {
        if (isInLineCast) {
            Expression exprWithInlineCast = addCastToExpression(te.typeCursor, te.expressionCursor, isInLineCast);
            return new TypedExpressionCursor(exprWithInlineCast, te.typeCursor);
        } else {
            return te;
        }
    });
}
Also used : BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) CastExpr(org.drools.javaparser.ast.expr.CastExpr) InlineCastExpr(org.drools.javaparser.ast.drlx.expr.InlineCastExpr) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) ArrayCreationExpr(org.drools.javaparser.ast.expr.ArrayCreationExpr) ThisExpr(org.drools.javaparser.ast.expr.ThisExpr) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 12 with FieldAccessExpr

use of org.drools.javaparser.ast.expr.FieldAccessExpr 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

FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)12 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)10 Expression (org.drools.javaparser.ast.expr.Expression)9 NameExpr (org.drools.javaparser.ast.expr.NameExpr)9 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)8 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)5 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)5 ArrayList (java.util.ArrayList)4 Optional (java.util.Optional)4 DrlxExpression (org.drools.javaparser.ast.drlx.expr.DrlxExpression)4 InlineCastExpr (org.drools.javaparser.ast.drlx.expr.InlineCastExpr)4 NullSafeFieldAccessExpr (org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr)4 DrlxParseUtil.nameExprToMethodCallExpr (org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr)4 List (java.util.List)3 Node (org.drools.javaparser.ast.Node)3 HalfBinaryExpr (org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr)3 CastExpr (org.drools.javaparser.ast.expr.CastExpr)3 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)3 ThisExpr (org.drools.javaparser.ast.expr.ThisExpr)3 UnaryExpr (org.drools.javaparser.ast.expr.UnaryExpr)3