Skip to content

Commit

Permalink
Fix reparenting bug
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Jun 30, 2017
1 parent 821b74a commit c1b1426
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
23 changes: 14 additions & 9 deletions javaparser-core/src/main/java/com/github/javaparser/ast/Node.java
Expand Up @@ -357,22 +357,27 @@ public List<Comment> getAllContainedComments() {
* Assign a new parent to this node, removing it * Assign a new parent to this node, removing it
* from the list of children of the previous parent, if any. * from the list of children of the previous parent, if any.
* *
* @param parentNode node to be set as parent * @param newParentNode node to be set as parent
*/ */
@Override @Override
public Node setParentNode(Node parentNode) { public Node setParentNode(Node newParentNode) {
if (parentNode == this.parentNode) { if (newParentNode == parentNode) {
return this; return this;
} }
observers.forEach(o -> o.parentChange(this, this.parentNode, parentNode)); observers.forEach(o -> o.parentChange(this, parentNode, newParentNode));
// remove from old parent, if any // remove from old parent, if any
if (this.parentNode != null) { if (parentNode != null) {
this.parentNode.childNodes.remove(this); final List<Node> parentChildNodes = parentNode.childNodes;
for (int i = 0; i < parentChildNodes.size(); i++) {
if (parentChildNodes.get(i) == this) {
parentChildNodes.remove(i);
}
}
} }
this.parentNode = parentNode; parentNode = newParentNode;
// add to new parent, if any // add to new parent, if any
if (this.parentNode != null) { if (parentNode != null) {
this.parentNode.childNodes.add(this); parentNode.childNodes.add(this);
} }
return this; return this;
} }
Expand Down
Expand Up @@ -26,8 +26,8 @@
import com.github.javaparser.ast.body.FieldDeclaration; import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator; import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.BlockComment; import com.github.javaparser.ast.comments.BlockComment;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.JavadocComment; import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.comments.LineComment; import com.github.javaparser.ast.comments.LineComment;
import com.github.javaparser.ast.observer.AstObserver; import com.github.javaparser.ast.observer.AstObserver;
Expand All @@ -42,9 +42,7 @@
import java.util.List; import java.util.List;


import static com.github.javaparser.JavaParser.parse; import static com.github.javaparser.JavaParser.parse;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;


public class NodeTest { public class NodeTest {


Expand Down Expand Up @@ -313,4 +311,20 @@ public void removeAllOnRequiredProperty() {
// Name is required, so to remove it the whole method is removed. // Name is required, so to remove it the whole method is removed.
assertEquals("class X {\n}\n", cu.toString()); assertEquals("class X {\n}\n", cu.toString());
} }

@Test
public void removingTheSecondOfAListOfIdenticalStatementsDoesNotMessUpTheParents() {
CompilationUnit unit = JavaParser.parse("public class Example {\n" +
" public static void example() {\n" +
" boolean swapped;\n" +
" swapped=false;\n" +
" swapped=false;\n" +
" }\n" +
"}\n");
// remove the second swapped=false
Node target = unit.getChildNodes().get(0).getChildNodes().get(1).getChildNodes().get(2).getChildNodes().get(2);
target.remove();
// This will throw an exception if the parents are bad.
System.out.println(unit.toString());
}
} }

0 comments on commit c1b1426

Please sign in to comment.