File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
src/main/java/de/danielbechler/diff/node Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -156,6 +156,34 @@ public Node getChild(final Element pathElement)
156156
157157 public void addChild (final Node node )
158158 {
159+ if (node .isRootNode ())
160+ {
161+ throw new IllegalArgumentException ("Detected attempt to add root node as child. " +
162+ "This is not allowed and must be a mistake." );
163+ }
164+ else if (node .getParentNode () != this )
165+ {
166+ throw new IllegalArgumentException ("Detected attempt to add child node that is already the " +
167+ "child of another node. Adding nodes multiple times is not allowed, since it could " +
168+ "cause infinite loops." );
169+ }
170+ else if (node == this )
171+ {
172+ throw new IllegalArgumentException ("Detected attempt to add a node to itself. " +
173+ "This would cause inifite loops and must never happen." );
174+ }
175+ else
176+ {
177+ final Collection <Node > children = node .getChildren ();
178+ for (final Node child : children )
179+ {
180+ if (child == this )
181+ {
182+ throw new IllegalArgumentException ("Detected attempt to add node to itself. " +
183+ "This would cause inifite loops and must never happen." );
184+ }
185+ }
186+ }
159187 node .setParentNode (this );
160188 children .put (node .getPathElement (), node );
161189 }
You can’t perform that action at this time.
0 commit comments