Search in sources :

Example 11 with IJavaSearchScope

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

the class NewTypeWizardPage method chooseEnclosingType.

/**
     * Opens a selection dialog that allows to select an enclosing type.
     *
     * @return returns the selected type or <code>null</code> if the dialog has been canceled. The caller typically sets
     *         the result to the enclosing type input field.
     *         <p>
     *         Clients can override this method if they want to offer a different dialog.
     *         </p>
     * @since 3.2
     */
protected IType chooseEnclosingType() {
    IPackageFragmentRoot root = getPackageFragmentRoot();
    if (root == null) {
        return null;
    }
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { root });
    FilteredTypesSelectionDialog dialog = new FilteredTypesSelectionDialog(getShell(), false, getWizard().getContainer(), scope, IJavaSearchConstants.TYPE);
    dialog.setTitle(NewWizardMessages.NewTypeWizardPage_ChooseEnclosingTypeDialog_title);
    dialog.setMessage(NewWizardMessages.NewTypeWizardPage_ChooseEnclosingTypeDialog_description);
    dialog.setInitialPattern(Signature.getSimpleName(getEnclosingTypeText()));
    if (dialog.open() == Window.OK) {
        return (IType) dialog.getFirstResult();
    }
    return null;
}
Also used : IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) FilteredTypesSelectionDialog(org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IType(org.eclipse.jdt.core.IType)

Example 12 with IJavaSearchScope

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

the class JavaContext method addImport.

/**
	 * Adds an import for type with type name <code>type</code> if possible.
	 * Returns a string which can be used to reference the type.
	 *
	 * @param type the fully qualified name of the type to import
	 * @return returns a type to which the type binding can be assigned to.
	 * 	The returned type contains is unqualified when an import could be added or was already known.
	 * 	It is fully qualified, if an import conflict prevented the import.
	 * @since 3.4
	 */
public String addImport(String type) {
    if (isReadOnly())
        return type;
    ICompilationUnit cu = getCompilationUnit();
    if (cu == null)
        return type;
    try {
        boolean qualified = type.indexOf('.') != -1;
        if (!qualified) {
            IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { cu.getJavaProject() });
            SimpleName nameNode = null;
            TypeNameMatch[] matches = findAllTypes(type, searchScope, nameNode, null, cu);
            if (// only add import if we have a single match
            matches.length != 1)
                return type;
            type = matches[0].getFullyQualifiedName();
        }
        CompilationUnit root = getASTRoot(cu);
        if (fImportRewrite == null) {
            if (root == null) {
                fImportRewrite = StubUtility.createImportRewrite(cu, true);
            } else {
                fImportRewrite = StubUtility.createImportRewrite(root, true);
            }
        }
        ImportRewriteContext context;
        if (root == null)
            context = null;
        else
            context = new ContextSensitiveImportRewriteContext(root, getCompletionOffset(), fImportRewrite);
        return fImportRewrite.addImport(type, context);
    } catch (JavaModelException e) {
        handleException(null, e);
        return type;
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) JavaModelException(org.eclipse.jdt.core.JavaModelException) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SimpleName(org.eclipse.jdt.core.dom.SimpleName)

Example 13 with IJavaSearchScope

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

the class RenameFieldProcessor method addAccessorOccurrences.

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);
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) TextEdit(org.eclipse.text.edits.TextEdit) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)

Example 14 with IJavaSearchScope

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

the class RenameMethodProcessor method searchForOuterTypesOfReferences.

private IType[] searchForOuterTypesOfReferences(IMethod[] newNameMethods, IProgressMonitor pm) throws CoreException {
    final Set<IType> outerTypesOfReferences = new HashSet<IType>();
    SearchPattern pattern = RefactoringSearchEngine.createOrPattern(newNameMethods, IJavaSearchConstants.REFERENCES);
    IJavaSearchScope scope = createRefactoringScope(getMethod());
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object element = match.getElement();
            if (!(element instanceof IMember))
                // e.g. an IImportDeclaration for a static method import
                return;
            IMember member = (IMember) element;
            IType declaring = member.getDeclaringType();
            if (declaring == null)
                return;
            IType outer = declaring.getDeclaringType();
            if (outer != null)
                outerTypesOfReferences.add(declaring);
        }
    };
    new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
    return outerTypesOfReferences.toArray(new IType[outerTypesOfReferences.size()]);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) RefactoringSearchEngine(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMember(org.eclipse.jdt.core.IMember) HashSet(java.util.HashSet) IType(org.eclipse.jdt.core.IType)

Example 15 with IJavaSearchScope

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

the class RippleMethodFinder2 method findAllDeclarations.

private void findAllDeclarations(IProgressMonitor monitor, WorkingCopyOwner owner) throws CoreException {
    fDeclarations = new ArrayList<IMethod>();
    class MethodRequestor extends SearchRequestor {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            IMethod method = (IMethod) match.getElement();
            boolean isBinary = method.isBinary();
            if (fBinaryRefs != null || !(fExcludeBinaries && isBinary)) {
                fDeclarations.add(method);
            }
            if (isBinary && fBinaryRefs != null) {
                fDeclarationToMatch.put(method, match);
            }
        }
    }
    int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
    int matchRule = SearchPattern.R_ERASURE_MATCH | SearchPattern.R_CASE_SENSITIVE;
    SearchPattern pattern = SearchPattern.createPattern(fMethod, limitTo, matchRule);
    SearchParticipant[] participants = SearchUtils.getDefaultSearchParticipants();
    IJavaSearchScope scope = RefactoringScopeFactory.createRelatedProjectsScope(fMethod.getJavaProject(), IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SYSTEM_LIBRARIES);
    MethodRequestor requestor = new MethodRequestor();
    SearchEngine searchEngine = owner != null ? new SearchEngine(owner) : new SearchEngine();
    searchEngine.search(pattern, participants, scope, requestor, monitor);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant)

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