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