private void addAccessorOccurrences( IProgressMonitor pm, IMethod accessor, String editName, String newAccessorName, RefactoringStatus status) throws CoreException { Assert.isTrue(accessor.exists()); IJavaSearchScope scope = RefactoringScopeFactory.create(accessor); SearchPattern pattern = SearchPattern.createPattern( accessor, IJavaSearchConstants.ALL_OCCURRENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE); SearchResultGroup[] groupedResults = RefactoringSearchEngine.search( pattern, scope, new MethodOccurenceCollector(accessor.getElementName()), pm, status); for (int i = 0; i < groupedResults.length; i++) { ICompilationUnit cu = groupedResults[i].getCompilationUnit(); if (cu == null) continue; SearchMatch[] results = groupedResults[i].getSearchResults(); for (int j = 0; j < results.length; j++) { SearchMatch searchResult = results[j]; TextEdit edit = new ReplaceEdit(searchResult.getOffset(), searchResult.getLength(), newAccessorName); addTextEdit(fChangeManager.get(cu), editName, edit); } } }
private void addDeclarationUpdate() throws CoreException { ISourceRange nameRange = fField.getNameRange(); TextEdit textEdit = new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), getNewElementName()); ICompilationUnit cu = fField.getCompilationUnit(); String groupName = RefactoringCoreMessages.RenameFieldRefactoring_Update_field_declaration; addTextEdit(fChangeManager.get(cu), groupName, textEdit); }
static ICompilationUnit createNewWorkingCopy( ICompilationUnit cu, TextChangeManager manager, WorkingCopyOwner owner, SubProgressMonitor pm) throws CoreException { ICompilationUnit newWc = cu.getWorkingCopy(owner, null); String previewContent = manager.get(cu).getPreviewContent(new NullProgressMonitor()); newWc.getBuffer().setContents(previewContent); newWc.reconcile(ICompilationUnit.NO_AST, false, owner, pm); return newWc; }
private void createEdits( ICompilationUnit unit, ASTRewrite rewriter, List<TextEditGroup> descriptions) throws CoreException { TextChange change = fChangeManager.get(unit); MultiTextEdit root = new MultiTextEdit(); change.setEdit(root); for (TextEditGroup group : descriptions) change.addTextEditGroup(group); root.addChild(rewriter.rewriteAST()); }
private void addReferenceUpdates(IProgressMonitor pm) { pm.beginTask("", fReferences.length); // $NON-NLS-1$ String editName = RefactoringCoreMessages.RenameFieldRefactoring_Update_field_reference; for (int i = 0; i < fReferences.length; i++) { ICompilationUnit cu = fReferences[i].getCompilationUnit(); if (cu == null) continue; SearchMatch[] results = fReferences[i].getSearchResults(); for (int j = 0; j < results.length; j++) { addTextEdit(fChangeManager.get(cu), editName, createTextChange(results[j])); } pm.worked(1); } }
private void addTextUpdates(ICompilationUnit cu, Set<TextMatch> matches) { for (Iterator<TextMatch> resultIter = matches.iterator(); resultIter.hasNext(); ) { TextMatch match = resultIter.next(); if (!match.isQualified() && fOnlyQualified) continue; int matchStart = match.getStartPosition(); ReplaceEdit edit = new ReplaceEdit(matchStart, fCurrentNameLength, fNewName); try { TextChangeCompatibility.addTextEdit( fManager.get(cu), TEXT_EDIT_LABEL, edit, TEXTUAL_MATCHES); } catch (MalformedTreeException e) { // conflicting update -> omit text match } } }
// TODO: Currently filters out declarations (MethodDeclarationMatch, FieldDeclarationMatch). // Long term solution: only pass reference search results in. static RefactoringStatus analyzeRenameChanges2( TextChangeManager manager, SearchResultGroup[] oldReferences, SearchResultGroup[] newReferences, String newElementName) { RefactoringStatus result = new RefactoringStatus(); HashMap<ICompilationUnit, SearchMatch[]> cuToNewResults = new HashMap<>(newReferences.length); for (int i1 = 0; i1 < newReferences.length; i1++) { ICompilationUnit cu = newReferences[i1].getCompilationUnit(); if (cu != null) cuToNewResults.put(cu.getPrimary(), newReferences[i1].getSearchResults()); } for (int i = 0; i < oldReferences.length; i++) { SearchResultGroup oldGroup = oldReferences[i]; SearchMatch[] oldMatches = oldGroup.getSearchResults(); ICompilationUnit cu = oldGroup.getCompilationUnit(); if (cu == null) continue; SearchMatch[] newSearchMatches = cuToNewResults.remove(cu); if (newSearchMatches == null) { for (int j = 0; j < oldMatches.length; j++) { SearchMatch oldMatch = oldMatches[j]; addShadowsError(cu, oldMatch, result); } } else { analyzeChanges(cu, manager.get(cu), oldMatches, newSearchMatches, newElementName, result); } } for (Iterator<Entry<ICompilationUnit, SearchMatch[]>> iter = cuToNewResults.entrySet().iterator(); iter.hasNext(); ) { Entry<ICompilationUnit, SearchMatch[]> entry = iter.next(); ICompilationUnit cu = entry.getKey(); SearchMatch[] newSearchMatches = entry.getValue(); for (int i = 0; i < newSearchMatches.length; i++) { SearchMatch newMatch = newSearchMatches[i]; addReferenceShadowedError(cu, newMatch, newElementName, result); } } return result; }
private static TextChange getTextChange(SearchMatch searchResult, TextChangeManager manager) { ICompilationUnit cu = SearchUtils.getCompilationUnit(searchResult); if (cu == null) return null; return manager.get(cu); }
/** * Add occurrences * * @param manager the text change manager * @param pm the progress monitor * @param status the status * @throws CoreException if change creation failed */ protected void addOccurrences( TextChangeManager manager, IProgressMonitor pm, RefactoringStatus status) throws CoreException /*thrown in subtype*/ { pm.beginTask("", fOccurrences.length); // $NON-NLS-1$ for (int i = 0; i < fOccurrences.length; i++) { ICompilationUnit cu = fOccurrences[i].getCompilationUnit(); if (cu == null) continue; SearchMatch[] results = fOccurrences[i].getSearchResults(); // Split matches into declaration and non-declaration matches List<SearchMatch> declarationsInThisCu = new ArrayList<>(); List<SearchMatch> referencesInThisCu = new ArrayList<>(); for (int j = 0; j < results.length; j++) { if (results[j] instanceof MethodDeclarationMatch) declarationsInThisCu.add(results[j]); else referencesInThisCu.add(results[j]); } // First, handle the declarations if (declarationsInThisCu.size() > 0) { if (fDelegateUpdating) { // Update with delegates CompilationUnitRewrite rewrite = new CompilationUnitRewrite(cu); rewrite.setResolveBindings(true); for (Iterator<SearchMatch> iter = declarationsInThisCu.iterator(); iter.hasNext(); ) { SearchMatch element = iter.next(); MethodDeclaration method = ASTNodeSearchUtil.getMethodDeclarationNode( (IMethod) element.getElement(), rewrite.getRoot()); DelegateCreator creator = new DelegateMethodCreator(); creator.setDeclareDeprecated(fDelegateDeprecation); creator.setDeclaration(method); creator.setSourceRewrite(rewrite); creator.setNewElementName(getNewElementName()); creator.prepareDelegate(); creator.createEdit(); } // Need to handle all delegates first as this // creates a completely new change object. TextChange changeForThisCu = rewrite.createChange(true); changeForThisCu.setKeepPreviewEdits(true); manager.manage(cu, changeForThisCu); } // Update the normal methods for (Iterator<SearchMatch> iter = declarationsInThisCu.iterator(); iter.hasNext(); ) { SearchMatch element = iter.next(); simpleUpdate(element, cu, manager.get(cu)); } } // Second, handle references if (fUpdateReferences) { for (Iterator<SearchMatch> iter = referencesInThisCu.iterator(); iter.hasNext(); ) { SearchMatch element = iter.next(); simpleUpdate(element, cu, manager.get(cu)); } } pm.worked(1); if (pm.isCanceled()) throw new OperationCanceledException(); } pm.done(); }