Search in sources :

Example 16 with IJavaSearchScope

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

the class MoveCuUpdateCreator method getReferences.

private static SearchResultGroup[] getReferences(ICompilationUnit unit, IProgressMonitor pm, RefactoringStatus status) throws CoreException {
    final SearchPattern pattern = RefactoringSearchEngine.createOrPattern(unit.getTypes(), IJavaSearchConstants.REFERENCES);
    if (pattern != null) {
        String binaryRefsDescription = Messages.format(RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description, BasicElementLabels.getFileName(unit));
        ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
        Collector requestor = new Collector(((IPackageFragment) unit.getParent()), binaryRefs);
        IJavaSearchScope scope = RefactoringScopeFactory.create(unit, true, false);
        SearchResultGroup[] result = RefactoringSearchEngine.search(pattern, scope, requestor, new SubProgressMonitor(pm, 1), status);
        binaryRefs.addErrorIfNecessary(status);
        return result;
    }
    return new SearchResultGroup[] {};
}
Also used : ReferencesInBinaryContext(org.eclipse.jdt.internal.corext.refactoring.base.ReferencesInBinaryContext) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 17 with IJavaSearchScope

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

Example 18 with IJavaSearchScope

use of org.eclipse.jdt.core.search.IJavaSearchScope in project bndtools by bndtools.

the class PkgPatternsProposalProvider method doGenerateProposals.

@Override
protected Collection<? extends IContentProposal> doGenerateProposals(String contents, int position) {
    String prefix = contents.substring(0, position);
    final int replaceFromPos;
    if (prefix.startsWith("!")) {
        //$NON-NLS-1$
        prefix = prefix.substring(1);
        replaceFromPos = 1;
    } else {
        replaceFromPos = 0;
    }
    Comparator<PkgPatternProposal> comparator = new Comparator<PkgPatternProposal>() {

        public int compare(PkgPatternProposal o1, PkgPatternProposal o2) {
            int result = o1.getPackageFragment().getElementName().compareTo(o2.getPackageFragment().getElementName());
            if (result == 0) {
                result = Boolean.valueOf(o1.isWildcard()).compareTo(Boolean.valueOf(o2.isWildcard()));
            }
            return result;
        }
    };
    final TreeSet<PkgPatternProposal> result = new TreeSet<PkgPatternProposal>(comparator);
    final IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { searchContext.getJavaProject() });
    final SearchPattern pattern = SearchPattern.createPattern("*" + prefix + "*", IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
    final SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            IPackageFragment pkg = (IPackageFragment) match.getElement();
            // "java." since these cannot be imported
            if (pkg.isDefaultPackage() || pkg.getElementName().startsWith("java."))
                return;
            result.add(new PkgPatternProposal(pkg, false, replaceFromPos));
            result.add(new PkgPatternProposal(pkg, true, replaceFromPos));
        }
    };
    IRunnableWithProgress runnable = new IRunnableWithProgress() {

        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, monitor);
            } catch (CoreException e) {
                throw new InvocationTargetException(e);
            }
        }
    };
    try {
        IRunnableContext runContext = searchContext.getRunContext();
        if (runContext != null) {
            runContext.run(false, false, runnable);
        } else {
            runnable.run(new NullProgressMonitor());
        }
        return result;
    } catch (InvocationTargetException e) {
        logger.logError("Error searching for packages.", e);
        return Collections.emptyList();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        return Collections.emptyList();
    }
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InvocationTargetException(java.lang.reflect.InvocationTargetException) Comparator(java.util.Comparator) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) TreeSet(java.util.TreeSet) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern)

Example 19 with IJavaSearchScope

use of org.eclipse.jdt.core.search.IJavaSearchScope in project bndtools by bndtools.

the class PrivatePackagesPart method doAddPackages.

