Search in sources :

Example 16 with SearchMatch

use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.

the class IntroduceIndirectionRefactoring method updateReferences.

private RefactoringStatus updateReferences(IProgressMonitor monitor) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    //$NON-NLS-1$
    monitor.beginTask("", 90);
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    IMethod[] ripple = RippleMethodFinder2.getRelatedMethods(fTargetMethod, false, new NoOverrideProgressMonitor(monitor, 10), null);
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    SearchResultGroup[] references = Checks.excludeCompilationUnits(getReferences(ripple, new NoOverrideProgressMonitor(monitor, 10), result), result);
    if (result.hasFatalError())
        return result;
    result.merge(Checks.checkCompileErrorsInAffectedFiles(references));
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    int ticksPerCU = references.length == 0 ? 0 : 70 / references.length;
    for (int i = 0; i < references.length; i++) {
        SearchResultGroup group = references[i];
        SearchMatch[] searchResults = group.getSearchResults();
        CompilationUnitRewrite currentCURewrite = getCachedCURewrite(group.getCompilationUnit());
        for (int j = 0; j < searchResults.length; j++) {
            SearchMatch match = searchResults[j];
            if (match.isInsideDocComment())
                continue;
            IMember enclosingMember = (IMember) match.getElement();
            ASTNode target = getSelectedNode(group.getCompilationUnit(), currentCURewrite.getRoot(), match.getOffset(), match.getLength());
            if (target instanceof SuperMethodInvocation) {
                // Cannot retarget calls to super - add a warning
                result.merge(createWarningAboutCall(enclosingMember, target, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_super_keyword));
                continue;
            }
            //$NON-NLS-1$
            Assert.isTrue(target instanceof MethodInvocation, "Element of call should be a MethodInvocation.");
            MethodInvocation invocation = (MethodInvocation) target;
            ITypeBinding typeBinding = getExpressionType(invocation);
            if (fIntermediaryFirstParameterType == null) {
                // no highest type yet
                fIntermediaryFirstParameterType = typeBinding.getTypeDeclaration();
            } else {
                // check if current type is higher
                result.merge(findCommonParent(typeBinding.getTypeDeclaration()));
            }
            if (result.hasFatalError())
                return result;
            // create an edit for this particular call
            result.merge(updateMethodInvocation(invocation, enclosingMember, currentCURewrite));
            // does call see the intermediary method?
            // => increase visibility of the type of the intermediary method.
            result.merge(adjustVisibility(fIntermediaryType, enclosingMember.getDeclaringType(), new NoOverrideProgressMonitor(monitor, 0)));
            if (monitor.isCanceled())
                throw new OperationCanceledException();
        }
        if (!isRewriteKept(group.getCompilationUnit()))
            createChangeAndDiscardRewrite(group.getCompilationUnit());
        monitor.worked(ticksPerCU);
    }
    monitor.done();
    return result;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IMember(org.eclipse.jdt.core.IMember) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod)

Example 17 with SearchMatch

use of org.eclipse.jdt.core.search.SearchMatch in project AutoRefactor by JnRouvignac.

the class SuperCallRatherThanUselessOverridingRefactoring method isMethodUsedInItsPackage.

/**
 * This method is extremely expensive.
 */
private boolean isMethodUsedInItsPackage(IMethodBinding methodBinding, MethodDeclaration node) {
    final IPackageBinding methodPackage = methodBinding.getDeclaringClass().getPackage();
    final AtomicBoolean methodIsUsedInPackage = new AtomicBoolean(false);
    final SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) {
            methodIsUsedInPackage.set(true);
        }
    };
    try {
        final SearchEngine searchEngine = new SearchEngine();
        searchEngine.search(createPattern(methodBinding.getJavaElement(), REFERENCES, R_EXACT_MATCH), new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createJavaSearchScope(new IJavaElement[] { methodPackage.getJavaElement() }), requestor, ctx.getProgressMonitor());
        return methodIsUsedInPackage.get();
    } catch (CoreException e) {
        throw new UnhandledException(node, e);
    }
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) UnhandledException(org.autorefactor.util.UnhandledException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding)

Example 18 with SearchMatch

use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.

the class RefactoringSearchEngine method groupByCu.

/**
	 * @param matchList a List of SearchMatch
	 * @param status the status to report errors.
	 * @return a SearchResultGroup[], grouped by SearchMatch#getResource()
	 */
