Search in sources :

Example 26 with Node

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

the class ConcreteSyntaxModelTest method printSimplestClass.

@Test
public void printSimplestClass() {
    Node node = JavaParser.parse("class A {}");
    assertEquals("class A {" + EOL + "}" + EOL, print(node));
}
Also used : Node(com.github.javaparser.ast.Node) Test(org.junit.Test)

Example 27 with Node

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

the class NodeWithModifiersTest method addModifierTriggerNotification.

@Test
public void addModifierTriggerNotification() {
    List<String> changes = new LinkedList<>();
    ClassOrInterfaceDeclaration decl = new ClassOrInterfaceDeclaration(EnumSet.noneOf(Modifier.class), false, "Foo");
    decl.register(new AstObserverAdapter() {

        @Override
        public void propertyChange(Node observedNode, ObservableProperty property, Object oldValue, Object newValue) {
            changes.add("property " + property.name() + " is changed to " + newValue);
        }
    });
    decl.addModifier(Modifier.PUBLIC);
    assertEquals(1, changes.size());
    assertEquals("property MODIFIERS is changed to [PUBLIC]", changes.get(0));
}
Also used : ObservableProperty(com.github.javaparser.ast.observer.ObservableProperty) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) AstObserverAdapter(com.github.javaparser.ast.observer.AstObserverAdapter) Modifier(com.github.javaparser.ast.Modifier) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 28 with Node

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

the class Node method getAllContainedComments.

/**
 * This is the list of Comment which are contained in the Node either because
 * they are properly associated to one of its children or because they are floating
 * around inside the Node
 *
 * @return all Comments within the node as a list
 */
public List<Comment> getAllContainedComments() {
    List<Comment> comments = new LinkedList<>();
    comments.addAll(getOrphanComments());
    for (Node child : getChildNodes()) {
        child.getComment().ifPresent(comments::add);
        comments.addAll(child.getAllContainedComments());
    }
    return comments;
}
Also used : BlockComment(com.github.javaparser.ast.comments.BlockComment) LineComment(com.github.javaparser.ast.comments.LineComment) Comment(com.github.javaparser.ast.comments.Comment) Node(com.github.javaparser.ast.Node) HasParentNode(com.github.javaparser.HasParentNode)

Example 29 with Node

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

the class VarValidator method accept.

@Override
public void accept(VarType node, ProblemReporter reporter) {
    // All allowed locations are within a VariableDeclaration inside a VariableDeclarationExpr inside something else.
    Optional<VariableDeclarator> variableDeclarator = node.findParent(VariableDeclarator.class);
    if (!variableDeclarator.isPresent()) {
        // Java 11's var in lambda's
        if (varAllowedInLambdaParameters) {
            boolean valid = node.findParent(Parameter.class).flatMap(Node::getParentNode).map((Node p) -> p instanceof LambdaExpr).orElse(false);
            if (valid) {
                return;
            }
        }
        reportIllegalPosition(node, reporter);
        return;
    }
    variableDeclarator.ifPresent(vd -> {
        Optional<Node> variableDeclarationExpr = vd.getParentNode();
        if (!variableDeclarationExpr.isPresent()) {
            reportIllegalPosition(node, reporter);
            return;
        }
        variableDeclarationExpr.ifPresent(vdeNode -> {
            if (!(vdeNode instanceof VariableDeclarationExpr)) {
                reportIllegalPosition(node, reporter);
                return;
            }
            VariableDeclarationExpr vde = (VariableDeclarationExpr) vdeNode;
            if (vde.getVariables().size() > 1) {
                reporter.report(vde, "\"var\" only takes a single variable.");
            }
            Optional<Node> container = vdeNode.getParentNode();
            if (!container.isPresent()) {
                reportIllegalPosition(node, reporter);
                return;
            }
            container.ifPresent(c -> {
                boolean positionIsFine = c instanceof ForStmt || c instanceof ForeachStmt || c instanceof ExpressionStmt;
                if (!positionIsFine) {
                    reportIllegalPosition(node, reporter);
                }
                // A local variable declaration ends up inside an ExpressionStmt.
                if (c instanceof ExpressionStmt) {
                    if (!vd.getInitializer().isPresent()) {
                        reporter.report(node, "\"var\" needs an initializer.");
                    }
                    vd.getInitializer().ifPresent(initializer -> {
                        if (initializer instanceof NullLiteralExpr) {
                            reporter.report(node, "\"var\" cannot infer type from just null.");
                        }
                        if (initializer instanceof ArrayCreationExpr) {
                            reporter.report(node, "\"var\" cannot infer array types.");
                        }
                    });
                }
            });
        });
    });
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Node(com.github.javaparser.ast.Node) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ForeachStmt(com.github.javaparser.ast.stmt.ForeachStmt) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) ForStmt(com.github.javaparser.ast.stmt.ForStmt) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr)

Example 30 with Node

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

the class TreeVisitor method visitBreadthFirst.

/**
 * https://en.wikipedia.org/wiki/Breadth-first_search
 *
 * @param node the start node, and the first one that is passed to process(node).
 */
public void visitBreadthFirst(Node node) {
    final Queue<Node> queue = new LinkedList<>();
    queue.offer(node);
    while (queue.size() > 0) {
        final Node head = queue.peek();
        for (Node child : head.getChildNodes()) {
            queue.offer(child);
        }
        process(queue.poll());
    }
}
Also used : Node(com.github.javaparser.ast.Node) LinkedList(java.util.LinkedList)

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