From 99a38d4d08cb020182663ffaf9418d28becf1fd6 Mon Sep 17 00:00:00 2001
From: Reinier Zwitserloot <reinier@tipit.to>
Date: Wed, 24 Jun 2009 01:22:08 +0200
Subject: Added proper support for changing the AST as its being visited, both
 removal and addition. The rule is now: children traversal traverses through
 the tree mostly as it was when it started.

---
 src/lombok/core/AST.java             | 9 +++++++--
 src/lombok/eclipse/EclipseAST.java   | 2 +-
 src/lombok/eclipse/handlers/PKG.java | 2 ++
 src/lombok/javac/JavacAST.java       | 4 ++--
 4 files changed, 12 insertions(+), 5 deletions(-)

(limited to 'src/lombok')

diff --git a/src/lombok/core/AST.java b/src/lombok/core/AST.java
index c1186d24..8e060f07 100644
--- a/src/lombok/core/AST.java
+++ b/src/lombok/core/AST.java
@@ -134,7 +134,7 @@ public abstract class AST<N> {
 		}
 		
 		public Collection<? extends Node> down() {
-			return children;
+			return new ArrayList<Node>(children);
 		}
 		
 		public boolean isHandled() {
@@ -154,13 +154,18 @@ public abstract class AST<N> {
 			return fileName;
 		}
 		
-		public Node add(N newChild, Kind kind) {
+		@SuppressWarnings("unchecked") public Node add(N newChild, Kind kind) {
 			Node n = buildTree(newChild, kind);
 			if ( n == null ) return null;
 			n.parent = this;
+			((List)children).add(n);
 			return n;
 		}
 		
+		public void removeChild(Node child) {
+			children.remove(child);
+		}
+		
 		public Node recursiveSetHandled() {
 			this.handled = true;
 			for ( Node child : children ) child.recursiveSetHandled();
diff --git a/src/lombok/eclipse/EclipseAST.java b/src/lombok/eclipse/EclipseAST.java
index 07ca5f11..df0020bc 100644
--- a/src/lombok/eclipse/EclipseAST.java
+++ b/src/lombok/eclipse/EclipseAST.java
@@ -256,7 +256,7 @@ public class EclipseAST extends AST<ASTNode> {
 		/** {@inheritDoc} */
 		@SuppressWarnings("unchecked")
 		@Override public Collection<Node> down() {
-			return (Collection<Node>) children;
+			return (Collection<Node>) super.down();
 		}
 		
 		/** {@inheritDoc} */
diff --git a/src/lombok/eclipse/handlers/PKG.java b/src/lombok/eclipse/handlers/PKG.java
index 1e391ef2..3cfbf9c6 100644
--- a/src/lombok/eclipse/handlers/PKG.java
+++ b/src/lombok/eclipse/handlers/PKG.java
@@ -135,7 +135,9 @@ class PKG {
 				for ( int i = 0 ; i < parent.methods.length ; i++ ) {
 					if ( parent.methods[i] instanceof ConstructorDeclaration &&
 							(parent.methods[i].bits & ASTNode.IsDefaultConstructor) != 0 ) {
+						EclipseAST.Node tossMe = type.getNodeFor(parent.methods[i]);
 						parent.methods[i] = method;
+						if ( tossMe != null ) tossMe.up().removeChild(tossMe);
 						injectionComplete = true;
 						break;
 					}
diff --git a/src/lombok/javac/JavacAST.java b/src/lombok/javac/JavacAST.java
index f0544256..ad2be523 100644
--- a/src/lombok/javac/JavacAST.java
+++ b/src/lombok/javac/JavacAST.java
@@ -69,7 +69,7 @@ public class JavacAST extends AST<JCTree> {
 	}
 	
 	private void traverseChildren(JavacASTVisitor visitor, Node node) {
-		for ( Node child : node.down() ) {
+		for ( Node child : new ArrayList<Node>(node.down()) ) {
 			child.traverse(visitor);
 		}
 	}
@@ -357,7 +357,7 @@ public class JavacAST extends AST<JCTree> {
 		/** {@inheritDoc} */
 		@SuppressWarnings("unchecked")
 		@Override public Collection<Node> down() {
-			return (Collection<Node>) children;
+			return (Collection<Node>) super.down();
 		}
 		
 		public void addError(String message) {
-- 
cgit