Search in sources :

Example 1 with SearchResultGroup

use of org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup in project che by eclipse.

the class RenameMethodProcessor method batchFindNewOccurrences.

//Lower memory footprint than batchFindNewOccurrences. Not used because it is too slow.
//Final solution is maybe to do searches in chunks of ~ 50 CUs.
//	private SearchResultGroup[] findNewOccurrences(IMethod[] newMethods, ICompilationUnit[] newDeclarationWCs, IProgressMonitor pm) throws CoreException {
//		pm.beginTask("", fOccurrences.length * 2); //$NON-NLS-1$
//
//		SearchPattern refsPattern= RefactoringSearchEngine.createOrPattern(newMethods, IJavaSearchConstants.REFERENCES);
//		SearchParticipant[] searchParticipants= SearchUtils.getDefaultSearchParticipants();
//		IJavaSearchScope scope= RefactoringScopeFactory.create(newMethods);
//		MethodOccurenceCollector requestor= new MethodOccurenceCollector(getNewElementName());
//		SearchEngine searchEngine= new SearchEngine(fWorkingCopyOwner);
//
//		//TODO: should process only references
//		for (int j= 0; j < fOccurrences.length; j++) { //should be getReferences()
//			//cut memory peak by holding only one reference CU at a time in memory
//			ICompilationUnit originalCu= fOccurrences[j].getCompilationUnit();
//			ICompilationUnit newWc= null;
//			try {
//				ICompilationUnit wc= RenameAnalyzeUtil.findWorkingCopyForCu(newDeclarationWCs, originalCu);
//				if (wc == null) {
//					newWc= RenameAnalyzeUtil.createNewWorkingCopy(originalCu, fChangeManager, fWorkingCopyOwner,
//							new SubProgressMonitor(pm, 1));
//				}
//				searchEngine.search(refsPattern, searchParticipants, scope,	requestor, new SubProgressMonitor(pm, 1));
//			} finally {
//				if (newWc != null)
//					newWc.discardWorkingCopy();
//			}
//		}
//		SearchResultGroup[] newResults= RefactoringSearchEngine.groupByResource(requestor.getResults());
//		pm.done();
//		return newResults;
//	}
private SearchResultGroup[] batchFindNewOccurrences(IMethod[] wcNewMethods, final IMethod[] wcOldMethods, ICompilationUnit[] newDeclarationWCs, IProgressMonitor pm, RefactoringStatus status) throws CoreException {
    //$NON-NLS-1$
    pm.beginTask("", 2);
    SearchPattern refsPattern = RefactoringSearchEngine.createOrPattern(wcNewMethods, IJavaSearchConstants.REFERENCES);
    SearchParticipant[] searchParticipants = SearchUtils.getDefaultSearchParticipants();
    IJavaSearchScope scope = RefactoringScopeFactory.create(wcNewMethods);
    MethodOccurenceCollector requestor;
    if (getDelegateUpdating()) {
        // There will be two new matches inside the delegate(s) (the invocation
        // and the javadoc) which are OK and must not be reported.
        // Note that except these ocurrences, the delegate bodies are empty
        // (as they were created this way).
        requestor = new MethodOccurenceCollector(getNewElementName()) {

            @Override
            public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException {
                for (int i = 0; i < wcOldMethods.length; i++) if (wcOldMethods[i].equals(match.getElement()))
                    return;
                super.acceptSearchMatch(unit, match);
            }
        };
    } else
        requestor = new MethodOccurenceCollector(getNewElementName());
    SearchEngine searchEngine = new SearchEngine(fWorkingCopyOwner);
    ArrayList<ICompilationUnit> needWCs = new ArrayList<ICompilationUnit>();
    HashSet<ICompilationUnit> declaringCUs = new HashSet<ICompilationUnit>(newDeclarationWCs.length);
    for (int i = 0; i < newDeclarationWCs.length; i++) declaringCUs.add(newDeclarationWCs[i].getPrimary());
    for (int i = 0; i < fOccurrences.length; i++) {
        ICompilationUnit cu = fOccurrences[i].getCompilationUnit();
        if (!declaringCUs.contains(cu))
            needWCs.add(cu);
    }
    ICompilationUnit[] otherWCs = null;
    try {
        otherWCs = RenameAnalyzeUtil.createNewWorkingCopies(needWCs.toArray(new ICompilationUnit[needWCs.size()]), fChangeManager, fWorkingCopyOwner, new SubProgressMonitor(pm, 1));
        searchEngine.search(refsPattern, searchParticipants, scope, requestor, new SubProgressMonitor(pm, 1));
    } finally {
        pm.done();
        if (otherWCs != null) {
            for (int i = 0; i < otherWCs.length; i++) {
                otherWCs[i].discardWorkingCopy();
            }
        }
    }
    SearchResultGroup[] newResults = RefactoringSearchEngine.groupByCu(requestor.getResults(), status);
    return newResults;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) ArrayList(java.util.ArrayList) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) RefactoringSearchEngine(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) HashSet(java.util.HashSet)

