Search in sources :

Example 1 with CsmElement

use of com.github.javaparser.printer.concretesyntaxmodel.CsmElement in project javaparser by javaparser.

the class LexicalPreservingPrinter method interpret.

private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText) {
    LexicalDifferenceCalculator.CalculatedSyntaxModel calculatedSyntaxModel = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(csm, node);
    List<TokenTextElement> indentation = findIndentation(node);
    boolean pendingIndentation = false;
    for (CsmElement element : calculatedSyntaxModel.elements) {
        if (pendingIndentation && !(element instanceof CsmToken && ((CsmToken) element).isNewLine())) {
            indentation.forEach(nodeText::addElement);
        }
        pendingIndentation = false;
        if (element instanceof LexicalDifferenceCalculator.CsmChild) {
            nodeText.addChild(((LexicalDifferenceCalculator.CsmChild) element).getChild());
        } else if (element instanceof CsmToken) {
            CsmToken csmToken = (CsmToken) element;
            nodeText.addToken(csmToken.getTokenType(), csmToken.getContent(node));
            if (csmToken.isNewLine()) {
                pendingIndentation = true;
            }
        } else if (element instanceof CsmMix) {
            CsmMix csmMix = (CsmMix) element;
            csmMix.getElements().forEach(e -> interpret(node, e, nodeText));
        } else {
            throw new UnsupportedOperationException(element.getClass().getSimpleName());
        }
    }
    // so they have to be handled in a special way
    if (node instanceof VariableDeclarator) {
        VariableDeclarator variableDeclarator = (VariableDeclarator) node;
        variableDeclarator.getParentNode().ifPresent(parent -> ((NodeWithVariables<?>) parent).getMaximumCommonType().ifPresent(mct -> {
            int extraArrayLevels = variableDeclarator.getType().getArrayLevel() - mct.getArrayLevel();
            for (int i = 0; i < extraArrayLevels; i++) {
                nodeText.addElement(new TokenTextElement(LBRACKET));
                nodeText.addElement(new TokenTextElement(RBRACKET));
            }
        }));
    }
    return nodeText;
}
Also used : java.util(java.util) AstObserver(com.github.javaparser.ast.observer.AstObserver) CsmMix(com.github.javaparser.printer.concretesyntaxmodel.CsmMix) TreeVisitor(com.github.javaparser.ast.visitor.TreeVisitor) Pair(com.github.javaparser.utils.Pair) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) DataKey(com.github.javaparser.ast.DataKey) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) Utils.assertNotNull(com.github.javaparser.utils.Utils.assertNotNull) Utils.decapitalize(com.github.javaparser.utils.Utils.decapitalize) PropagatingAstObserver(com.github.javaparser.ast.observer.PropagatingAstObserver) Method(java.lang.reflect.Method) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) TokenTypes.eolTokenKind(com.github.javaparser.TokenTypes.eolTokenKind) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ObservableProperty(com.github.javaparser.ast.observer.ObservableProperty) InvocationTargetException(java.lang.reflect.InvocationTargetException) GeneratedJavaParserConstants(com.github.javaparser.GeneratedJavaParserConstants) com.github.javaparser(com.github.javaparser) ParameterizedType(java.lang.reflect.ParameterizedType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) NodeWithVariables(com.github.javaparser.ast.nodeTypes.NodeWithVariables) ConcreteSyntaxModel(com.github.javaparser.printer.ConcreteSyntaxModel) Utils(com.github.javaparser.utils.Utils) Writer(java.io.Writer) Comment(com.github.javaparser.ast.comments.Comment) Comparator(java.util.Comparator) CsmMix(com.github.javaparser.printer.concretesyntaxmodel.CsmMix) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) NodeWithVariables(com.github.javaparser.ast.nodeTypes.NodeWithVariables)

Example 2 with CsmElement

use of com.github.javaparser.printer.concretesyntaxmodel.CsmElement in project javaparser by javaparser.

the class DifferenceTest method annotationDeclarationExampleWithNameChanged.

@Test
public void annotationDeclarationExampleWithNameChanged() throws IOException {
    considerExample("AnnotationDeclaration_Example1_original");
    AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
    CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(element, annotationDeclaration);
    SimpleName newName = new SimpleName("NewName");
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.NAME, annotationDeclaration.getName(), newName);
    Difference diff = Difference.calculate(csmOriginal, csmChanged);
    diff.removeIndentationElements();
    int i = 0;
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.AT)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.INTERFACE)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(removed(new CsmChild(annotationDeclaration.getName())), diff.getElements().get(i++));
    assertEquals(added(new CsmChild(newName)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(0))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(1))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(2))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(3))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(4))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(5))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
    assertEquals(i, diff.getElements().size());
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) Test(org.junit.Test)

