Search in sources :

Example 1 with ConditionalExpr

use of com.github.javaparser.ast.expr.ConditionalExpr in project drools by kiegroup.

the class DrlxParseUtil method getExpressionType.

public static java.lang.reflect.Type getExpressionType(RuleContext context, TypeResolver typeResolver, Expression expr, Collection<String> usedDeclarations) {
    if (expr instanceof LiteralExpr) {
        return getLiteralExpressionType((LiteralExpr) expr);
    }
    if (expr instanceof UnaryExpr) {
        return getExpressionType(context, typeResolver, expr.asUnaryExpr().getExpression(), usedDeclarations);
    }
    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 MapCreationLiteralExpression) {
        return Map.class;
    }
    if (expr instanceof ListCreationLiteralExpression) {
        return List.class;
    }
    if (expr instanceof NameExpr) {
        return expressionTypeNameExpr(context, usedDeclarations, ((NameExpr) expr).getNameAsString());
    }
    if (expr instanceof DrlNameExpr) {
        return expressionTypeNameExpr(context, usedDeclarations, ((DrlNameExpr) expr).getNameAsString());
    }
    if (expr instanceof BinaryExpr) {
        return boolean.class;
    }
    if (expr instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) expr;
        Optional<Expression> scopeExpression = methodCallExpr.getScope();
        if (scopeExpression.isPresent()) {
            java.lang.reflect.Type scopeType = getExpressionType(context, typeResolver, scopeExpression.get(), usedDeclarations);
            return returnTypeOfMethodCallExpr(context, typeResolver, methodCallExpr, scopeType, usedDeclarations);
        } else {
            throw new IllegalStateException("Scope expression is not present for " + ((MethodCallExpr) expr).getNameAsString() + "!");
        }
    }
    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);
        }
    }
    if (expr instanceof ConditionalExpr) {
        ConditionalExpr ternaryExpr = ((ConditionalExpr) expr);
        java.lang.reflect.Type conditionType = getExpressionType(context, typeResolver, ternaryExpr.getCondition(), usedDeclarations);
        if (conditionType != Boolean.class && conditionType != boolean.class) {
            context.addCompilationError(new InvalidExpressionErrorResult("Condtion used in ternary expression '" + expr + "' isn't boolean"));
            return Object.class;
        }
        java.lang.reflect.Type leftType = getExpressionType(context, typeResolver, ternaryExpr.getThenExpr(), usedDeclarations);
        java.lang.reflect.Type rightType = getExpressionType(context, typeResolver, ternaryExpr.getElseExpr(), usedDeclarations);
        Class<?> leftClass = toRawClass(leftType);
        Class<?> rightClass = toRawClass(rightType);
        if (leftClass.isAssignableFrom(rightClass)) {
            return leftType;
        }
        if (rightClass.isAssignableFrom(leftClass)) {
            return rightType;
        }
        return Object.class;
    }
    if (expr.isClassExpr()) {
        return Class.class;
    }
    throw new RuntimeException("Unknown expression type: " + PrintUtil.printNode(expr));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Collectors.toList(java.util.stream.Collectors.toList) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) ArrayAccessExpr(com.github.javaparser.ast.expr.ArrayAccessExpr) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) ConditionalExpr(com.github.javaparser.ast.expr.ConditionalExpr) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) Expression(com.github.javaparser.ast.expr.Expression) ListCreationLiteralExpression(org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) MapCreationLiteralExpression(org.drools.mvel.parser.ast.expr.MapCreationLiteralExpression) ClassUtil.toRawClass(org.drools.modelcompiler.util.ClassUtil.toRawClass) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 2 with ConditionalExpr

use of com.github.javaparser.ast.expr.ConditionalExpr in project drools by kiegroup.

the class ASTCompilerVisitor method visit.

@Override
public DirectCompilerResult visit(IfExpressionNode n) {
    DirectCompilerResult condition = n.getCondition().accept(this);
    DirectCompilerResult thenExpr = n.getThenExpression().accept(this);
    DirectCompilerResult elseExpr = n.getElseExpression().accept(this);
    return DirectCompilerResult.of(new ConditionalExpr(new BinaryExpr(Expressions.nativeInstanceOf(Constants.BooleanT, condition.getExpression()), Expressions.reflectiveCastTo(Constants.BooleanT, condition.getExpression()), BinaryExpr.Operator.AND), new EnclosedExpr(thenExpr.getExpression()), new EnclosedExpr(elseExpr.getExpression())), // should find common type between then/else
    thenExpr.resultType).withFD(condition).withFD(thenExpr).withFD(elseExpr);
}
Also used : BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) ConditionalExpr(com.github.javaparser.ast.expr.ConditionalExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr)

Example 3 with ConditionalExpr

use of com.github.javaparser.ast.expr.ConditionalExpr in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitConditionalExpression.

@Override
public Void visitConditionalExpression(ConditionalExpressionTree javacTree, Node javaParserNode) {
    ConditionalExpr node = castNode(ConditionalExpr.class, javaParserNode, javacTree);
    processConditionalExpression(javacTree, node);
    javacTree.getCondition().accept(this, node.getCondition());
    javacTree.getTrueExpression().accept(this, node.getThenExpr());
    javacTree.getFalseExpression().accept(this, node.getElseExpr());
    return null;
}
Also used : ConditionalExpr(com.github.javaparser.ast.expr.ConditionalExpr)

Example 4 with ConditionalExpr

use of com.github.javaparser.ast.expr.ConditionalExpr in project checker-framework by typetools.

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final ConditionalExpr node1, final Node other) {
    ConditionalExpr node2 = (ConditionalExpr) other;
    defaultAction(node1, node2);
    node1.getCondition().accept(this, node2.getCondition());
    node1.getElseExpr().accept(this, node2.getElseExpr());
    node1.getThenExpr().accept(this, node2.getThenExpr());
}
Also used : ConditionalExpr(com.github.javaparser.ast.expr.ConditionalExpr)

Aggregations

ConditionalExpr (com.github.javaparser.ast.expr.ConditionalExpr)4 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)2 NodeList (com.github.javaparser.ast.NodeList)1 ArrayAccessExpr (com.github.javaparser.ast.expr.ArrayAccessExpr)1 ArrayCreationExpr (com.github.javaparser.ast.expr.ArrayCreationExpr)1 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)1 CharLiteralExpr (com.github.javaparser.ast.expr.CharLiteralExpr)1 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)1 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)1 LiteralExpr (com.github.javaparser.ast.expr.LiteralExpr)1 LongLiteralExpr (com.github.javaparser.ast.expr.LongLiteralExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 UnaryExpr (com.github.javaparser.ast.expr.UnaryExpr)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1