Search in sources :

Example 1 with Printable

use of com.github.javaparser.printer.Printable 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)

Aggregations

Modifier (com.github.javaparser.ast.Modifier)1 Node (com.github.javaparser.ast.Node)1 NodeList (com.github.javaparser.ast.NodeList)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 Printable (com.github.javaparser.printer.Printable)1