Example 2 with SearchResultGroup

use of org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup in project che by eclipse.

the class RenameFieldProcessor method checkAccessorDeclarations.

private RefactoringStatus checkAccessorDeclarations(IProgressMonitor pm, IMethod existingAccessor) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    SearchPattern pattern = SearchPattern.createPattern(existingAccessor, IJavaSearchConstants.DECLARATIONS, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
    IJavaSearchScope scope = SearchEngine.createHierarchyScope(fField.getDeclaringType());
    SearchResultGroup[] groupDeclarations = RefactoringSearchEngine.search(pattern, scope, pm, result);
    Assert.isTrue(groupDeclarations.length > 0);
    if (groupDeclarations.length != 1) {
        String message = Messages.format(RefactoringCoreMessages.RenameFieldRefactoring_overridden, JavaElementUtil.createMethodSignature(existingAccessor));
        result.addError(message);
    } else {
        SearchResultGroup group = groupDeclarations[0];
        Assert.isTrue(group.getSearchResults().length > 0);
        if (group.getSearchResults().length != 1) {
            String message = Messages.format(RefactoringCoreMessages.RenameFieldRefactoring_overridden_or_overrides, JavaElementUtil.createMethodSignature(existingAccessor));
            result.addError(message);
        }
    }
    return result;
}
Also used : IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)

Example 3 with SearchResultGroup

use of org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup in project che by eclipse.

the class RenameFieldProcessor method getReferences.

private SearchResultGroup[] getReferences(IProgressMonitor pm, RefactoringStatus status) throws CoreException {
    String binaryRefsDescription = Messages.format(RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description, BasicElementLabels.getJavaElementName(getCurrentElementName()));
    ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
    SearchResultGroup[] result = RefactoringSearchEngine.search(createSearchPattern(), createRefactoringScope(), new CuCollectingSearchRequestor(binaryRefs), pm, status);
    binaryRefs.addErrorIfNecessary(status);
    return result;
}
Also used : ReferencesInBinaryContext(org.eclipse.jdt.internal.corext.refactoring.base.ReferencesInBinaryContext) CuCollectingSearchRequestor(org.eclipse.jdt.internal.corext.refactoring.CuCollectingSearchRequestor) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)

Example 4 with SearchResultGroup

use of org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup in project che by eclipse.

the class RenameAnalyzeUtil method existsInNewOccurrences.

private static boolean existsInNewOccurrences(SearchMatch searchResult, SearchResultGroup[] newOccurrences, TextChangeManager manager) {
    SearchResultGroup newGroup = findOccurrenceGroup(searchResult.getResource(), newOccurrences);
    if (newGroup == null)
        return false;
    IRegion oldEditRange = getCorrespondingEditChangeRange(searchResult, manager);
    if (oldEditRange == null)
        return false;
    SearchMatch[] newSearchResults = newGroup.getSearchResults();
    int oldRangeOffset = oldEditRange.getOffset();
    for (int i = 0; i < newSearchResults.length; i++) {
        if (newSearchResults[i].getOffset() == oldRangeOffset)
            return true;
    }
    return false;
}
Also used : SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) IRegion(org.eclipse.jface.text.IRegion)

Example 5 with SearchResultGroup

use of org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup in project che by eclipse.

the class IntroduceFactoryRefactoring method findNonPrimaryType.

private IType findNonPrimaryType(String fullyQualifiedName, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException {
    SearchPattern p = SearchPattern.createPattern(fullyQualifiedName, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
    final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(p);
    engine.setFiltering(true, true);
    engine.setScope(RefactoringScopeFactory.create(fCtorBinding.getJavaElement().getJavaProject()));
    engine.setStatus(status);
    engine.searchPattern(new SubProgressMonitor(pm, 1));
    SearchResultGroup[] groups = (SearchResultGroup[]) engine.getResults();
    if (groups.length != 0) {
        for (int i = 0; i < groups.length; i++) {
            SearchMatch[] matches = groups[i].getSearchResults();
            for (int j = 0; j < matches.length; j++) {
                if (matches[j].getAccuracy() == SearchMatch.A_ACCURATE)
                    return (IType) matches[j].getElement();
            }
        }
    }
    return null;
}
Also used : SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) RefactoringSearchEngine2(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Aggregations

SearchResultGroup (org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)23 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)15 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)12 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)8 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)7 ArrayList (java.util.ArrayList)6 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)6 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)5 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 IType (org.eclipse.jdt.core.IType)4 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)4 HashSet (java.util.HashSet)3 RefactoringSearchEngine2 (org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2)3 TextChange (org.eclipse.ltk.core.refactoring.TextChange)3 HashMap (java.util.HashMap)2 CoreException (org.eclipse.core.runtime.CoreException)2 IMethod (org.eclipse.jdt.core.IMethod)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)2