Example 3 with CsmElement

use of com.github.javaparser.printer.concretesyntaxmodel.CsmElement in project javaparser by javaparser.

the class DifferenceTest method annotationDeclarationExampleWithJavadocRemoved.

@Test
public void annotationDeclarationExampleWithJavadocRemoved() throws IOException {
    considerExample("AnnotationDeclaration_Example9_original");
    AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
    CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(element, annotationDeclaration);
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.COMMENT, annotationDeclaration.getComment().get(), null);
    Difference diff = Difference.calculate(csmOriginal, csmChanged);
    diff.removeIndentationElements();
    int i = 0;
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.PUBLIC)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.AT)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.INTERFACE)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getName())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(0))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(1))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(2))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(3))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(4))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(5))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
    assertEquals(i, diff.getElements().size());
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) Test(org.junit.Test)

Example 4 with CsmElement

use of com.github.javaparser.printer.concretesyntaxmodel.CsmElement in project javaparser by javaparser.

the class DifferenceTest method annotationDeclarationExampleWithModifierAdded.

@Test
public void annotationDeclarationExampleWithModifierAdded() throws IOException {
    considerExample("AnnotationDeclaration_Example1_original");
    AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
    CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(element, annotationDeclaration);
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.MODIFIERS, EnumSet.noneOf(Modifier.class), EnumSet.of(Modifier.PUBLIC));
    Difference diff = Difference.calculate(csmOriginal, csmChanged);
    diff.removeIndentationElements();
    int i = 0;
    assertEquals(added(new CsmToken(GeneratedJavaParserConstants.PUBLIC)), diff.getElements().get(i++));
    assertEquals(added(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.AT)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.INTERFACE)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getName())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(0))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(1))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(2))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(3))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(4))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmChild(annotationDeclaration.getMember(5))), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
    assertEquals(i, diff.getElements().size());
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) Modifier(com.github.javaparser.ast.Modifier) Test(org.junit.Test)

Example 5 with CsmElement

use of com.github.javaparser.printer.concretesyntaxmodel.CsmElement in project javaparser by javaparser.

the class LexicalDifferenceCalculatorTest method annotationDeclarationModifiersExampleModified.

@Test
public void annotationDeclarationModifiersExampleModified() throws IOException {
    considerExample("AnnotationDeclaration_Example1_original");
    AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
    CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
    LexicalDifferenceCalculator.CalculatedSyntaxModel csm = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.MODIFIERS, EnumSet.noneOf(Modifier.class), EnumSet.of(Modifier.PUBLIC));
    csm.removeIndentationElements();
    int i = 0;
    assertEquals(new CsmToken(GeneratedJavaParserConstants.PUBLIC), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.AT), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.INTERFACE), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getName()), csm.elements.get(i++));
    assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.LBRACE), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(0)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(1)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(2)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(3)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(4)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(5)), csm.elements.get(i++));
    assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
    assertEquals(new CsmToken(GeneratedJavaParserConstants.RBRACE), csm.elements.get(i++));
    assertEquals(i, csm.elements.size());
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) Modifier(com.github.javaparser.ast.Modifier) Test(org.junit.Test)

Aggregations

CsmElement (com.github.javaparser.printer.concretesyntaxmodel.CsmElement)14 CsmToken (com.github.javaparser.printer.concretesyntaxmodel.CsmToken)14 Test (org.junit.Test)13 CsmChild (com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild)6 AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)5 Modifier (com.github.javaparser.ast.Modifier)3 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)3 PackageDeclaration (com.github.javaparser.ast.PackageDeclaration)2 com.github.javaparser (com.github.javaparser)1 GeneratedJavaParserConstants (com.github.javaparser.GeneratedJavaParserConstants)1 TokenTypes.eolTokenKind (com.github.javaparser.TokenTypes.eolTokenKind)1 DataKey (com.github.javaparser.ast.DataKey)1 Node (com.github.javaparser.ast.Node)1 NodeList (com.github.javaparser.ast.NodeList)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 Comment (com.github.javaparser.ast.comments.Comment)1 NodeWithVariables (com.github.javaparser.ast.nodeTypes.NodeWithVariables)1 AstObserver (com.github.javaparser.ast.observer.AstObserver)1 ObservableProperty (com.github.javaparser.ast.observer.ObservableProperty)1 PropagatingAstObserver (com.github.javaparser.ast.observer.PropagatingAstObserver)1