Search in sources :

Example 6 with CsmChild

use of com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild 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 7 with CsmChild

use of com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild in project javaparser by javaparser.

the class DifferenceTest method calculateDifferenceAIsEmpty.

@Test
public void calculateDifferenceAIsEmpty() {
    Node n1 = new FieldDeclaration();
    Node n2 = new MethodDeclaration();
    LexicalDifferenceCalculator.CalculatedSyntaxModel a = new LexicalDifferenceCalculator.CalculatedSyntaxModel(Collections.emptyList());
    LexicalDifferenceCalculator.CalculatedSyntaxModel b = new LexicalDifferenceCalculator.CalculatedSyntaxModel(Arrays.asList(new CsmToken(GeneratedJavaParserConstants.LPAREN), new CsmChild(n1), new CsmToken(GeneratedJavaParserConstants.RPAREN), new CsmChild(n2)));
    Difference diff = Difference.calculate(a, b);
    assertEquals(4, diff.getElements().size());
    assertEquals(added(new CsmToken(GeneratedJavaParserConstants.LPAREN)), diff.getElements().get(0));
    assertEquals(added(new CsmChild(n1)), diff.getElements().get(1));
    assertEquals(added(new CsmToken(GeneratedJavaParserConstants.RPAREN)), diff.getElements().get(2));
    assertEquals(added(new CsmChild(n2)), diff.getElements().get(3));
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) Node(com.github.javaparser.ast.Node) Test(org.junit.Test)

Example 8 with CsmChild

use of com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild in project javaparser by javaparser.

the class Difference method matching.

private static boolean matching(CsmElement a, CsmElement b) {
    if (a instanceof CsmChild) {
        if (b instanceof CsmChild) {
            CsmChild childA = (CsmChild) a;
            CsmChild childB = (CsmChild) b;
            return childA.getChild().equals(childB.getChild());
        } else if (b instanceof CsmToken) {
            return false;
        } else if (b instanceof CsmIndent) {
            return false;
        } else if (b instanceof CsmUnindent) {
            return false;
        } else {
            throw new UnsupportedOperationException(a.getClass().getSimpleName() + " " + b.getClass().getSimpleName());
        }
    } else if (a instanceof CsmToken) {
        if (b instanceof CsmToken) {
            CsmToken childA = (CsmToken) a;
            CsmToken childB = (CsmToken) b;
            return childA.getTokenType() == childB.getTokenType();
        } else if (b instanceof CsmChild) {
            return false;
        } else if (b instanceof CsmIndent) {
            return false;
        } else if (b instanceof CsmUnindent) {
            return false;
        } else {
            throw new UnsupportedOperationException(a.getClass().getSimpleName() + " " + b.getClass().getSimpleName());
        }
    } else if (a instanceof CsmIndent) {
        return b instanceof CsmIndent;
    } else if (a instanceof CsmUnindent) {
        return b instanceof CsmUnindent;
    }
    throw new UnsupportedOperationException(a.getClass().getSimpleName() + " " + b.getClass().getSimpleName());
}
Also used : CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild)

Example 9 with CsmChild

use of com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild in project javaparser by javaparser.

the class Difference method calculate.

/**
 * Calculate the Difference between two CalculatedSyntaxModel elements, determining which elements were kept,
 * which were added and which were removed.
 */
