Search in sources :

Example 1 with PropertyMetaModel

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);
        }
    }
}
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 2 with PropertyMetaModel

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) + ":{", "}"));
}
Also used : Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) ArrayList(java.util.ArrayList) NodeMetaModel(com.github.javaparser.metamodel.NodeMetaModel) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 3 with PropertyMetaModel

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));
}
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 4 with PropertyMetaModel

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;");
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 5 with PropertyMetaModel

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;");
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Aggregations

PropertyMetaModel (com.github.javaparser.metamodel.PropertyMetaModel)17 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 SeparatedItemStringBuilder (com.github.javaparser.utils.SeparatedItemStringBuilder)5 Node (com.github.javaparser.ast.Node)4 NodeList (com.github.javaparser.ast.NodeList)4 NodeMetaModel (com.github.javaparser.metamodel.NodeMetaModel)4 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)3 ArrayList (java.util.ArrayList)2 TokenRange (com.github.javaparser.TokenRange)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)1 VisitorGenerator (com.github.javaparser.generator.VisitorGenerator)1 BaseNodeMetaModel (com.github.javaparser.metamodel.BaseNodeMetaModel)1 CodeGenerationUtils.f (com.github.javaparser.utils.CodeGenerationUtils.f)1 SourceRoot (com.github.javaparser.utils.SourceRoot)1 Arrays (java.util.Arrays)1 List (java.util.List)1