Search in sources :

Example 1 with ArrayCreationExpr

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

the class DrlxParseUtil method getExpressionType.

public static Class<?> getExpressionType(RuleContext context, TypeResolver typeResolver, Expression expr, Collection<String> usedDeclarations) {
    if (expr instanceof LiteralExpr) {
        return getLiteralExpressionType((LiteralExpr) expr);
    }
    if (expr instanceof ArrayAccessExpr) {
        return getClassFromContext(typeResolver, ((ArrayCreationExpr) ((ArrayAccessExpr) expr).getName()).getElementType().asString());
    }
    if (expr instanceof ArrayCreationExpr) {
        return getClassFromContext(typeResolver, ((ArrayCreationExpr) expr).getElementType().asString());
    }
    if (expr instanceof NameExpr) {
        String name = ((NameExpr) expr).getNameAsString();
        if (usedDeclarations != null) {
            usedDeclarations.add(name);
        }
        return context.getDeclarationById(name).map(DeclarationSpec::getDeclarationClass).get();
    }
    if (expr instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) expr;
        Class<?> scopeType = getExpressionType(context, typeResolver, methodCallExpr.getScope().get(), usedDeclarations);
        return returnTypeOfMethodCallExpr(context, typeResolver, methodCallExpr, scopeType, usedDeclarations);
    }
    if (expr instanceof ObjectCreationExpr) {
        final ClassOrInterfaceType type = ((ObjectCreationExpr) expr).getType();
        return getClassFromContext(typeResolver, type.asString());
    }
    if (expr.isCastExpr()) {
        String typeName = expr.asCastExpr().getType().toString();
        try {
            return typeResolver.resolveType(expr.asCastExpr().getType().toString());
        } catch (ClassNotFoundException e) {
            context.addCompilationError(new InvalidExpressionErrorResult("Unknown type in cast expression: " + typeName));
            throw new RuntimeException("Unknown type in cast expression: " + typeName);
        }
    }
    throw new RuntimeException("Unknown expression type: " + expr);
}
Also used : ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ArrayAccessExpr(org.drools.javaparser.ast.expr.ArrayAccessExpr) ExpressionTyper.findLeftLeafOfNameExpr(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper.findLeftLeafOfNameExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) LongLiteralExpr(org.drools.javaparser.ast.expr.LongLiteralExpr) CharLiteralExpr(org.drools.javaparser.ast.expr.CharLiteralExpr) NullLiteralExpr(org.drools.javaparser.ast.expr.NullLiteralExpr) BooleanLiteralExpr(org.drools.javaparser.ast.expr.BooleanLiteralExpr) IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) DoubleLiteralExpr(org.drools.javaparser.ast.expr.DoubleLiteralExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) ArrayCreationExpr(org.drools.javaparser.ast.expr.ArrayCreationExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 2 with ArrayCreationExpr

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

Aggregations

ArrayCreationExpr (org.drools.javaparser.ast.expr.ArrayCreationExpr)2 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)2 NameExpr (org.drools.javaparser.ast.expr.NameExpr)2 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)2 HalfBinaryExpr (org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr)1 InlineCastExpr (org.drools.javaparser.ast.drlx.expr.InlineCastExpr)1 NullSafeFieldAccessExpr (org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr)1 ArrayAccessExpr (org.drools.javaparser.ast.expr.ArrayAccessExpr)1 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)1 BooleanLiteralExpr (org.drools.javaparser.ast.expr.BooleanLiteralExpr)1 CastExpr (org.drools.javaparser.ast.expr.CastExpr)1 CharLiteralExpr (org.drools.javaparser.ast.expr.CharLiteralExpr)1 DoubleLiteralExpr (org.drools.javaparser.ast.expr.DoubleLiteralExpr)1 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)1 Expression (org.drools.javaparser.ast.expr.Expression)1 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)1 IntegerLiteralExpr (org.drools.javaparser.ast.expr.IntegerLiteralExpr)1 LiteralExpr (org.drools.javaparser.ast.expr.LiteralExpr)1 LongLiteralExpr (org.drools.javaparser.ast.expr.LongLiteralExpr)1 NullLiteralExpr (org.drools.javaparser.ast.expr.NullLiteralExpr)1