static Difference calculate(LexicalDifferenceCalculator.CalculatedSyntaxModel original, LexicalDifferenceCalculator.CalculatedSyntaxModel after) {
    // For performance reasons we use the positions of matching children
    // to guide the calculation of the difference
    // 
    // Suppose we have:
    // qwerty[A]uiop
    // qwer[A]uiop
    // 
    // with [A] being a child and lowercase letters being tokens
    // 
    // We would calculate the Difference between "qwerty" and "qwer" then we know the A is kept, and then we
    // would calculate the difference between "uiop" and "uiop"
    Map<Node, Integer> childrenInOriginal = findChildrenPositions(original);
    Map<Node, Integer> childrenInAfter = findChildrenPositions(after);
    List<Node> commonChildren = new LinkedList<>(childrenInOriginal.keySet());
    commonChildren.retainAll(childrenInAfter.keySet());
    commonChildren.sort(Comparator.comparingInt(childrenInOriginal::get));
    List<DifferenceElement> elements = new LinkedList<>();
    int originalIndex = 0;
    int afterIndex = 0;
    int commonChildrenIndex = 0;
    while (commonChildrenIndex < commonChildren.size()) {
        Node child = commonChildren.get(commonChildrenIndex++);
        int posOfNextChildInOriginal = childrenInOriginal.get(child);
        int posOfNextChildInAfter = childrenInAfter.get(child);
        if (originalIndex < posOfNextChildInOriginal || afterIndex < posOfNextChildInAfter) {
            elements.addAll(calculateImpl(original.sub(originalIndex, posOfNextChildInOriginal), after.sub(afterIndex, posOfNextChildInAfter)).elements);
        }
        elements.add(new Kept(new CsmChild(child)));
        originalIndex = posOfNextChildInOriginal + 1;
        afterIndex = posOfNextChildInAfter + 1;
    }
    if (originalIndex < original.elements.size() || afterIndex < after.elements.size()) {
        elements.addAll(calculateImpl(original.sub(originalIndex, original.elements.size()), after.sub(afterIndex, after.elements.size())).elements);
    }
    return new Difference(elements);
}
Also used : CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) Node(com.github.javaparser.ast.Node)

Example 10 with CsmChild

use of com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild in project javaparser by javaparser.

the class DifferenceTest method calculateDifferenceBIsEmpty.

@Test
public void calculateDifferenceBIsEmpty() {
    Node n1 = new FieldDeclaration();
    Node n2 = new MethodDeclaration();
    LexicalDifferenceCalculator.CalculatedSyntaxModel a = new LexicalDifferenceCalculator.CalculatedSyntaxModel(Arrays.asList(new CsmToken(GeneratedJavaParserConstants.LPAREN), new CsmChild(n1), new CsmToken(GeneratedJavaParserConstants.RPAREN), new CsmChild(n2)));
    LexicalDifferenceCalculator.CalculatedSyntaxModel b = new LexicalDifferenceCalculator.CalculatedSyntaxModel(Collections.emptyList());
    Difference diff = Difference.calculate(a, b);
    assertEquals(4, diff.getElements().size());
    assertEquals(removed(new CsmToken(GeneratedJavaParserConstants.LPAREN)), diff.getElements().get(0));
    assertEquals(removed(new CsmChild(n1)), diff.getElements().get(1));
    assertEquals(removed(new CsmToken(GeneratedJavaParserConstants.RPAREN)), diff.getElements().get(2));
    assertEquals(removed(new CsmChild(n2)), diff.getElements().get(3));
}
Also used : CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) Node(com.github.javaparser.ast.Node) Test(org.junit.Test)

Aggregations

CsmChild (com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild)14 CsmToken (com.github.javaparser.printer.concretesyntaxmodel.CsmToken)10 Test (org.junit.Test)10 CsmElement (com.github.javaparser.printer.concretesyntaxmodel.CsmElement)6 Node (com.github.javaparser.ast.Node)4 Modifier (com.github.javaparser.ast.Modifier)2 GeneratedJavaParserConstants (com.github.javaparser.GeneratedJavaParserConstants)1 TokenTypes (com.github.javaparser.TokenTypes)1 PackageDeclaration (com.github.javaparser.ast.PackageDeclaration)1 Comment (com.github.javaparser.ast.comments.Comment)1 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)1 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)1 Statement (com.github.javaparser.ast.stmt.Statement)1 ArrayType (com.github.javaparser.ast.type.ArrayType)1 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)1 com.github.javaparser.printer.concretesyntaxmodel (com.github.javaparser.printer.concretesyntaxmodel)1 CsmIndent (com.github.javaparser.printer.concretesyntaxmodel.CsmIndent)1 CsmUnindent (com.github.javaparser.printer.concretesyntaxmodel.CsmUnindent)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1