Search in sources :

Example 56 with Node

use of com.github.javaparser.ast.Node in project javaparser by javaparser.

the class LexicalPreservingPrinter method storeInitialText.

private static void storeInitialText(Node root) {
    Map<Node, List<JavaToken>> tokensByNode = new IdentityHashMap<>();
    // range of interest and ignore all other branches
    for (JavaToken token : root.getTokenRange().get()) {
        Range tokenRange = token.getRange().orElseThrow(() -> new RuntimeException("Token without range: " + token));
        Node owner = findNodeForToken(root, tokenRange);
        if (owner == null) {
            throw new RuntimeException("Token without node owning it: " + token);
        }
        if (!tokensByNode.containsKey(owner)) {
            tokensByNode.put(owner, new LinkedList<>());
        }
        tokensByNode.get(owner).add(token);
    }
    // Now that we know the tokens we use them to create the initial NodeText for each node
    new TreeVisitor() {

        @Override
        public void process(Node node) {
            if (!PhantomNodeLogic.isPhantomNode(node)) {
                LexicalPreservingPrinter.storeInitialTextForOneNode(node, tokensByNode.get(node));
            }
        }
    }.visitBreadthFirst(root);
}
Also used : TreeVisitor(com.github.javaparser.ast.visitor.TreeVisitor) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList)

Example 57 with Node

use of com.github.javaparser.ast.Node in project javaparser by javaparser.

the class LexicalDifferenceCalculator method calculatedSyntaxModelAfterListRemoval.

// Visible for testing
CalculatedSyntaxModel calculatedSyntaxModelAfterListRemoval(CsmElement csm, ObservableProperty observableProperty, NodeList nodeList, int index) {
    List<CsmElement> elements = new LinkedList<>();
    Node container = nodeList.getParentNodeForChildren();
    calculatedSyntaxModelForNode(csm, container, elements, new ListRemovalChange(observableProperty, index));
    return new CalculatedSyntaxModel(elements);
}
Also used : Node(com.github.javaparser.ast.Node)

Example 58 with Node

use of com.github.javaparser.ast.Node in project javaparser by javaparser.

the class LexicalDifferenceCalculator method calculatedSyntaxModelForNode.

private void calculatedSyntaxModelForNode(CsmElement csm, Node node, List<CsmElement> elements, Change change) {
    if (csm instanceof CsmSequence) {
        CsmSequence csmSequence = (CsmSequence) csm;
        csmSequence.getElements().forEach(e -> calculatedSyntaxModelForNode(e, node, elements, change));
    } else if (csm instanceof CsmComment) {
    // nothing to do
    } else if (csm instanceof CsmSingleReference) {
        CsmSingleReference csmSingleReference = (CsmSingleReference) csm;
        Node child;
        if (change instanceof PropertyChange && ((PropertyChange) change).getProperty() == csmSingleReference.getProperty()) {
            child = (Node) ((PropertyChange) change).getNewValue();
        } else {
            child = csmSingleReference.getProperty().getValueAsSingleReference(node);
        }
        if (child != null) {
            elements.add(new CsmChild(child));
        }
    } else if (csm instanceof CsmNone) {
    // nothing to do
    } else if (csm instanceof CsmToken) {
        elements.add(csm);
    } else if (csm instanceof CsmOrphanCommentsEnding) {
    // nothing to do
    } else if (csm instanceof CsmList) {
        CsmList csmList = (CsmList) csm;
        if (csmList.getProperty().isAboutNodes()) {
            Object rawValue = change.getValue(csmList.getProperty(), node);
            NodeList nodeList;
            if (rawValue instanceof Optional) {
                Optional optional = (Optional) rawValue;
                if (optional.isPresent()) {
                    if (!(optional.get() instanceof NodeList)) {
                        throw new IllegalStateException("Expected NodeList, found " + optional.get().getClass().getCanonicalName());
                    }
                    nodeList = (NodeList) optional.get();
                } else {
                    nodeList = new NodeList();
                }
            } else {
                if (!(rawValue instanceof NodeList)) {
                    throw new IllegalStateException("Expected NodeList, found " + rawValue.getClass().getCanonicalName());
                }
                nodeList = (NodeList) rawValue;
            }
            if (!nodeList.isEmpty()) {
                calculatedSyntaxModelForNode(csmList.getPreceeding(), node, elements, change);
                for (int i = 0; i < nodeList.size(); i++) {
                    if (i != 0) {
                        calculatedSyntaxModelForNode(csmList.getSeparatorPre(), node, elements, change);
                    }
                    elements.add(new CsmChild(nodeList.get(i)));
                    if (i != (nodeList.size() - 1)) {
                        calculatedSyntaxModelForNode(csmList.getSeparatorPost(), node, elements, change);
                    }
                }
                calculatedSyntaxModelForNode(csmList.getFollowing(), node, elements, change);
            }
        } else {
            Collection collection = (Collection) change.getValue(csmList.getProperty(), node);
            if (!collection.isEmpty()) {
                calculatedSyntaxModelForNode(csmList.getPreceeding(), node, elements, change);
                boolean first = true;
                for (Iterator it = collection.iterator(); it.hasNext(); ) {
                    if (!first) {
                        calculatedSyntaxModelForNode(csmList.getSeparatorPre(), node, elements, change);
                    }
                    Object value = it.next();
                    if (value instanceof Modifier) {
                        Modifier modifier = (Modifier) value;
                        elements.add(new CsmToken(toToken(modifier)));
                    } else {
                        throw new UnsupportedOperationException(it.next().getClass().getSimpleName());
                    }
                    if (it.hasNext()) {
                        calculatedSyntaxModelForNode(csmList.getSeparatorPost(), node, elements, change);
                    }
                    first = false;
                }
                calculatedSyntaxModelForNode(csmList.getFollowing(), node, elements, change);
            }
        }
    } else if (csm instanceof CsmConditional) {
        CsmConditional csmConditional = (CsmConditional) csm;
        boolean satisfied = change.evaluate(csmConditional, node);
        if (satisfied) {
            calculatedSyntaxModelForNode(csmConditional.getThenElement(), node, elements, change);
        } else {
            calculatedSyntaxModelForNode(csmConditional.getElseElement(), node, elements, change);
        }
    } else if (csm instanceof CsmIndent) {
        elements.add(csm);
    } else if (csm instanceof CsmUnindent) {
        elements.add(csm);
    } else if (csm instanceof CsmAttribute) {
        CsmAttribute csmAttribute = (CsmAttribute) csm;
        Object value = change.getValue(csmAttribute.getProperty(), node);
        String text = value.toString();
        if (value instanceof Printable) {
            text = ((Printable) value).asString();
        }
        elements.add(new CsmToken(csmAttribute.getTokenType(node, value.toString()), text));
    } else if ((csm instanceof CsmString) && (node instanceof StringLiteralExpr)) {
        elements.add(new CsmToken(GeneratedJavaParserConstants.STRING_LITERAL, "\"" + ((StringLiteralExpr) node).getValue() + "\""));
    } else if (csm instanceof CsmMix) {
        CsmMix csmMix = (CsmMix) csm;
        List<CsmElement> mixElements = new LinkedList<>();
        csmMix.getElements().forEach(e -> calculatedSyntaxModelForNode(e, node, mixElements, change));
        elements.add(new CsmMix(mixElements));
    } else {
        throw new UnsupportedOperationException(csm.getClass().getSimpleName() + " " + csm);
    }
}
Also used : Node(com.github.javaparser.ast.Node) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Modifier(com.github.javaparser.ast.Modifier) NodeList(com.github.javaparser.ast.NodeList) Printable(com.github.javaparser.printer.Printable)

