Search in sources :

Example 1 with NodeList

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());
}
Also used : Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with NodeList

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);
}
Also used : NodeList(com.github.javaparser.ast.NodeList)

Example 3 with NodeList

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

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) + ":{", "}"));
}
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 5 with NodeList

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

Aggregations

NodeList (com.github.javaparser.ast.NodeList)83 Expression (com.github.javaparser.ast.expr.Expression)48 NameExpr (com.github.javaparser.ast.expr.NameExpr)40 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)37 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)36 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)29 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 List (java.util.List)22 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)18 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)18 CompilationUnit (com.github.javaparser.ast.CompilationUnit)17 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)17 ArrayList (java.util.ArrayList)17 Collectors (java.util.stream.Collectors)17 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)16 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)16 Parameter (com.github.javaparser.ast.body.Parameter)15 Test (org.junit.Test)15 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14