Skip to content

Commit

Permalink
Do not remove annotations that are array elements
Browse files Browse the repository at this point in the history
  • Loading branch information
mernst committed Dec 12, 2022
1 parent 6ae34a2 commit bb38c3d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,9 @@ protected void testAnnotationInsertion() {
System.lineSeparator(),
"Sanity check of erasing then reinserting annotations produced a different AST.",
"File: " + root.getSourceFile(),
"Original node: " + visitor.getMismatchedNode1(),
"Node with annotations re-inserted: " + visitor.getMismatchedNode2(),
"Node class: " + visitor.getMismatchedNode1().getClass().getSimpleName(),
"Original node: " + oneLine(visitor.getMismatchedNode1()),
"Node with annotations re-inserted: " + oneLine(visitor.getMismatchedNode2()),
"Original annotations: " + visitor.getMismatchedNode1().getAnnotations(),
"Re-inserted annotations: " + visitor.getMismatchedNode2().getAnnotations(),
"Original AST:",
Expand All @@ -454,6 +455,16 @@ protected void testAnnotationInsertion() {
}
}

/**
* Replace newlines in the printed representation by spaces.
*
* @param arg an object to format
* @return the object's toString representation, on one line
*/
private String oneLine(Object arg) {
return arg.toString().replace(System.lineSeparator(), " ");
}

/**
* Type-check classTree and skips classes specified by the skipDef option. Subclasses should
* override {@link #processClassTree(ClassTree)} instead of this method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,15 @@ public abstract class DoubleJavaParserVisitor extends VoidVisitorAdapter<Node> {
* corresponding elements in order.
*
* @param list1 first list of nodes
* @param list2 second list of nodes
* @param list2 second list of nodes, which has the same size as the first list
*/
private void visitLists(List<? extends Node> list1, List<? extends Node> list2) {
assert list1.size() == list2.size();
if (list1.size() != list2.size()) {
throw new Error(
String.format(
"%s.visitLists(%s [size %d], %s [size %d])",
this.getClass().getCanonicalName(), list1, list1.size(), list2, list2.size()));
}
for (int i = 0; i < list1.size(); i++) {
list1.get(i).accept(this, list2.get(i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2247,7 +2247,15 @@ public abstract void processCompoundAssignment(
* @param javaParserNodes list of corresponding JavaParser nodes
*/
private void visitLists(List<? extends Tree> javacTrees, List<? extends Node> javaParserNodes) {
assert javacTrees.size() == javaParserNodes.size();
if (javacTrees.size() != javaParserNodes.size()) {
throw new BugInCF(
"%s.visitLists(%s [size %d], %s [size %d])",
this.getClass().getCanonicalName(),
javacTrees,
javacTrees.size(),
javaParserNodes,
javaParserNodes.size());
}
Iterator<? extends Node> nodeIter = javaParserNodes.iterator();
for (Tree tree : javacTrees) {
tree.accept(this, nodeIter.next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.ArrayInitializerExpr;
import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.StringLiteralExpr;
Expand Down Expand Up @@ -266,6 +267,11 @@ public void defaultAction(Node node) {
}
}
}

@Override
public void visit(ArrayInitializerExpr node, Void p) {
// Do not remove annotations that are array elements.
}
}

/**
Expand Down

0 comments on commit bb38c3d

Please sign in to comment.