use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class LexicalPreservingPrinter method findNodeListName.
private static ObservableProperty findNodeListName(NodeList nodeList) {
Node parent = nodeList.getParentNodeForChildren();
for (Method m : parent.getClass().getMethods()) {
if (m.getParameterCount() == 0 && m.getReturnType().getCanonicalName().equals(NodeList.class.getCanonicalName())) {
try {
Object raw = m.invoke(parent);
if (!(raw instanceof NodeList)) {
throw new IllegalStateException("Expected NodeList, found " + raw.getClass().getCanonicalName());
}
NodeList result = (NodeList) raw;
if (result == nodeList) {
String name = m.getName();
if (name.startsWith("get")) {
name = name.substring("get".length());
}
return ObservableProperty.fromCamelCaseName(decapitalize(name));
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
} else if (m.getParameterCount() == 0 && isReturningOptionalNodeList(m)) {
try {
Optional<NodeList<?>> raw = (Optional<NodeList<?>>) m.invoke(parent);
if (raw.isPresent() && raw.get() == nodeList) {
String name = m.getName();
if (name.startsWith("get")) {
name = name.substring("get".length());
}
return ObservableProperty.fromCamelCaseName(decapitalize(name));
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
throw new IllegalArgumentException("Cannot find list name of NodeList of size " + nodeList.size());
}
use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class LexicalDifferenceCalculator method calculatedSyntaxModelAfterListRemoval.
// Visible for testing
CalculatedSyntaxModel calculatedSyntaxModelAfterListRemoval(Node container, ObservableProperty observableProperty, int index) {
CsmElement csm = ConcreteSyntaxModel.forClass(container.getClass());
Object rawValue = observableProperty.getRawValue(container);
if (!(rawValue instanceof NodeList)) {
throw new IllegalStateException("Expected NodeList, found " + rawValue.getClass().getCanonicalName());
}
NodeList nodeList = (NodeList) rawValue;
return calculatedSyntaxModelAfterListRemoval(csm, observableProperty, nodeList, index);
}
use of com.github.javaparser.ast.NodeList 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.ast.NodeList 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.ast.NodeList 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));
}
Aggregations