Skip to content

Commit

Permalink
Merge remote-tracking branch 'javaparser/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Jul 2, 2017
2 parents b5b70ee + c8fc5be commit 9e004b9
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
* 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
public Node setParentNode(Node parentNode) {
if (parentNode == this.parentNode) {
public Node setParentNode(Node newParentNode) {
if (newParentNode == parentNode) {
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
if (this.parentNode != null) {
this.parentNode.childNodes.remove(this);
if (parentNode != null) {
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
if (this.parentNode != null) {
this.parentNode.childNodes.add(this);
if (parentNode != null) {
parentNode.childNodes.add(this);
}
return this;
}
Expand Down
Expand Up @@ -26,8 +26,8 @@
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
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.Comment;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.comments.LineComment;
import com.github.javaparser.ast.observer.AstObserver;
Expand All @@ -42,9 +42,7 @@
import java.util.List;

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

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.
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 9e004b9

Please sign in to comment.