Search in sources :

Example 6 with StringLiteralExpr

use of com.github.javaparser.ast.expr.StringLiteralExpr 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

StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)6 NameExpr (com.github.javaparser.ast.expr.NameExpr)3 Node (com.github.javaparser.ast.Node)2 NodeList (com.github.javaparser.ast.NodeList)2 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)2 Test (org.junit.Test)2 Modifier (com.github.javaparser.ast.Modifier)1 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)1 CharLiteralExpr (com.github.javaparser.ast.expr.CharLiteralExpr)1 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)1 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)1 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)1 LongLiteralExpr (com.github.javaparser.ast.expr.LongLiteralExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 Printable (com.github.javaparser.printer.Printable)1 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)1 ConstraintFormula (com.github.javaparser.symbolsolver.resolution.typeinference.ConstraintFormula)1