private void doAddPackages() {
    // Prepare the exclusion list based on existing private packages
    final Set<String> packageNameSet = new HashSet<String>(packages);
    // Create a filter from the exclusion list and packages matching
    // "java.*", which must not be included in a bundle
    IPackageFilter filter = new IPackageFilter() {

        @Override
        public boolean select(String packageName) {
            return !packageName.equals("java") && !packageName.startsWith("java.") && !packageNameSet.contains(packageName);
        }
    };
    IFormPage page = (IFormPage) getManagedForm().getContainer();
    IWorkbenchWindow window = page.getEditorSite().getWorkbenchWindow();
    // Prepare the package lister from the Java project
    IJavaProject javaProject = getJavaProject();
    if (javaProject == null) {
        MessageDialog.openError(getSection().getShell(), "Error", "Cannot add packages: unable to find a Java project associated with the editor input.");
        return;
    }
    IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject });
    JavaSearchScopePackageLister packageLister = new JavaSearchScopePackageLister(searchScope, window);
    // Create and open the dialog
    PackageSelectionDialog dialog = new PackageSelectionDialog(getSection().getShell(), packageLister, filter, "Select new packages to include in the bundle.");
    dialog.setSourceOnly(true);
    dialog.setMultipleSelection(true);
    if (dialog.open() == Window.OK) {
        Object[] results = dialog.getResult();
        List<String> added = new LinkedList<String>();
        // Select the results
        for (Object result : results) {
            String newPackageName = (String) result;
            if (packages.add(newPackageName)) {
                added.add(newPackageName);
            }
        }
        // Update the model and view
        if (!added.isEmpty()) {
            viewer.add(added.toArray());
            markDirty();
        }
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFormPage(org.eclipse.ui.forms.editor.IFormPage) LinkedList(java.util.LinkedList) IJavaProject(org.eclipse.jdt.core.IJavaProject) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) JavaSearchScopePackageLister(bndtools.internal.pkgselection.JavaSearchScopePackageLister) IPackageFilter(bndtools.internal.pkgselection.IPackageFilter) HashSet(java.util.HashSet) PackageSelectionDialog(bndtools.internal.pkgselection.PackageSelectionDialog)

Example 20 with IJavaSearchScope

use of org.eclipse.jdt.core.search.IJavaSearchScope in project bndtools by bndtools.

the class TestSuiteLabelProvider method doAdd.

void doAdd() {
    // Prepare the exclusion list based on existing test cases
    final Set<String> testSuitesSet = new HashSet<String>(testSuites);
    // Create a filter from the exclusion list
    ITestCaseFilter filter = new ITestCaseFilter() {

        @Override
        public boolean select(String testCaseName) {
            return !testSuitesSet.contains(testCaseName);
        }
    };
    IFormPage page = (IFormPage) getManagedForm().getContainer();
    IWorkbenchWindow window = page.getEditorSite().getWorkbenchWindow();
    // Prepare the package lister from the Java project
    IJavaProject javaProject = getJavaProject();
    if (javaProject == null) {
        MessageDialog.openError(getSection().getShell(), "Error", "Cannot add test cases: unable to find a Java project associated with the editor input.");
        return;
    }
    IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject });
    JavaSearchScopeTestCaseLister testCaseLister = new JavaSearchScopeTestCaseLister(searchScope, window);
    // Create and open the dialog
    TestCaseSelectionDialog dialog = new TestCaseSelectionDialog(getSection().getShell(), testCaseLister, filter, Messages.TestSuitesPart_title);
    dialog.setSourceOnly(true);
    dialog.setMultipleSelection(true);
    if (dialog.open() == Window.OK) {
        Object[] results = dialog.getResult();
        List<String> added = new LinkedList<String>();
        // Select the results
        for (Object result : results) {
            String newTestSuites = (String) result;
            if (testSuites.add(newTestSuites)) {
                added.add(newTestSuites);
            }
        }
        // Update the model and view
        if (!added.isEmpty()) {
            viewer.add(added.toArray());
            markDirty();
        }
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ITestCaseFilter(bndtools.internal.testcaseselection.ITestCaseFilter) IFormPage(org.eclipse.ui.forms.editor.IFormPage) LinkedList(java.util.LinkedList) IJavaProject(org.eclipse.jdt.core.IJavaProject) TestCaseSelectionDialog(bndtools.internal.testcaseselection.TestCaseSelectionDialog) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) JavaSearchScopeTestCaseLister(bndtools.internal.testcaseselection.JavaSearchScopeTestCaseLister) HashSet(java.util.HashSet)

Aggregations

IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)20 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)12 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)8 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 IJavaProject (org.eclipse.jdt.core.IJavaProject)5 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)5 SearchResultGroup (org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)5 HashSet (java.util.HashSet)4 CoreException (org.eclipse.core.runtime.CoreException)4 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)4 IType (org.eclipse.jdt.core.IType)4 ArrayList (java.util.ArrayList)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)3 RefactoringSearchEngine (org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine)3 IPackageFilter (bndtools.internal.pkgselection.IPackageFilter)2 JavaSearchScopePackageLister (bndtools.internal.pkgselection.JavaSearchScopePackageLister)2 PackageSelectionDialog (bndtools.internal.pkgselection.PackageSelectionDialog)2 LinkedList (java.util.LinkedList)2 IMethod (org.eclipse.jdt.core.IMethod)2