1 11 package org.eclipse.jdt.internal.corext.refactoring.structure; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.text.edits.MultiTextEdit; 18 import org.eclipse.text.edits.TextEdit; 19 import org.eclipse.text.edits.TextEditGroup; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 24 import org.eclipse.jface.text.Document; 25 import org.eclipse.jface.text.IDocument; 26 27 import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup; 28 import org.eclipse.ltk.core.refactoring.GroupCategorySet; 29 30 import org.eclipse.jdt.core.ICompilationUnit; 31 import org.eclipse.jdt.core.JavaModelException; 32 import org.eclipse.jdt.core.WorkingCopyOwner; 33 import org.eclipse.jdt.core.dom.AST; 34 import org.eclipse.jdt.core.dom.CompilationUnit; 35 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 36 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 37 38 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; 39 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 40 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange; 41 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; 42 43 import org.eclipse.jdt.internal.ui.JavaPlugin; 44 45 56 public class CompilationUnitRewrite { 57 private ICompilationUnit fCu; 59 private List fTextEditGroups= new ArrayList (); 60 61 private CompilationUnit fRoot; private ASTRewrite fRewrite; private ImportRewrite fImportRewrite; private ImportRemover fImportRemover; private boolean fResolveBindings= true; 66 private boolean fStatementsRecovery= true; 67 private boolean fBindingsRecovery= false; 68 private final WorkingCopyOwner fOwner; 69 private IDocument fRememberContent= null; 70 71 72 public CompilationUnitRewrite(ICompilationUnit cu) { 73 this(null, cu, null); 74 } 75 76 public CompilationUnitRewrite(WorkingCopyOwner owner, ICompilationUnit cu) { 77 this(owner, cu, null); 78 } 79 80 public CompilationUnitRewrite(ICompilationUnit cu, CompilationUnit root) { 81 this(null, cu, root); 82 } 83 84 public CompilationUnitRewrite(WorkingCopyOwner owner, ICompilationUnit cu, CompilationUnit root) { 85 fOwner= owner; 86 fCu= cu; 87 fRoot= root; 88 } 89 90 public void rememberContent() { 91 fRememberContent= new Document(); 92 } 93 94 95 112 public void setResolveBindings(boolean resolve) { 113 fResolveBindings= resolve; 114 } 115 116 129 public void setStatementsRecovery(boolean statementsRecovery) { 130 fStatementsRecovery= statementsRecovery; 131 } 132 133 146 public void setBindingRecovery(boolean bindingsRecovery) { 147 fBindingsRecovery= bindingsRecovery; 148 } 149 150 public void clearASTRewrite() { 151 fRewrite= null; 152 fTextEditGroups= new ArrayList (); 153 } 154 155 public void clearImportRewrites() { 156 fImportRewrite= null; 157 } 158 159 public void clearASTAndImportRewrites() { 160 clearASTRewrite(); 161 fImportRewrite= null; 162 } 163 164 public CategorizedTextEditGroup createCategorizedGroupDescription(String name, GroupCategorySet set) { 165 CategorizedTextEditGroup result= new CategorizedTextEditGroup(name, set); 166 fTextEditGroups.add(result); 167 return result; 168 } 169 170 public TextEditGroup createGroupDescription(String name) { 171 TextEditGroup result= new TextEditGroup(name); 172 fTextEditGroups.add(result); 173 return result; 174 } 175 176 177 public CompilationUnitChange createChange() throws CoreException { 178 return createChange(true, null); 179 } 180 181 189 public CompilationUnitChange createChange(boolean generateGroups, IProgressMonitor monitor) throws CoreException { 190 return createChange(fCu.getElementName(), generateGroups, monitor); 191 } 192 193 202 public CompilationUnitChange createChange(String name, boolean generateGroups, IProgressMonitor monitor) throws CoreException { 203 CompilationUnitChange cuChange= new CompilationUnitChange(name, fCu); 204 MultiTextEdit multiEdit= new MultiTextEdit(); 205 cuChange.setEdit(multiEdit); 206 return attachChange(cuChange, generateGroups, monitor); 207 } 208 209 210 222 public CompilationUnitChange attachChange(CompilationUnitChange cuChange, boolean generateGroups, IProgressMonitor monitor) throws CoreException { 223 try { 224 boolean needsAstRewrite= fRewrite != null; boolean needsImportRemoval= fImportRemover != null && fImportRemover.hasRemovedNodes(); 226 boolean needsImportRewrite= fImportRewrite != null && fImportRewrite.hasRecordedChanges() || needsImportRemoval; 227 if (!needsAstRewrite && !needsImportRemoval && !needsImportRewrite) 228 return null; 229 230 MultiTextEdit multiEdit= (MultiTextEdit) cuChange.getEdit(); 231 if (multiEdit == null) { 232 multiEdit= new MultiTextEdit(); 233 cuChange.setEdit(multiEdit); 234 } 235 236 if (needsAstRewrite) { 237 TextEdit rewriteEdit; 238 if (fRememberContent != null) { 239 rewriteEdit= fRewrite.rewriteAST(fRememberContent, fCu.getJavaProject().getOptions(true)); 240 } else { 241 rewriteEdit= fRewrite.rewriteAST(); 242 } 243 if (!isEmptyEdit(rewriteEdit)) { 244 multiEdit.addChild(rewriteEdit); 245 if (generateGroups) { 246 for (Iterator iter= fTextEditGroups.iterator(); iter.hasNext();) { 247 TextEditGroup group= (TextEditGroup) iter.next(); 248 cuChange.addTextEditGroup(group); 249 } 250 } 251 } 252 } 253 if (needsImportRemoval) { 254 fImportRemover.applyRemoves(getImportRewrite()); 255 } 256 if (needsImportRewrite) { 257 TextEdit importsEdit= fImportRewrite.rewriteImports(monitor); 258 if (!isEmptyEdit(importsEdit)) { 259 multiEdit.addChild(importsEdit); 260 String importUpdateName= RefactoringCoreMessages.ASTData_update_imports; 261 cuChange.addTextEditGroup(new TextEditGroup(importUpdateName, importsEdit)); 262 } 263 } else { 264 265 } 266 if (isEmptyEdit(multiEdit)) 267 return null; 268 return cuChange; 269 } finally { 270 if (monitor != null) 271 monitor.done(); 272 } 273 } 274 275 private static boolean isEmptyEdit(TextEdit edit) { 276 return edit.getClass() == MultiTextEdit.class && ! edit.hasChildren(); 277 } 278 279 public ICompilationUnit getCu() { 280 return fCu; 281 } 282 283 public CompilationUnit getRoot() { 284 if (fRoot == null) 285 fRoot= new RefactoringASTParser(AST.JLS3).parse(fCu, fOwner, fResolveBindings, fStatementsRecovery, fBindingsRecovery, null); 286 return fRoot; 287 } 288 289 public AST getAST() { 290 return getRoot().getAST(); 291 } 292 293 public ASTRewrite getASTRewrite() { 294 if (fRewrite == null) { 295 fRewrite= ASTRewrite.create(getRoot().getAST()); 296 if (fRememberContent != null) { try { 298 fRememberContent.set(fCu.getSource()); 299 } catch (JavaModelException e) { 300 fRememberContent= null; 301 } 302 } 303 } 304 return fRewrite; 305 } 306 307 public ImportRewrite getImportRewrite() { 308 if (fImportRewrite == null) { 309 try { 311 if (fRoot == null) { 312 fImportRewrite= StubUtility.createImportRewrite(fCu, true); 313 } else { 314 fImportRewrite= StubUtility.createImportRewrite(getRoot(), true); 315 } 316 } catch (CoreException e) { 317 JavaPlugin.log(e); 318 throw new IllegalStateException (e.getMessage()); } 320 } 321 return fImportRewrite; 322 323 } 324 325 public ImportRemover getImportRemover() { 326 if (fImportRemover == null) { 327 fImportRemover= new ImportRemover(fCu.getJavaProject(), getRoot()); 328 } 329 return fImportRemover; 330 } 331 } 332 | Popular Tags |