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); } } }
IMarker createMarkerFromSearchMatch(IFile file, SearchMatch match) { IMarker marker = null; try { if (CHOICE_METHOD_DECLARATION.equals(mChoice)) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put(IMarker.CHAR_START, new Integer(match.getOffset())); map.put(IMarker.CHAR_END, new Integer(match.getOffset() + match.getLength())); marker = file.createMarker(IMarker.TEXT); marker.setAttributes(map); } else if (CHOICE_ERROR_LINE.equals(mChoice)) { marker = file.createMarker(IMarker.TEXT); marker.setAttribute(IMarker.LINE_NUMBER, mLineNumber); } } catch (CoreException e) { Status s = new Status(Status.ERROR, DdmsPlugin.PLUGIN_ID, e.getMessage(), e); DdmsPlugin.getDefault().getLog().log(s); } return marker; }
protected final ReplaceEdit createReplaceEdit(SearchMatch searchResult, ICompilationUnit cu) { if (searchResult.isImplicit()) { // handle Annotation Element references, see bug 94062 StringBuffer sb = new StringBuffer(getNewElementName()); if (JavaCore.INSERT.equals( cu.getJavaProject() .getOption( DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR, true))) sb.append(' '); sb.append('='); if (JavaCore.INSERT.equals( cu.getJavaProject() .getOption( DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ASSIGNMENT_OPERATOR, true))) sb.append(' '); return new ReplaceEdit(searchResult.getOffset(), 0, sb.toString()); } else { return new ReplaceEdit( searchResult.getOffset(), searchResult.getLength(), getNewElementName()); } }
/** * @param change * @param oldMatches * @return Map <Integer updatedOffset, SearchMatch oldMatch> */ private static Map<Integer, SearchMatch> getUpdatedChangeOffsets( TextChange change, SearchMatch[] oldMatches) { Map<Integer, SearchMatch> updatedOffsets = new HashMap<>(); Map<Integer, Integer> oldToUpdatedOffsets = getEditChangeOffsetUpdates(change); for (int i = 0; i < oldMatches.length; i++) { SearchMatch oldMatch = oldMatches[i]; Integer updatedOffset = oldToUpdatedOffsets.get(new Integer(oldMatch.getOffset())); if (updatedOffset == null) updatedOffset = new Integer(-1); // match not updated updatedOffsets.put(updatedOffset, oldMatch); } return updatedOffsets; }
private static void addShadowsError( ICompilationUnit cu, SearchMatch oldMatch, RefactoringStatus result) { // Old match not found in new matches -> reference has been shadowed // TODO: should not have to filter declarations: if (oldMatch instanceof MethodDeclarationMatch || oldMatch instanceof FieldDeclarationMatch) return; ISourceRange range = new SourceRange(oldMatch.getOffset(), oldMatch.getLength()); RefactoringStatusContext context = JavaStatusContext.create(cu, range); String message = Messages.format( RefactoringCoreMessages.RenameAnalyzeUtil_shadows, BasicElementLabels.getFileName(cu)); result.addError(message, context); }
@Override public void acceptSearchMatch(SearchMatch match) { if (fRequireExactMatch && (match.getAccuracy() != SearchMatch.A_ACCURATE)) { return; } if (match.isInsideDocComment()) { return; } if (match.getElement() != null && match.getElement() instanceof IMember) { IMember member = (IMember) match.getElement(); switch (member.getElementType()) { case IJavaElement.METHOD: case IJavaElement.TYPE: case IJavaElement.FIELD: case IJavaElement.INITIALIZER: fSearchResults.addMember( member, member, match.getOffset(), match.getOffset() + match.getLength()); break; } } }
/* * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch) */ @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { if (match.getAccuracy() == SearchMatch.A_INACCURATE) return; int offset = match.getOffset(); int length = match.getLength(); if (offset == -1 || length == -1) return; if (!(match.getElement() instanceof IJavaElement)) return; IJavaElement javaElement = (IJavaElement) match.getElement(); // ignore matches in import declarations: if (javaElement.getElementType() == IJavaElement.IMPORT_DECLARATION) return; if (javaElement.getElementType() == IJavaElement.CLASS_FILE) return; // matches in import statements of class files if (javaElement.getElementType() == IJavaElement.TYPE) return; // classes extending the accessor class and workaround for bug 61286 // heuristic: ignore matches in resource bundle name field: if (javaElement.getElementType() == IJavaElement.FIELD) { IField field = (IField) javaElement; String source = field.getSource(); if (source != null && fgGetClassNameMatcher.match(source)) return; } if (javaElement instanceof ISourceReference) { String source = ((ISourceReference) javaElement).getSource(); if (source != null) { if (source.indexOf("NLS.initializeMessages") != -1) // $NON-NLS-1$ return; } } // found reference to NLS Wrapper - now check if the key is there: Position mutableKeyPosition = new Position(offset, length); // TODO: What to do if argument string not found? Currently adds a match with type name. String key = findKey(mutableKeyPosition, javaElement); if (key == null || isKeyDefined(key)) return; ICompilationUnit[] allCompilationUnits = JavaModelUtil.getAllCompilationUnits(new IJavaElement[] {javaElement}); Object element = javaElement; if (allCompilationUnits != null && allCompilationUnits.length == 1) element = allCompilationUnits[0]; fResult.addMatch( new Match(element, mutableKeyPosition.getOffset(), mutableKeyPosition.getLength())); }
private static void analyzeChanges( ICompilationUnit cu, TextChange change, SearchMatch[] oldMatches, SearchMatch[] newMatches, String newElementName, RefactoringStatus result) { Map<Integer, SearchMatch> updatedOldOffsets = getUpdatedChangeOffsets(change, oldMatches); for (int i = 0; i < newMatches.length; i++) { SearchMatch newMatch = newMatches[i]; Integer offsetInNew = new Integer(newMatch.getOffset()); SearchMatch oldMatch = updatedOldOffsets.remove(offsetInNew); if (oldMatch == null) { addReferenceShadowedError(cu, newMatch, newElementName, result); } } for (Iterator<SearchMatch> iter = updatedOldOffsets.values().iterator(); iter.hasNext(); ) { // remaining old matches are not found any more -> they have been shadowed SearchMatch oldMatch = iter.next(); addShadowsError(cu, oldMatch, result); } }
private static IRegion createTextRange(SearchMatch searchResult) { return new Region(searchResult.getOffset(), searchResult.getLength()); }
private TextEdit createTextChange(SearchMatch match) { return new ReplaceEdit(match.getOffset(), match.getLength(), getNewElementName()); }
/* (non-Javadoc) * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch) */ public void acceptSearchMatch(SearchMatch match) throws CoreException { if (!match.isInsideDocComment()) fMatch = new Match( match.getElement(), Match.UNIT_CHARACTER, match.getOffset(), match.getLength()); }