Search in sources :

Example 46 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine 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 47 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.

the class JDTUtils method findElementsAtSelection.

public static IJavaElement[] findElementsAtSelection(ITypeRoot unit, int line, int column, PreferenceManager preferenceManager, IProgressMonitor monitor) throws JavaModelException {
    if (unit == null) {
        return null;
    }
    int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column);
    if (offset > -1) {
        return unit.codeSelect(offset, 0);
    }
    if (unit instanceof IClassFile) {
        IClassFile classFile = (IClassFile) unit;
        ContentProviderManager contentProvider = JavaLanguageServerPlugin.getContentProviderManager();
        String contents = contentProvider.getSource(classFile, monitor);
        if (contents != null) {
            IDocument document = new Document(contents);
            try {
                offset = document.getLineOffset(line) + column;
                if (offset > -1) {
                    String name = parse(contents, offset);
                    if (name == null) {
                        return null;
                    }
                    SearchPattern pattern = SearchPattern.createPattern(name, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_FULL_MATCH);
                    IJavaSearchScope scope = createSearchScope(unit.getJavaProject());
                    List<IJavaElement> elements = new ArrayList<>();
                    SearchRequestor requestor = new SearchRequestor() {

                        @Override
                        public void acceptSearchMatch(SearchMatch match) {
                            if (match.getElement() instanceof IJavaElement) {
                                elements.add((IJavaElement) match.getElement());
                            }
                        }
                    };
                    SearchEngine searchEngine = new SearchEngine();
                    searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, null);
                    return elements.toArray(new IJavaElement[0]);
                }
            } catch (BadLocationException | CoreException e) {
                JavaLanguageServerPlugin.logException(e.getMessage(), e);
            }
        }
    }
    return null;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) IClassFile(org.eclipse.jdt.core.IClassFile) ContentProviderManager(org.eclipse.jdt.ls.core.internal.managers.ContentProviderManager) ArrayList(java.util.ArrayList) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 48 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.

the class WorkspaceSymbolHandler method search.

public List<SymbolInformation> search(String query, IProgressMonitor monitor) {
    if (query == null || query.trim().isEmpty()) {
        return Collections.emptyList();
    }
    try {
        ArrayList<SymbolInformation> symbols = new ArrayList<>();
        new SearchEngine().searchAllTypeNames(null, SearchPattern.R_PATTERN_MATCH, query.toCharArray(), SearchPattern.R_CAMELCASE_MATCH, IJavaSearchConstants.TYPE, createSearchScope(), new TypeNameMatchRequestor() {

            @Override
            public void acceptTypeNameMatch(TypeNameMatch match) {
                SymbolInformation symbolInformation = new SymbolInformation();
                symbolInformation.setContainerName(match.getTypeContainerName());
                symbolInformation.setName(match.getSimpleTypeName());
                symbolInformation.setKind(mapKind(match));
                Location location;
                try {
                    if (match.getType().isBinary()) {
                        location = JDTUtils.toLocation(match.getType().getClassFile());
                    } else {
                        location = JDTUtils.toLocation(match.getType());
                    }
                } catch (Exception e) {
                    JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getSimpleTypeName(), e);
                    return;
                }
                symbolInformation.setLocation(location);
                symbols.add(symbolInformation);
            }

            private SymbolKind mapKind(TypeNameMatch match) {
                int flags = match.getModifiers();
                if (Flags.isInterface(flags)) {
                    return SymbolKind.Interface;
                }
                if (Flags.isAnnotation(flags)) {
                    return SymbolKind.Property;
                }
                if (Flags.isEnum(flags)) {
                    return SymbolKind.Enum;
                }
                return SymbolKind.Class;
            }
        }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
        return symbols;
    } catch (Exception e) {
        JavaLanguageServerPlugin.logException("Problem getting search for" + query, e);
    }
    return Collections.emptyList();
}
Also used : SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) SymbolKind(org.eclipse.lsp4j.SymbolKind) ArrayList(java.util.ArrayList) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) JavaModelException(org.eclipse.jdt.core.JavaModelException) Location(org.eclipse.lsp4j.Location)

Example 49 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.

the class RenameProcessor method renameOccurrences.

public void renameOccurrences(WorkspaceEdit edit, String newName, IProgressMonitor monitor) throws CoreException {
    if (fElement == null || !canRename()) {
        return;
    }
    IJavaElement[] elementsToSearch = null;
    if (fElement instanceof IMethod) {
        elementsToSearch = RippleMethodFinder.getRelatedMethods((IMethod) fElement, monitor, null);
    } else {
        elementsToSearch = new IJavaElement[] { fElement };
    }
    SearchPattern pattern = createOccurrenceSearchPattern(elementsToSearch);
    if (pattern == null) {
        return;
    }
    SearchEngine engine = new SearchEngine();
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object o = match.getElement();
            if (o instanceof IJavaElement) {
                IJavaElement element = (IJavaElement) o;
                ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
                if (compilationUnit == null) {
                    return;
                }
                TextEdit replaceEdit = collectMatch(match, element, compilationUnit, newName);
                if (replaceEdit != null) {
                    convert(edit, compilationUnit, replaceEdit);
                }
            }
        }
    }, monitor);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod)

