Avoid reusing document provided for autocomplete proposal when rewriting ast modifications
diff --git a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/ConstantStringHover.java b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/ConstantStringHover.java
index 93d8463..799ad81 100644
--- a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/ConstantStringHover.java
+++ b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/ConstantStringHover.java
@@ -44,7 +44,7 @@
             return;
         }
 
-        CompilationUnit cu = ASTutilsUI.getCompilationUnit(typeRoot);
+        CompilationUnit cu = ASTutilsUI.getAstRoot(typeRoot);
 
         if (cu == null) {
             return;
diff --git a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/JavaResourceAuditor.java b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/JavaResourceAuditor.java
index cecf043..a1e1e70 100644
--- a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/JavaResourceAuditor.java
+++ b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/JavaResourceAuditor.java
@@ -4,7 +4,7 @@
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
- * 
+ *
  * Contributors:
  *     Martin Reiterer - initial API and implementation
  ******************************************************************************/
@@ -44,7 +44,7 @@
     public JavaResourceAuditor() {
         this.reset();
     }
-    
+
     @Override
     public String[] getFileEndings() {
         return new String[] { "java" };
@@ -58,7 +58,8 @@
                 resource.getProject().getName());
 
         // get a reference to the shared AST of the loaded CompilationUnit
-        CompilationUnit cu = ASTutilsUI.getCompilationUnit(resource);
+        CompilationUnit cu = ASTutilsUI.getAstRoot(ASTutilsUI
+                .getCompilationUnit(resource));
         if (cu == null) {
             System.out.println("Cannot audit resource: "
                     + resource.getFullPath());
diff --git a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/MessageCompletionProposalComputer.java b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/MessageCompletionProposalComputer.java
index 17de6ae..6c48d4f 100644
--- a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/MessageCompletionProposalComputer.java
+++ b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/MessageCompletionProposalComputer.java
@@ -4,7 +4,7 @@
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
- * 
+ *
  *  *  * Contributors:
  * 	   Martin Reiterer - initial API and implementation
  *     Alexej Strelzow - integrated refactoring mechanism
@@ -82,7 +82,8 @@
                 csav = new ResourceAuditVisitor(null, manager.getProject()
                         .getName());
 
-                cu = ASTutilsUI.getCompilationUnit(resource);
+                cu = ASTutilsUI.getAstRoot(ASTutilsUI
+                        .getCompilationUnit(resource));
 
                 cu.accept(csav);
             }
diff --git a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/quickfix/IgnoreStringFromInternationalization.java b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/quickfix/IgnoreStringFromInternationalization.java
index 67429aa..d40dcc2 100644
--- a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/quickfix/IgnoreStringFromInternationalization.java
+++ b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/quickfix/IgnoreStringFromInternationalization.java
@@ -4,7 +4,7 @@
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
- * 
+ *
  * Contributors:
  *     Martin Reiterer - initial API and implementation
  ******************************************************************************/
@@ -38,7 +38,8 @@
     public void run(IMarker marker) {
         IResource resource = marker.getResource();
 
-        CompilationUnit cu = ASTutilsUI.getCompilationUnit(resource);
+        CompilationUnit cu = ASTutilsUI.getAstRoot(ASTutilsUI
+                .getCompilationUnit(resource));
 
         ITextFileBufferManager bufferManager = FileBuffers
                 .getTextFileBufferManager();
diff --git a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/refactoring/RefactoringService.java b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/refactoring/RefactoringService.java
index d69bcaf..35f720f 100644
--- a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/refactoring/RefactoringService.java
+++ b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/refactoring/RefactoringService.java
@@ -4,7 +4,7 @@
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
- * 
+ *
  * Contributors:
  *     Alexej Strelzow - initial API and implementation
  ******************************************************************************/
@@ -27,7 +27,7 @@
 
 /**
  * Service class, which can be used to execute key refactorings.
- * 
+ *
  * @author Alexej Strelzow
  */
 public class RefactoringService implements IRefactoringService {
@@ -74,7 +74,8 @@
     public void openRefactorDialog(IFile file, int selectionOffset) {
 
         String projectName = file.getProject().getName();
-        CompilationUnit cu = ASTutilsUI.getCompilationUnit(file);
+        CompilationUnit cu = ASTutilsUI.getAstRoot(ASTutilsUI
+                .getCompilationUnit(file));
 
         StringLiteral literal = ASTutils.getStringLiteralAtPos(cu,
                 selectionOffset);
diff --git a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/util/ASTutilsUI.java b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/util/ASTutilsUI.java
index d799870..e2d6f1c 100644
--- a/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/util/ASTutilsUI.java
+++ b/org.eclipse.babel.tapiji.tools.java.ui/src/org/eclipse/babel/tapiji/tools/java/ui/util/ASTutilsUI.java
@@ -4,7 +4,7 @@
  * are made available under the terms of the Eclipse Public License v1.0

  * which accompanies this distribution, and is available at

  * http://www.eclipse.org/legal/epl-v10.html

- * 

+ *

  * Contributors:

  *     Martin Reiterer - initial API and implementation

  *     Alexej Strelzow - seperation of ui/non-ui (methods moved from ASTUtils)

@@ -37,15 +37,17 @@
 import org.eclipse.core.runtime.CoreException;

 import org.eclipse.core.runtime.IPath;

 import org.eclipse.jdt.core.ICompilationUnit;

-import org.eclipse.jdt.core.IJavaElement;

 import org.eclipse.jdt.core.ITypeRoot;

 import org.eclipse.jdt.core.JavaCore;

+import org.eclipse.jdt.core.JavaModelException;

 import org.eclipse.jdt.core.dom.AST;

+import org.eclipse.jdt.core.dom.ASTParser;

 import org.eclipse.jdt.core.dom.CompilationUnit;

 import org.eclipse.jdt.core.dom.ImportDeclaration;

 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;

 import org.eclipse.jdt.ui.SharedASTProvider;

 import org.eclipse.jface.text.BadLocationException;

+import org.eclipse.jface.text.Document;

 import org.eclipse.jface.text.IDocument;

 import org.eclipse.swt.widgets.Display;

 import org.eclipse.text.edits.MalformedTreeException;

@@ -53,20 +55,12 @@
 

 public class ASTutilsUI {

 

-    public static CompilationUnit getCompilationUnit(IResource resource) {

-        IJavaElement je = JavaCore.create(resource,

+    public static ICompilationUnit getCompilationUnit(IResource resource) {

+        return (ICompilationUnit) JavaCore.create(resource,

                 JavaCore.create(resource.getProject()));

-        // get the type of the currently loaded resource

-        ITypeRoot typeRoot = ((ICompilationUnit) je);

-

-        if (typeRoot == null) {

-            return null;

-        }

-

-        return getCompilationUnit(typeRoot);

     }

 

-    public static CompilationUnit getCompilationUnit(ITypeRoot typeRoot) {

+    public static CompilationUnit getAstRoot(ITypeRoot typeRoot) {

         // get a reference to the shared AST of the loaded CompilationUnit

         CompilationUnit cu = SharedASTProvider.getAST(typeRoot,

         // do not wait for AST creation

@@ -78,68 +72,84 @@
     public static String insertNewBundleRef(IDocument document,

             IResource resource, int startPos, int endPos,

             String resourceBundleId, String key) {

-    	boolean createRBReference = false;

+        boolean createRBReference = false;

         String reference = "";

 

-        CompilationUnit cu = getCompilationUnit(resource);

-        AST ast = cu.getAST();

-        ASTRewrite rewriter = ASTRewrite.create(ast);

-

-        String variableName = ASTutils.resolveRBReferenceVar(document,

-                resource, startPos, resourceBundleId, cu);

-        if (variableName == null) {

-        	variableName = ASTutils.getNonExistingRBRefName(resourceBundleId, cu);

-        	createRBReference = true;

-        }

-

         try {

-            reference = ASTutils.createResourceReference(resourceBundleId, key,

-                    null, resource, startPos, variableName, ast, rewriter, cu);

+            // creation of DOM/AST from a ICompilationUnit

+            ICompilationUnit cu = getCompilationUnit(resource);

+            CompilationUnit astRoot = getAstRoot(cu);

+            AST ast = astRoot.getAST();

 

-            if (reference != null) {

-	            if (startPos > 0 && document.get().charAt(startPos - 1) == '\"') {

-	                startPos--;

-	                endPos++;

-	            }

-	

-	            if ((startPos + endPos) < document.getLength()

-	                    && document.get().charAt(startPos + endPos) == '\"') {

-	                endPos++;

-	            }

-	

-	            if ((startPos + endPos) < document.getLength()

-	                    && document.get().charAt(startPos + endPos - 1) == ';') {

-	                endPos--;

-	            }

-	

-	            document.replace(startPos, endPos, reference);

+            ASTRewrite rewriter = ASTRewrite.create(ast);

+            final String source = cu.getSource();

+            document = new Document(source);

 

+            String variableName = ASTutils.resolveRBReferenceVar(document,

+                    resource, startPos, resourceBundleId, astRoot);

+            if (variableName == null) {

+                variableName = ASTutils.getNonExistingRBRefName(

+                        resourceBundleId, astRoot);

+                createRBReference = true;

             }

-            // create non-internationalisation-comment

-            //ASTutils.createReplaceNonInternationalisationComment(cu, document,

-            //        startPos);

-        } catch (BadLocationException e) {

-            e.printStackTrace();

+

+            try {

+                reference = ASTutils.createResourceReference(resourceBundleId,

+                        key, null, resource, startPos, variableName, ast,

+                        rewriter, astRoot);

+

+                if (reference != null) {

+                    if (startPos > 0

+                            && document.get().charAt(startPos - 1) == '\"') {

+                        startPos--;

+                        endPos++;

+                    }

+

+                    if ((startPos + endPos) < document.getLength()

+                            && document.get().charAt(startPos + endPos) == '\"') {

+                        endPos++;

+                    }

+

+                    if ((startPos + endPos) < document.getLength()

+                            && document.get().charAt(startPos + endPos - 1) == ';') {

+                        endPos--;

+                    }

+

+                    document.replace(startPos, endPos, reference);

+

+                }

+                // create non-internationalisation-comment

+                ASTutils.createReplaceNonInternationalisationComment(astRoot,

+                        document, startPos);

+            } catch (BadLocationException e) {

+                e.printStackTrace();

+            }

+

+            if (createRBReference) {

+                ASTutils.createResourceBundleReference(resource, startPos,

+                        document, resourceBundleId, null, true, variableName,

+                        astRoot, ast, rewriter);

+            }

+

+            // computation of the text edits

+            TextEdit edits = rewriter.rewriteAST(document, null);

+

+            // computation of the new source code

+            try {

+                edits.apply(document);

+                String newSource = document.get();

+

+                // update of the compilation unit

+                cu.getBuffer().setContents(newSource);

+            } catch (MalformedTreeException e) {

+                Logger.logError(e);

+            } catch (BadLocationException e) {

+                Logger.logError(e);

+            }

+        } catch (JavaModelException e) {

+            Logger.logError(e);

         }

 

-        if (createRBReference) {

-            ASTutils.createResourceBundleReference(resource, startPos,

-                    document, resourceBundleId, null, true, variableName, cu, ast, rewriter);

-        }

-

-        // computation of the text edits

-        TextEdit edits = rewriter.rewriteAST(document, null);

-

-        // computation of the new source code

-        try {

-			edits.apply(document);

-		} catch (MalformedTreeException e) {

-			Logger.logError(e);

-		} catch (BadLocationException e) {

-			Logger.logError(e);

-		}

-        

-        

         return reference;

     }

 

@@ -149,52 +159,72 @@
         boolean createRBReference = false;

         String reference = "";

 

-        CompilationUnit cu = getCompilationUnit(resource);

-        AST ast = cu.getAST();

-        ASTRewrite rewriter = ASTRewrite.create(ast);

-

-        String variableName = ASTutils.resolveRBReferenceVar(document,

-                resource, offset, resourceBundleId, cu);

-        if (variableName == null) {

-        	variableName = ASTutils.getNonExistingRBRefName(resourceBundleId, cu);

-        	createRBReference = true;

-        }

-

-        reference = ASTutils.createResourceReference(resourceBundleId, key,

-                locale, resource, offset, variableName, ast, rewriter, cu);

-

-        if (reference != null) {

-	        try {

-				document.replace(offset, length, reference);

-			} catch (BadLocationException e) {

-				Logger.logError(e);

-				return null;

-			}

-        }

-        // create non-internationalisation-comment

-        //ASTutils.createReplaceNonInternationalisationComment(cu, document,

-        //        offset);

-

-        // TODO retrieve cu in the same way as in createResourceReference

-        // the current version does not parse method bodies

-

-        if (createRBReference) {

-            ASTutils.createResourceBundleReference(resource, offset, document,

-                    resourceBundleId, locale, true, variableName, cu, ast, rewriter);

-        }

-        

-        // computation of the text edits

-        TextEdit edits = rewriter.rewriteAST(document, null);

-

-        // computation of the new source code

         try {

-			edits.apply(document);

-		} catch (MalformedTreeException e) {

-			Logger.logError(e);

-		} catch (BadLocationException e) {

-			Logger.logError(e);

-		}

-        

+

+            ICompilationUnit cu = getCompilationUnit(resource);

+

+            // creation of DOM/AST from a ICompilationUnit

+            CompilationUnit astRoot = getAstRoot(cu);

+            AST ast = astRoot.getAST();

+

+            ASTRewrite rewriter = ASTRewrite.create(ast);

+            final String source = cu.getSource();

+            document = new Document(source);

+

+            String variableName = ASTutils.resolveRBReferenceVar(document,

+                    resource, offset, resourceBundleId, astRoot);

+

+            if (variableName == null) {

+                variableName = ASTutils.getNonExistingRBRefName(

+                        resourceBundleId, astRoot);

+                createRBReference = true;

+            }

+

+            reference = ASTutils.createResourceReference(resourceBundleId, key,

+                    locale, resource, offset, variableName, ast, rewriter,

+                    astRoot);

+

+            if (reference != null) {

+                try {

+                    document.replace(offset, length, reference);

+                } catch (BadLocationException e) {

+                    Logger.logError(e);

+                    return null;

+                }

+            }

+            // create non-internationalisation-comment

+            ASTutils.createReplaceNonInternationalisationComment(astRoot,

+                    document, offset);

+

+            // TODO retrieve cu in the same way as in createResourceReference

+            // the current version does not parse method bodies

+

+            if (createRBReference) {

+                ASTutils.createResourceBundleReference(resource, offset,

+                        document, resourceBundleId, locale, true, variableName,

+                        astRoot, ast, rewriter);

+            }

+

+            // computation of the text edits

+            TextEdit edits = rewriter.rewriteAST(document, null);

+

+            // computation of the new source code

+            try {

+                edits.apply(document);

+                String newSource = document.get();

+

+                // update of the compilation unit

+                cu.getBuffer().setContents(newSource);

+            } catch (MalformedTreeException e) {

+                Logger.logError(e);

+            } catch (BadLocationException e) {

+                Logger.logError(e);

+            }

+

+        } catch (JavaModelException e) {

+            Logger.logError(e);

+        }

+

         return reference;

     }

 

@@ -202,7 +232,7 @@
      * Performs the refactoring of messages key. The key can be a {@link String}

      * or an Enumeration! If it is an enumeration, then the enumPath needs to be

      * provided!

-     * 

+     *

      * @param projectName

      *            The name of the project, where the resource bundle file is in

      * @param resourceBundleId

@@ -236,7 +266,7 @@
                  * java.util.ResourceBundle or ch.qos.cal10n.MessageConveyor

                  * will be changed. An exception is the enum file, which gets

                  * referenced by the Cal10n framework.

-                 * 

+                 *

                  * {@inheritDoc}

                  */

                 @Override

@@ -246,7 +276,8 @@
                         return true;

                     }

 

-                    final CompilationUnit cu = getCompilationUnit(resource);

+                    final ICompilationUnit icu = getCompilationUnit(resource);

+                    final CompilationUnit cu = getAstRoot(icu);

 

                     // step 1: import filter

                     for (Object obj : cu.imports()) {

@@ -279,7 +310,7 @@
         if (enumPath != null) { // Cal10n support, change the enum file

             IFile file = project.getFile(enumPath.substring(project.getName()

                     .length() + 1));

-            final CompilationUnit enumCu = getCompilationUnit(file);

+            final CompilationUnit enumCu = getAstRoot(getCompilationUnit(file));

 

             Cal10nEnumRefactoringVisitor enumVisitor = new Cal10nEnumRefactoringVisitor(

                     enumCu, oldKey, newKey, changeSet);