Search in sources :

Example 46 with Node

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

the class DrlxParseUtil method rescopeNamesToNewScope.

/**
 * Mutates expression
 * such that, if it contains a NameExpr for any of the <code>names</code>,
 * it is replaced with a FieldAccessExpr having <code>newScope</code> as the scope.
 */
public static void rescopeNamesToNewScope(Expression newScope, List<String> names, Node e) {
    if (e instanceof NodeWithArguments) {
        NodeWithArguments<?> arguments = (NodeWithArguments) e;
        for (Expression argument : arguments.getArguments()) {
            rescopeNamesToNewScope(newScope, names, argument);
        }
    }
    if (e instanceof AssignExpr) {
        AssignExpr assignExpr = (AssignExpr) e;
        rescopeNamesToNewScope(newScope, names, assignExpr.getTarget());
        rescopeNamesToNewScope(newScope, names, assignExpr.getValue());
    } else if (e instanceof BinaryExpr) {
        rescopeNamesToNewScope(newScope, names, ((BinaryExpr) e).getLeft());
        rescopeNamesToNewScope(newScope, names, ((BinaryExpr) e).getRight());
    } else if (e instanceof UnaryExpr) {
        rescopeNamesToNewScope(newScope, names, ((UnaryExpr) e).getExpression());
    } else if (e instanceof EnclosedExpr) {
        rescopeNamesToNewScope(newScope, names, ((EnclosedExpr) e).getInner());
    } else if (e instanceof Expression) {
        Optional<Expression> rootNode = DrlxParseUtil.findRootNodeViaScope((Expression) e);
        if (rootNode.isPresent() && rootNode.get() instanceof NameExpr) {
            NameExpr nameExpr = (NameExpr) rootNode.get();
            if (names.contains(nameExpr.getNameAsString())) {
                Expression prepend = new FieldAccessExpr(newScope, nameExpr.getNameAsString());
                if (e instanceof NameExpr) {
                    Optional<Node> parentNode = e.getParentNode();
                    if (parentNode.isPresent()) {
                        // actually `e` was not composite, it was already the NameExpr node I was looking to replace.
                        parentNode.get().replace(nameExpr, prepend);
                    } else {
                        throw new IllegalStateException("Cannot find parent node for " + ((NameExpr) e).getNameAsString() + "!");
                    }
                } else {
                    e.replace(nameExpr, prepend);
                }
            }
        }
    } else {
        for (Node child : e.getChildNodes()) {
            rescopeNamesToNewScope(newScope, names, child);
        }
    }
}
Also used : BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) Node(com.github.javaparser.ast.Node) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) 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) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) NodeWithArguments(com.github.javaparser.ast.nodeTypes.NodeWithArguments)

Example 47 with Node

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

the class DroolsMvelParserTest method testBindingOnRight.

@Test
public void testBindingOnRight() {
    String expr = "$n : name == \"Mario\" && $a : age > 20";
    DrlxExpression drlxExpression = parseExpression(parser, expr);
    Expression bExpr = drlxExpression.getExpr();
    assertTrue(bExpr instanceof BinaryExpr);
    Node left = ((BinaryExpr) bExpr).getLeft();
    assertTrue(left instanceof DrlxExpression);
    DrlxExpression leftExpr = (DrlxExpression) left;
    SimpleName leftBind = leftExpr.getBind();
    assertEquals("$n", leftBind.asString());
    Expression expression = leftExpr.getExpr();
    BinaryExpr binaryExpr = ((BinaryExpr) expression);
    assertEquals("name", toString(binaryExpr.getLeft()));
    assertEquals("\"Mario\"", toString(binaryExpr.getRight()));
    assertEquals(Operator.EQUALS, binaryExpr.getOperator());
    Node right = ((BinaryExpr) bExpr).getRight();
    assertTrue(right instanceof DrlxExpression);
    DrlxExpression rightExpr = (DrlxExpression) right;
    SimpleName rightBind = rightExpr.getBind();
    assertEquals("$a", rightBind.asString());
    BinaryExpr binaryExpr2 = ((BinaryExpr) rightExpr.getExpr());
    assertEquals("age", toString(binaryExpr2.getLeft()));
    assertEquals("20", toString(binaryExpr2.getRight()));
    assertEquals(Operator.GREATER, binaryExpr2.getOperator());
}
Also used : DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) Node(com.github.javaparser.ast.Node) SimpleName(com.github.javaparser.ast.expr.SimpleName) Test(org.junit.Test)

Example 48 with Node

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

the class DroolsMvelParserTest method testComplexEnclosedBindVariable.

