Search in sources :

Example 16 with Node

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

the class XmlPrinter method output.

public void output(Node node, String name, int level, StringBuilder builder) {
    assertNotNull(node);
    NodeMetaModel metaModel = node.getMetaModel();
    List<PropertyMetaModel> allPropertyMetaModels = metaModel.getAllPropertyMetaModels();
    List<PropertyMetaModel> attributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute).filter(PropertyMetaModel::isSingular).collect(toList());
    List<PropertyMetaModel> subNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode).filter(PropertyMetaModel::isSingular).collect(toList());
    List<PropertyMetaModel> subLists = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNodeList).collect(toList());
    builder.append("<").append(name);
    if (outputNodeType) {
        builder.append(attribute("type", metaModel.getTypeName()));
    }
    for (PropertyMetaModel attributeMetaModel : attributes) {
        builder.append(attribute(attributeMetaModel.getName(), attributeMetaModel.getValue(node).toString()));
    }
    builder.append(">");
    for (PropertyMetaModel subNodeMetaModel : subNodes) {
        Node value = (Node) subNodeMetaModel.getValue(node);
        if (value != null) {
            output(value, subNodeMetaModel.getName(), level + 1, builder);
        }
    }
    for (PropertyMetaModel subListMetaModel : subLists) {
        NodeList<? extends Node> subList = (NodeList<? extends Node>) subListMetaModel.getValue(node);
        if (subList != null && !subList.isEmpty()) {
            String listName = subListMetaModel.getName();
            builder.append("<").append(listName).append(">");
            String singular = listName.substring(0, listName.length() - 1);
            for (Node subListNode : subList) {
                output(subListNode, singular, level + 1, builder);
            }
            builder.append(close(listName));
        }
    }
    builder.append(close(name));
}
Also used : Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) NodeMetaModel(com.github.javaparser.metamodel.NodeMetaModel) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 17 with Node

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

the class DumpVisitor method printOrphanCommentsEnding.

private void printOrphanCommentsEnding(final Node node) {
    List<Node> everything = new LinkedList<Node>();
    everything.addAll(node.getChildrenNodes());
    sortByBeginPosition(everything);
    if (everything.size() == 0)
        return;
    int commentsAtEnd = 0;
    boolean findingComments = true;
    while (findingComments && commentsAtEnd < everything.size()) {
        Node last = everything.get(everything.size() - 1 - commentsAtEnd);
        findingComments = (last instanceof Comment);
        if (findingComments)
            commentsAtEnd++;
    }
    for (int i = 0; i < commentsAtEnd; i++) {
        everything.get(everything.size() - commentsAtEnd + i).accept(this, null);
    }
}
Also used : Comment(com.github.javaparser.ast.comments.Comment) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) BlockComment(com.github.javaparser.ast.comments.BlockComment) LineComment(com.github.javaparser.ast.comments.LineComment) Node(com.github.javaparser.ast.Node) LinkedList(java.util.LinkedList)

Example 18 with Node

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

the class PrettyPrintVisitorTest 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 19 with Node

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

the class PrettyPrintVisitorTest method printAClassWithField.

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

Example 20 with Node

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

the class DifferenceTest method calculateDifferenceAIsEmpty.

@Test
public void calculateDifferenceAIsEmpty() {
    Node n1 = new FieldDeclaration();
    Node n2 = new MethodDeclaration();
    LexicalDifferenceCalculator.CalculatedSyntaxModel a = new LexicalDifferenceCalculator.CalculatedSyntaxModel(Collections.emptyList());
    LexicalDifferenceCalculator.CalculatedSyntaxModel b = new LexicalDifferenceCalculator.CalculatedSyntaxModel(Arrays.asList(new CsmToken(GeneratedJavaParserConstants.LPAREN), new CsmChild(n1), new CsmToken(GeneratedJavaParserConstants.RPAREN), new CsmChild(n2)));
    Difference diff = Difference.calculate(a, b);
    assertEquals(4, diff.getElements().size());
    assertEquals(added(new CsmToken(GeneratedJavaParserConstants.LPAREN)), diff.getElements().get(0));
    assertEquals(added(new CsmChild(n1)), diff.getElements().get(1));
    assertEquals(added(new CsmToken(GeneratedJavaParserConstants.RPAREN)), diff.getElements().get(2));
    assertEquals(added(new CsmChild(n2)), diff.getElements().get(3));
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) Node(com.github.javaparser.ast.Node) Test(org.junit.Test)

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