Example 59 with Node

use of com.github.javaparser.ast.Node in project javaparser by javaparser.

the class LexicalDifferenceCalculator method calculatedSyntaxModelAfterListReplacement.

// Visible for testing
private CalculatedSyntaxModel calculatedSyntaxModelAfterListReplacement(CsmElement csm, ObservableProperty observableProperty, NodeList nodeList, int index, Node newValue) {
    List<CsmElement> elements = new LinkedList<>();
    Node container = nodeList.getParentNodeForChildren();
    calculatedSyntaxModelForNode(csm, container, elements, new ListReplacementChange(observableProperty, index, newValue));
    return new CalculatedSyntaxModel(elements);
}
Also used : Node(com.github.javaparser.ast.Node)

Example 60 with Node

use of com.github.javaparser.ast.Node in project javaparser by javaparser.

the class YamlPrinter 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());
    if (outputNodeType)
        builder.append(System.lineSeparator() + indent(level) + name + "(Type=" + metaModel.getTypeName() + "): ");
    else
        builder.append(System.lineSeparator() + indent(level) + name + ": ");
    level++;
    for (PropertyMetaModel a : attributes) {
        builder.append(System.lineSeparator() + indent(level) + a.getName() + ": \"" + a.getValue(node).toString() + "\"");
    }
    for (PropertyMetaModel sn : subNodes) {
        Node nd = (Node) sn.getValue(node);
        if (nd != null)
            output(nd, sn.getName(), level, builder);
    }
    for (PropertyMetaModel sl : subLists) {
        NodeList<? extends Node> nl = (NodeList<? extends Node>) sl.getValue(node);
        if (nl != null && nl.isNonEmpty()) {
            builder.append(System.lineSeparator() + indent(level) + sl.getName() + ": ");
            String slName = sl.getName();
            slName = slName.endsWith("s") ? slName.substring(0, sl.getName().length() - 1) : slName;
            for (Node nd : nl) output(nd, "- " + slName, level + 1, 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)

Aggregations

Node (com.github.javaparser.ast.Node)95 Test (org.junit.Test)24 Expression (com.github.javaparser.ast.expr.Expression)22 NodeList (com.github.javaparser.ast.NodeList)18 Comment (com.github.javaparser.ast.comments.Comment)13 CompilationUnit (com.github.javaparser.ast.CompilationUnit)12 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)12 NameExpr (com.github.javaparser.ast.expr.NameExpr)12 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 ArrayList (java.util.ArrayList)11 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)8 Collectors (java.util.stream.Collectors)8 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)7 LineComment (com.github.javaparser.ast.comments.LineComment)7 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)7 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)7 SimpleName (com.github.javaparser.ast.expr.SimpleName)7 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)7 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)6 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)6