@Test
public void testComplexEnclosedBindVariable() {
    String expr = "($n : name == \"Mario\") && (age > 20)";
    DrlxExpression drlxExpression = parseExpression(parser, expr);
    Expression bExpr = drlxExpression.getExpr();
    assertTrue(bExpr instanceof BinaryExpr);
    Node left = ((BinaryExpr) bExpr).getLeft();
    assertTrue(left instanceof EnclosedExpr);
    Expression inner = ((EnclosedExpr) left).getInner();
    assertTrue(inner instanceof DrlxExpression);
    DrlxExpression innerDrlxExpression = (DrlxExpression) inner;
    SimpleName bind = innerDrlxExpression.getBind();
    assertEquals("$n", bind.asString());
    Expression expression = innerDrlxExpression.getExpr();
    BinaryExpr binaryExpr = ((BinaryExpr) expression);
    assertEquals("name", toString(binaryExpr.getLeft()));
    assertEquals("\"Mario\"", toString(binaryExpr.getRight()));
    assertEquals(Operator.EQUALS, binaryExpr.getOperator());
    Node right = ((BinaryExpr) bExpr).getRight();
    assertTrue(right instanceof EnclosedExpr);
    Expression expression2 = ((EnclosedExpr) right).getInner();
    BinaryExpr binaryExpr2 = ((BinaryExpr) expression2);
    assertEquals("age", toString(binaryExpr2.getLeft()));
    assertEquals("20", toString(binaryExpr2.getRight()));
    assertEquals(Operator.GREATER, binaryExpr2.getOperator());
}
Also used : DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) Node(com.github.javaparser.ast.Node) SimpleName(com.github.javaparser.ast.expr.SimpleName) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) Test(org.junit.Test)

Example 49 with Node

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

the class AnnotationEqualityVisitor method defaultAction.

@Override
public <T extends Node> void defaultAction(T node1, T node2) {
    if (!(node1 instanceof NodeWithAnnotations<?>) || !(node2 instanceof NodeWithAnnotations<?>)) {
        return;
    }
    // Comparing annotations with "equals" considers comments in the AST attached to the
    // annotations. These should be ignored because two valid ASTs for the same file may differ in
    // where comments appear, or whether they appear at all. So, to check if two nodes have the same
    // annotations we create copies with all comments removed and compare their lists of annotations
    // directly.
    Node node1Copy = node1.clone();
    Node node2Copy = node2.clone();
    for (Comment comment : node1Copy.getAllContainedComments()) {
        comment.remove();
    }
    for (Comment comment : node2Copy.getAllContainedComments()) {
        comment.remove();
    }
    List<AnnotationExpr> node1annos = ((NodeWithAnnotations<?>) node1Copy).getAnnotations();
    List<AnnotationExpr> node2annos = ((NodeWithAnnotations<?>) node2Copy).getAnnotations();
    if (!node1annos.equals(node2annos)) {
        annotationsMatch = false;
        mismatchedNode1 = (NodeWithAnnotations<?>) node1;
        mismatchedNode2 = (NodeWithAnnotations<?>) node2;
    }
}
Also used : NodeWithAnnotations(com.github.javaparser.ast.nodeTypes.NodeWithAnnotations) Comment(com.github.javaparser.ast.comments.Comment) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) Node(com.github.javaparser.ast.Node)

Example 50 with Node

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

the class RemoveAnnotationsForInference method isSuppressed.

// This approach searches upward to find all the active warning suppressions.
// An alternative, more efficient approach would be to track the current set of warning
// suppressions, using a stack.
// There are two problems with the alternative approach (and besides, this approach is fast
// enough as it is).
// 1. JavaParser sometimes visits members before the annotation, so there was not a chance to
// observe the annotation and place it on the suppression stack.  This should be fixed for
// ModifierVisitor (but not for other visitors such as GenericListVisitorAdapter) in
// JavaParser release 3.19.0.
// 2. A user might write an annotation before @SuppressWarnings, as in:
// @Interned @SuppressWarnings("interning")
// The {@code @Interned} annotation is visited before the {@code @SuppressWarnings}
// annotation is.  This could be addressed by searching just the parent's annotations.
/**
 * Returns true if warnings about the given annotation are suppressed.
 *
 * <p>Its heuristic is to look for a {@code @SuppressWarnings} annotation on a containing program
 * element, whose string is one of the elements of the annotation's fully-qualified name.
 *
 * @param arg an annotation
 * @return true if warnings about the given annotation are suppressed
 */
private static boolean isSuppressed(AnnotationExpr arg) {
    String name = arg.getNameAsString();
    // If it's a simple name for which we know a fully-qualified name,
    // try all fully-qualified names that it could expand to.
    Collection<String> names;
    if (simpleToFullyQualified.containsKey(name)) {
        names = simpleToFullyQualified.get(name);
    } else {
        names = Collections.singletonList(name);
    }
    Iterator<Node> itor = new Node.ParentsVisitor(arg);
    while (itor.hasNext()) {
        Node n = itor.next();
        if (n instanceof NodeWithAnnotations) {
            for (AnnotationExpr ae : ((NodeWithAnnotations<?>) n).getAnnotations()) {
                if (suppresses(ae, names)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : NodeWithAnnotations(com.github.javaparser.ast.nodeTypes.NodeWithAnnotations) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) Node(com.github.javaparser.ast.Node)

Aggregations

Node (com.github.javaparser.ast.Node)95 Test (org.junit.Test)24 Expression (com.github.javaparser.ast.expr.Expression)22 NodeList (com.github.javaparser.ast.NodeList)18 Comment (com.github.javaparser.ast.comments.Comment)13 CompilationUnit (com.github.javaparser.ast.CompilationUnit)12 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)12 NameExpr (com.github.javaparser.ast.expr.NameExpr)12 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 ArrayList (java.util.ArrayList)11 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)8 Collectors (java.util.stream.Collectors)8 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)7 LineComment (com.github.javaparser.ast.comments.LineComment)7 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)7 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)7 SimpleName (com.github.javaparser.ast.expr.SimpleName)7 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)7 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)6 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)6