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));
}
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);
}
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;
}
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());
}
Aggregations