Search in sources :

Example 1 with CsmToken

use of com.github.javaparser.printer.concretesyntaxmodel.CsmToken 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 CsmToken

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

the class DifferenceTest method addingStatementToEmptyMethodBody.

@Test
public void addingStatementToEmptyMethodBody() {
    String code = "class A { void foo(char p1, int p2) {} }";
    considerCode(code);
    Statement s = new ExpressionStmt(new BinaryExpr(new IntegerLiteralExpr("10"), new IntegerLiteralExpr("2"), BinaryExpr.Operator.PLUS));
    MethodDeclaration m = cu.getClassByName("A").get().getMethodsByName("foo").get(0);
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(m.getBody().get());
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterListAddition(m.getBody().get(), ObservableProperty.STATEMENTS, 0, s);
    Difference diff = Difference.calculate(csmOriginal, csmChanged);
    int i = 0;
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmIndent()), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmChild(s)), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmUnindent()), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
    assertEquals(i, diff.getElements().size());
}
Also used : CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) Statement(com.github.javaparser.ast.stmt.Statement) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) CsmIndent(com.github.javaparser.printer.concretesyntaxmodel.CsmIndent) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmUnindent(com.github.javaparser.printer.concretesyntaxmodel.CsmUnindent) Test(org.junit.Test)

Example 3 with CsmToken

use of com.github.javaparser.printer.concretesyntaxmodel.CsmToken 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 4 with CsmToken

use of com.github.javaparser.printer.concretesyntaxmodel.CsmToken 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 5 with CsmToken

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

Aggregations

CsmToken (com.github.javaparser.printer.concretesyntaxmodel.CsmToken)18 Test (org.junit.Test)17 CsmElement (com.github.javaparser.printer.concretesyntaxmodel.CsmElement)14 CsmChild (com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild)10 AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)5 Modifier (com.github.javaparser.ast.Modifier)3 Node (com.github.javaparser.ast.Node)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 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