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());
}
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));
}
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());
}
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);
}
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));
}
Aggregations