use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.
the class DroolsMvelParserTest method testAndWithImplicitParameterAndParenthesisMixedRight.
@Test
public void testAndWithImplicitParameterAndParenthesisMixedRight() {
String expr = "value (< 1 || (> 2 && < 3))";
Expression expression = parseExpression(parser, expr).getExpr();
BinaryExpr comboExpr = ((BinaryExpr) expression);
assertEquals(Operator.OR, comboExpr.getOperator());
BinaryExpr first = ((BinaryExpr) comboExpr.getLeft());
assertEquals("value", toString(first.getLeft()));
assertEquals("1", toString(first.getRight()));
assertEquals(Operator.LESS, first.getOperator());
BinaryExpr comboExprRight = ((BinaryExpr) comboExpr.getRight());
assertEquals(Operator.AND, comboExprRight.getOperator());
BinaryExpr third = (BinaryExpr) comboExprRight.getLeft();
assertEquals("value", toString(third.getLeft()));
assertEquals("2", toString(third.getRight()));
assertEquals(Operator.GREATER, third.getOperator());
HalfBinaryExpr forth = (HalfBinaryExpr) comboExprRight.getRight();
assertEquals("3", toString(forth.getRight()));
assertEquals(HalfBinaryExpr.Operator.LESS, forth.getOperator());
}
use of com.github.javaparser.ast.expr.BinaryExpr 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.BinaryExpr in project checker-framework by typetools.
the class JointJavacJavaParserVisitor method visitBinary.
@Override
public Void visitBinary(BinaryTree javacTree, Node javaParserNode) {
BinaryExpr node = castNode(BinaryExpr.class, javaParserNode, javacTree);
processBinary(javacTree, node);
javacTree.getLeftOperand().accept(this, node.getLeft());
javacTree.getRightOperand().accept(this, node.getRight());
return null;
}
use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.
the class ExpressionTyper method processFirstNode.
private Optional<TypedExpressionCursor> processFirstNode(Expression drlxExpr, List<Node> childNodes, Node firstNode, boolean isInLineCast, java.lang.reflect.Type originalTypeCursor) {
Optional<TypedExpressionCursor> result;
if (isThisExpression(firstNode) || (firstNode instanceof DrlNameExpr && printNode(firstNode).equals(bindingId))) {
result = of(thisExpr(drlxExpr, childNodes, isInLineCast, originalTypeCursor));
} else if (firstNode instanceof DrlNameExpr) {
result = drlNameExpr(drlxExpr, (DrlNameExpr) firstNode, isInLineCast, originalTypeCursor);
} else if (firstNode instanceof NameExpr) {
result = drlNameExpr(drlxExpr, new DrlNameExpr(((NameExpr) firstNode).getName()), isInLineCast, originalTypeCursor);
} else if (firstNode instanceof FieldAccessExpr) {
if (((FieldAccessExpr) firstNode).getScope() instanceof ThisExpr) {
result = of(fieldAccessExpr(originalTypeCursor, ((FieldAccessExpr) firstNode).getName()));
} else {
try {
Class<?> resolvedType = ruleContext.getTypeResolver().resolveType(PrintUtil.printNode(firstNode));
result = of(new TypedExpressionCursor(new NameExpr(PrintUtil.printNode(firstNode)), resolvedType));
} catch (ClassNotFoundException e) {
result = empty();
}
}
} else if (firstNode instanceof NullSafeFieldAccessExpr && ((NullSafeFieldAccessExpr) firstNode).getScope() instanceof ThisExpr) {
result = of(fieldAccessExpr(originalTypeCursor, ((NullSafeFieldAccessExpr) firstNode).getName()));
} else if (firstNode instanceof MethodCallExpr) {
Optional<Expression> scopeExpr = ((MethodCallExpr) firstNode).getScope();
Optional<DeclarationSpec> scopeDecl = scopeExpr.flatMap(scope -> ruleContext.getDeclarationById(PrintUtil.printNode(scope)));
Expression scope;
java.lang.reflect.Type type;
if (scopeDecl.isPresent() && !scopeDecl.get().getBindingId().equals(bindingId)) {
type = scopeDecl.get().getDeclarationClass();
scope = new NameExpr(scopeDecl.get().getBindingId());
context.addUsedDeclarations(scopeDecl.get().getBindingId());
} else if (scopeExpr.isPresent()) {
TypedExpressionCursor parsedScope = processFirstNode(drlxExpr, childNodes, scopeExpr.get(), isInLineCast, originalTypeCursor).get();
type = parsedScope.typeCursor;
scope = parsedScope.expressionCursor;
} else {
type = originalTypeCursor;
scope = new NameExpr(THIS_PLACEHOLDER);
}
result = of(methodCallExpr((MethodCallExpr) firstNode, type, scope));
} else if (firstNode instanceof ObjectCreationExpr) {
result = of(objectCreationExpr((ObjectCreationExpr) firstNode));
} 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 if (firstNode instanceof ArrayAccessExpr) {
Optional<DeclarationSpec> scopeDecl = ruleContext.getDeclarationById(((ArrayAccessExpr) firstNode).getName().toString());
Expression scope;
java.lang.reflect.Type type;
if (scopeDecl.isPresent() && !scopeDecl.get().getBindingId().equals(bindingId)) {
type = scopeDecl.get().getDeclarationClass();
scope = new NameExpr(scopeDecl.get().getBindingId());
context.addUsedDeclarations(scopeDecl.get().getBindingId());
} else {
type = originalTypeCursor;
scope = new NameExpr(THIS_PLACEHOLDER);
}
result = arrayAccessExpr((ArrayAccessExpr) firstNode, type, scope);
} else if (firstNode instanceof MapCreationLiteralExpression) {
result = mapCreationLiteral((MapCreationLiteralExpression) firstNode, originalTypeCursor);
} else if (firstNode instanceof ListCreationLiteralExpression) {
result = listCreationLiteral((ListCreationLiteralExpression) firstNode, originalTypeCursor);
} else {
result = of(new TypedExpressionCursor((Expression) firstNode, getExpressionType(ruleContext, ruleContext.getTypeResolver(), (Expression) firstNode, context.getUsedDeclarations())));
}
if (result.isPresent()) {
processNullSafeDereferencing(drlxExpr);
}
return result.map(te -> {
if (isInLineCast) {
Expression exprWithInlineCast = addCastToExpression(toRawClass(te.typeCursor), te.expressionCursor, isInLineCast);
return new TypedExpressionCursor(exprWithInlineCast, te.typeCursor);
} else {
return te;
}
});
}
use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.
the class ConstraintParser method handleSpecialComparisonCases.
private Expression handleSpecialComparisonCases(ExpressionTyper expressionTyper, TypedExpression typedExpression) {
if (typedExpression.getExpression() instanceof BinaryExpr && isComparisonOperator(((BinaryExpr) typedExpression.getExpression()).getOperator())) {
BinaryExpr binaryExpr = (BinaryExpr) typedExpression.getExpression();
Optional<TypedExpression> leftTyped = expressionTyper.toTypedExpression(binaryExpr.getLeft()).getTypedExpression();
Optional<TypedExpression> rightTyped = expressionTyper.toTypedExpression(binaryExpr.getRight()).getTypedExpression();
if (leftTyped.isPresent() && rightTyped.isPresent()) {
SpecialComparisonResult leftResult = handleSpecialComparisonCases(expressionTyper, binaryExpr.getOperator(), leftTyped.get(), rightTyped.get());
return leftResult.expression;
}
}
return null;
}
Aggregations