Search in sources :

Example 6 with SearchPattern

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

the class CreateCopyOfCompilationUnitChange method getReferences.

private static SearchResultGroup getReferences(final ICompilationUnit copy, IProgressMonitor monitor) throws JavaModelException {
    final ICompilationUnit[] copies = new ICompilationUnit[] { copy };
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(copies);
    final IType type = copy.findPrimaryType();
    if (type == null)
        return null;
    SearchPattern pattern = createSearchPattern(type);
    final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(pattern);
    engine.setScope(scope);
    engine.setWorkingCopies(copies);
    engine.setRequestor(new IRefactoringSearchRequestor() {

        TypeOccurrenceCollector fTypeOccurrenceCollector = new TypeOccurrenceCollector(type);

        public SearchMatch acceptSearchMatch(SearchMatch match) {
            try {
                return fTypeOccurrenceCollector.acceptSearchMatch2(copy, match);
            } catch (CoreException e) {
                JavaPlugin.log(e);
                return null;
            }
        }
    });
    engine.searchPattern(monitor);
    final Object[] results = engine.getResults();
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=106127)
    for (int index = 0; index < results.length; index++) {
        SearchResultGroup group = (SearchResultGroup) results[index];
        if (group.getCompilationUnit().equals(copy))
            return group;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) IType(org.eclipse.jdt.core.IType) IRefactoringSearchRequestor(org.eclipse.jdt.internal.corext.refactoring.IRefactoringSearchRequestor) CoreException(org.eclipse.core.runtime.CoreException) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) TypeOccurrenceCollector(org.eclipse.jdt.internal.corext.refactoring.rename.TypeOccurrenceCollector) RefactoringSearchEngine2(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2)

Example 7 with SearchPattern

use of org.eclipse.jdt.core.search.SearchPattern 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)

Example 8 with SearchPattern

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

the class IntroduceIndirectionRefactoring method getReferences.

private SearchResultGroup[] getReferences(IMethod[] methods, IProgressMonitor pm, RefactoringStatus status) throws CoreException {
    SearchPattern pattern = RefactoringSearchEngine.createOrPattern(methods, IJavaSearchConstants.REFERENCES);
    IJavaSearchScope scope = RefactoringScopeFactory.create(fIntermediaryType, false);
    return RefactoringSearchEngine.search(pattern, scope, pm, status);
}
Also used : IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern)

Example 9 with SearchPattern

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

the class RefactoringSearchEngine method createOrPattern.

public static SearchPattern createOrPattern(IJavaElement[] elements, int limitTo) {
    if (elements == null || elements.length == 0)
        return null;
    Set<IJavaElement> set = new HashSet<IJavaElement>(Arrays.asList(elements));
    Iterator<IJavaElement> iter = set.iterator();
    IJavaElement first = iter.next();
    SearchPattern pattern = SearchPattern.createPattern(first, limitTo, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
    if (// check for bug 90138
    pattern == null)
        //$NON-NLS-1$ //$NON-NLS-2$
        throw new IllegalArgumentException("Invalid java element: " + first.getHandleIdentifier() + "\n" + first.toString());
    while (iter.hasNext()) {
        IJavaElement each = iter.next();
        SearchPattern nextPattern = SearchPattern.createPattern(each, limitTo, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
        if (// check for bug 90138
        nextPattern == null)
            //$NON-NLS-1$ //$NON-NLS-2$
            throw new IllegalArgumentException("Invalid java element: " + each.getHandleIdentifier() + "\n" + each.toString());
        pattern = SearchPattern.createOrPattern(pattern, nextPattern);
    }
    return pattern;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) HashSet(java.util.HashSet)

Example 10 with SearchPattern

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

the class RenameTypeProcessor method checkConflictingTypes.

private RefactoringStatus checkConflictingTypes(IProgressMonitor pm) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    IJavaSearchScope scope = RefactoringScopeFactory.create(fType);
    SearchPattern pattern = SearchPattern.createPattern(getNewElementName(), IJavaSearchConstants.TYPE, IJavaSearchConstants.ALL_OCCURRENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
    ICompilationUnit[] cusWithReferencesToConflictingTypes = RefactoringSearchEngine.findAffectedCompilationUnits(pattern, scope, pm, result);
    if (cusWithReferencesToConflictingTypes.length == 0)
        return result;
    ICompilationUnit[] cusWithReferencesToRenamedType = getCus(fReferences);
    Set<ICompilationUnit> conflicts = getIntersection(cusWithReferencesToRenamedType, cusWithReferencesToConflictingTypes);
    if (cusWithReferencesToConflictingTypes.length > 0) {
        cus: for (ICompilationUnit cu : cusWithReferencesToConflictingTypes) {
            String packageName = fType.getPackageFragment().getElementName();
            if (((IPackageFragment) cu.getParent()).getElementName().equals(packageName)) {
                boolean hasOnDemandImport = false;
                IImportDeclaration[] imports = cu.getImports();
                for (IImportDeclaration importDecl : imports) {
                    if (importDecl.isOnDemand()) {
                        hasOnDemandImport = true;
                    } else {
                        String importName = importDecl.getElementName();
                        int packageLength = importName.length() - getNewElementName().length() - 1;
                        if (packageLength > 0 && importName.endsWith(getNewElementName()) && importName.charAt(packageLength) == '.') {
                            // explicit import from another package => no problem
                            continue cus;
                        }
                    }
                }
                if (hasOnDemandImport) {
                    // the renamed type in the same package will shadow the *-imported type
                    conflicts.add(cu);
                }
            }
        }
    }
    for (ICompilationUnit conflict : conflicts) {
        RefactoringStatusContext context = JavaStatusContext.create(conflict);
        String message = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_another_type, new String[] { getNewElementLabel(), BasicElementLabels.getFileName(conflict) });
        result.addError(message, context);
    }
    return result;
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration)

Aggregations

SearchPattern (org.eclipse.jdt.core.search.SearchPattern)21 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)13 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)12 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)9 CoreException (org.eclipse.core.runtime.CoreException)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)6 SearchResultGroup (org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)6 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)5 IJavaElement (org.eclipse.jdt.core.IJavaElement)5 ArrayList (java.util.ArrayList)4 IMethod (org.eclipse.jdt.core.IMethod)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 RefactoringSearchEngine (org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine)4 HashSet (java.util.HashSet)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 IType (org.eclipse.jdt.core.IType)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 IField (org.eclipse.jdt.core.IField)2