use of com.github.javaparser.metamodel.PropertyMetaModel in project javaparser by javaparser.
the class DotPrinter method output.
public void output(Node node, String parentNodeName, String name, 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());
String ndName = nextNodeName();
if (outputNodeType)
builder.append(System.lineSeparator() + ndName + " [label=\"" + name + " (" + metaModel.getTypeName() + ")\"];");
else
builder.append(System.lineSeparator() + ndName + " [label=\"" + name + "\"];");
if (parentNodeName != null)
builder.append(System.lineSeparator() + parentNodeName + " -> " + ndName + ";");
for (PropertyMetaModel a : attributes) {
String attrName = nextNodeName();
builder.append(System.lineSeparator() + attrName + " [label=\"" + a.getName() + "='" + a.getValue(node).toString() + "'\"];");
builder.append(System.lineSeparator() + ndName + " -> " + attrName + ";");
}
for (PropertyMetaModel sn : subNodes) {
Node nd = (Node) sn.getValue(node);
if (nd != null)
output(nd, ndName, sn.getName(), builder);
}
for (PropertyMetaModel sl : subLists) {
NodeList<? extends Node> nl = (NodeList<? extends Node>) sl.getValue(node);
if (nl != null && nl.isNonEmpty()) {
String ndLstName = nextNodeName();
builder.append(System.lineSeparator() + ndLstName + " [label=\"" + sl.getName() + "\"];");
builder.append(System.lineSeparator() + ndName + " -> " + ndLstName + ";");
String slName = sl.getName().substring(0, sl.getName().length() - 1);
for (Node nd : nl) output(nd, ndLstName, slName, builder);
}
}
}
use of com.github.javaparser.metamodel.PropertyMetaModel in project javaparser by javaparser.
the class JsonPrinter method output.
public String output(Node node, String name, int level) {
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());
final List<String> content = new ArrayList<>();
if (outputNodeType) {
content.add(q("type") + ":" + q(metaModel.getTypeName()));
}
for (PropertyMetaModel attributeMetaModel : attributes) {
content.add(q(attributeMetaModel.getName()) + ":" + q(attributeMetaModel.getValue(node).toString()));
}
for (PropertyMetaModel subNodeMetaModel : subNodes) {
Node value = (Node) subNodeMetaModel.getValue(node);
if (value != null) {
content.add(output(value, subNodeMetaModel.getName(), level + 1));
}
}
for (PropertyMetaModel subListMetaModel : subLists) {
NodeList<? extends Node> subList = (NodeList<? extends Node>) subListMetaModel.getValue(node);
if (subList != null && !subList.isEmpty()) {
final List<String> listContent = new ArrayList<>();
for (Node subListNode : subList) {
listContent.add(output(subListNode, null, level + 1));
}
content.add(listContent.stream().collect(Collectors.joining(",", q(subListMetaModel.getName()) + ":[", "]")));
}
}
if (name == null) {
return content.stream().collect(Collectors.joining(",", "{", "}"));
}
return content.stream().collect(Collectors.joining(",", q(name) + ":{", "}"));
}
use of com.github.javaparser.metamodel.PropertyMetaModel 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.metamodel.PropertyMetaModel in project javaparser by javaparser.
the class EqualsVisitorGenerator method generateVisitMethodBody.
@Override
protected void generateVisitMethodBody(BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
visitMethod.getParameters().forEach(p -> p.setFinal(true));
BlockStmt body = visitMethod.getBody().get();
body.getStatements().clear();
body.addStatement(f("final %s n2 = (%s) arg;", node.getTypeName(), node.getTypeName()));
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
final String getter = field.getGetterMethodName() + "()";
if (field.getNodeReference().isPresent()) {
if (field.isNodeList()) {
body.addStatement(f("if (!nodesEquals(n.%s, n2.%s)) return false;", getter, getter));
} else {
body.addStatement(f("if (!nodeEquals(n.%s, n2.%s)) return false;", getter, getter));
}
} else {
body.addStatement(f("if (!objEquals(n.%s, n2.%s)) return false;", getter, getter));
}
}
if (body.getStatements().size() == 1) {
// Only the cast line was added, but nothing is using it, so remove it again.
body.getStatements().clear();
}
body.addStatement("return true;");
}
use of com.github.javaparser.metamodel.PropertyMetaModel in project javaparser by javaparser.
the class GenericVisitorAdapterGenerator method generateVisitMethodBody.
@Override
protected void generateVisitMethodBody(BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
visitMethod.getParameters().forEach(p -> p.setFinal(true));
BlockStmt body = visitMethod.getBody().get();
body.getStatements().clear();
body.addStatement("R result;");
final String resultCheck = "if (result != null) return result;";
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
final String getter = field.getGetterMethodName() + "()";
if (field.getNodeReference().isPresent()) {
if (field.isOptional()) {
body.addStatement(f("if (n.%s.isPresent()) {" + " result = n.%s.get().accept(this, arg);" + " %s" + "}", getter, getter, resultCheck));
} else {
body.addStatement(f("{ result = n.%s.accept(this, arg); %s }", getter, resultCheck));
}
}
}
body.addStatement("return null;");
}
Aggregations