Example 50 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project webtools.sourceediting by eclipse.

the class AddImportHandler method execute.

/* (non-Javadoc)
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
public Object execute(ExecutionEvent event) throws ExecutionException {
    final IEditorSite site = HandlerUtil.getActiveEditor(event).getEditorSite();
    final ISelectionProvider provider = site.getSelectionProvider();
    final ISelection selection = provider != null ? provider.getSelection() : null;
    if (selection instanceof IStructuredSelection && selection instanceof ITextSelection) {
        final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        final int offset = ((ITextSelection) selection).getOffset();
        final Object firstElement = structuredSelection.getFirstElement();
        if (firstElement instanceof IDOMNode) {
            final IDOMModel model = ((IDOMNode) firstElement).getModel();
            INodeAdapter adapter = model.getDocument().getAdapterFor(IJSPTranslation.class);
            if (adapter != null) {
                JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
                final JSPTranslationExtension translation = translationAdapter.getJSPTranslation();
                translation.reconcileCompilationUnit();
                final ICompilationUnit cu = translation.getCompilationUnit();
                CompilationUnit astRoot = SharedASTProvider.getAST(cu, SharedASTProvider.WAIT_YES, null);
                if (astRoot != null) {
                    final ASTNode node = NodeFinder.perform(astRoot, translation.getJavaOffset(offset), 0);
                    if (node != null) {
                        SimpleName name = null;
                        if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
                            name = (SimpleName) node;
                        } else if (node.getNodeType() == ASTNode.QUALIFIED_NAME) {
                            name = ((QualifiedName) node).getName();
                        }
                        if (name != null) {
                            IBinding binding = name.resolveBinding();
                            if (binding instanceof ITypeBinding && (binding.getJavaElement() == null || !binding.getJavaElement().exists())) {
                                // Look it up!
                                ITypeBinding typeBinding = (ITypeBinding) binding;
                                final IImportContainer importContainer = cu.getImportContainer();
                                if (!importContainer.getImport(typeBinding.getQualifiedName()).exists()) {
                                    final List typesFound = new ArrayList();
                                    final TypeNameMatchRequestor collector = new TypeNameMatcher(typesFound);
                                    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { cu.getJavaProject() });
                                    final int mode = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
                                    try {
                                        new SearchEngine().searchAllTypeNames(null, mode, name.getIdentifier().toCharArray(), mode, IJavaSearchConstants.CLASS_AND_INTERFACE, scope, collector, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
                                        final int length = typesFound.size();
                                        final List elements = new ArrayList();
                                        for (int i = 0; i < length; i++) {
                                            final TypeNameMatch match = (TypeNameMatch) typesFound.get(i);
                                            final int modifiers = match.getModifiers();
                                            if (!Flags.isPrivate(modifiers) && !Flags.isPackageDefault(modifiers)) {
                                                elements.add(match);
                                            }
                                        }
                                        TypeNameMatch match = null;
                                        // If there's only one match, insert it; otherwise, open the dialog to choose from the list
                                        if (elements.size() == 1) {
                                            match = (TypeNameMatch) elements.get(0);
                                        } else if (elements.size() > 1) {
                                            ElementListSelectionDialog dialog = new ElementListSelectionDialog(site.getShell(), LABEL_PROVIDER);
                                            dialog.setElements(elements.toArray(new TypeNameMatch[elements.size()]));
                                            dialog.setTitle(JSPUIMessages.AddImportHandler_title);
                                            dialog.setMessage(JSPUIMessages.AddImportHandler_label);
                                            if (dialog.open() == Window.OK) {
                                                final Object result = dialog.getFirstResult();
                                                if (result instanceof TypeNameMatch) {
                                                    match = (TypeNameMatch) result;
                                                }
                                            }
                                        }
                                        addImport(match, model.getStructuredDocument());
                                    } catch (JavaModelException e) {
                                        // $NON-NLS-1$
                                        Logger.logException("Exception while determining import.", e);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) JavaModelException(org.eclipse.jdt.core.JavaModelException) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) JSPTranslationExtension(org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) ElementListSelectionDialog(org.eclipse.ui.dialogs.ElementListSelectionDialog) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IImportContainer(org.eclipse.jdt.core.IImportContainer) ISelection(org.eclipse.jface.viewers.ISelection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) ITextSelection(org.eclipse.jface.text.ITextSelection) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) IEditorSite(org.eclipse.ui.IEditorSite)

Aggregations

SearchEngine (org.eclipse.jdt.core.search.SearchEngine)131 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)73 CoreException (org.eclipse.core.runtime.CoreException)71 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)61 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)61 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)58 ArrayList (java.util.ArrayList)52 JavaModelException (org.eclipse.jdt.core.JavaModelException)48 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)38 IType (org.eclipse.jdt.core.IType)38 IJavaElement (org.eclipse.jdt.core.IJavaElement)37 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)24 SearchParticipant (org.eclipse.jdt.core.search.SearchParticipant)24 TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)21 HashSet (java.util.HashSet)20 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)20 IMethod (org.eclipse.jdt.core.IMethod)20 TypeNameMatchRequestor (org.eclipse.jdt.core.search.TypeNameMatchRequestor)16 IJavaProject (org.eclipse.jdt.core.IJavaProject)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)12