public static SearchResultGroup[] groupByCu(List<SearchMatch> matchList, RefactoringStatus status) {
    Map<IResource, List<SearchMatch>> grouped = new HashMap<IResource, List<SearchMatch>>();
    boolean hasPotentialMatches = false;
    boolean hasNonCuMatches = false;
    for (Iterator<SearchMatch> iter = matchList.iterator(); iter.hasNext(); ) {
        SearchMatch searchMatch = iter.next();
        if (searchMatch.getAccuracy() == SearchMatch.A_INACCURATE)
            hasPotentialMatches = true;
        if (!grouped.containsKey(searchMatch.getResource()))
            grouped.put(searchMatch.getResource(), new ArrayList<SearchMatch>(1));
        grouped.get(searchMatch.getResource()).add(searchMatch);
    }
    for (Iterator<IResource> iter = grouped.keySet().iterator(); iter.hasNext(); ) {
        IResource resource = iter.next();
        IJavaElement element = JavaCore.create(resource);
        if (!(element instanceof ICompilationUnit)) {
            iter.remove();
            hasNonCuMatches = true;
        }
    }
    SearchResultGroup[] result = new SearchResultGroup[grouped.keySet().size()];
    int i = 0;
    for (Iterator<IResource> iter = grouped.keySet().iterator(); iter.hasNext(); ) {
        IResource resource = iter.next();
        List<SearchMatch> searchMatches = grouped.get(resource);
        SearchMatch[] matchArray = searchMatches.toArray(new SearchMatch[searchMatches.size()]);
        result[i] = new SearchResultGroup(resource, matchArray);
        i++;
    }
    addStatusErrors(status, hasPotentialMatches, hasNonCuMatches);
    return result;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IResource(org.eclipse.core.resources.IResource)

Example 19 with SearchMatch

use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.

the class RefactoringSearchEngine2 method getGroupedMatches.

/**
	 * Returns the found search matches in grouped by their containing resource.
	 *
	 * @return the found search matches
	 */
private SearchResultGroup[] getGroupedMatches() {
    final Map<IResource, List<SearchMatch>> grouped = new HashMap<IResource, List<SearchMatch>>();
    List<SearchMatch> matches = null;
    IResource resource = null;
    SearchMatch match = null;
    for (final Iterator<?> iterator = getSearchMatches().iterator(); iterator.hasNext(); ) {
        match = (SearchMatch) iterator.next();
        resource = match.getResource();
        if (!grouped.containsKey(resource))
            grouped.put(resource, new ArrayList<SearchMatch>(4));
        matches = grouped.get(resource);
        matches.add(match);
    }
    if (fBinary) {
        final Collection<IResource> collection = getCollector().getBinaryResources();
        for (final Iterator<IResource> iterator = grouped.keySet().iterator(); iterator.hasNext(); ) {
            resource = iterator.next();
            if (collection.contains(resource))
                iterator.remove();
        }
    }
    final SearchResultGroup[] result = new SearchResultGroup[grouped.keySet().size()];
    int index = 0;
    for (final Iterator<IResource> iterator = grouped.keySet().iterator(); iterator.hasNext(); ) {
        resource = iterator.next();
        matches = grouped.get(resource);
        result[index++] = new SearchResultGroup(resource, matches.toArray(new SearchMatch[matches.size()]));
    }
    return result;
}
Also used : SearchMatch(org.eclipse.jdt.core.search.SearchMatch) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) IResource(org.eclipse.core.resources.IResource)

Example 20 with SearchMatch

use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.

the class RefactoringSearchEngine2 method getSearchMatches.

/**
	 * Returns the search matches filtered by their accuracy.
	 *
	 * @return the filtered search matches
	 */
private Collection<?> getSearchMatches() {
    Collection<?> results = null;
    if (fInaccurate) {
        results = new LinkedList<Object>(getCollector().getCollectedMatches());
        final Collection<SearchMatch> collection = getCollector().getInaccurateMatches();
        SearchMatch match = null;
        for (final Iterator<?> iterator = results.iterator(); iterator.hasNext(); ) {
            match = (SearchMatch) iterator.next();
            if (collection.contains(match))
                iterator.remove();
        }
    } else
        results = getCollector().getCollectedMatches();
    return results;
}
Also used : SearchMatch(org.eclipse.jdt.core.search.SearchMatch)

Aggregations

SearchMatch (org.eclipse.jdt.core.search.SearchMatch)35 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)13 SearchResultGroup (org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)12 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)10 ArrayList (java.util.ArrayList)8 CoreException (org.eclipse.core.runtime.CoreException)8 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)8 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)8 IJavaElement (org.eclipse.jdt.core.IJavaElement)7 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)5 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)5 HashMap (java.util.HashMap)4 IResource (org.eclipse.core.resources.IResource)4 IMethod (org.eclipse.jdt.core.IMethod)4 RefactoringSearchEngine (org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine)4 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 HashSet (java.util.HashSet)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 IType (org.eclipse.jdt.core.IType)3