Search in sources :

Example 26 with SearchEngine

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

the class ClassFileUtil method getURI.

public static String getURI(IProject project, String fqcn) throws JavaModelException {
    Assert.isNotNull(project, "Project can't be null");
    Assert.isNotNull(fqcn, "FQCN can't be null");
    IJavaProject javaProject = JavaCore.create(project);
    int lastDot = fqcn.lastIndexOf(".");
    String packageName = lastDot > 0 ? fqcn.substring(0, lastDot) : "";
    String className = lastDot > 0 ? fqcn.substring(lastDot + 1) : fqcn;
    ClassUriExtractor extractor = new ClassUriExtractor();
    new SearchEngine().searchAllTypeNames(packageName.toCharArray(), SearchPattern.R_EXACT_MATCH, className.toCharArray(), SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, JDTUtils.createSearchScope(javaProject, JavaLanguageServerPlugin.getPreferencesManager()), extractor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor());
    return extractor.uri;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) SearchEngine(org.eclipse.jdt.core.search.SearchEngine)

Example 27 with SearchEngine

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

the class JdtBasedConstructorScope method collectContents.

public void collectContents(IJavaSearchScope searchScope, SearchRequestor searchRequestor) throws CoreException {
    SearchPattern pattern = new ConstructorDeclarationPattern(null, null, SearchPattern.R_PREFIX_MATCH);
    new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), searchScope, searchRequestor, new NullProgressMonitor());
}
Also used : ConstructorDeclarationPattern(org.eclipse.jdt.internal.core.search.matching.ConstructorDeclarationPattern) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) SearchPattern(org.eclipse.jdt.core.search.SearchPattern)

Example 28 with SearchEngine

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

the class JvmImplementationOpener method openImplementations.

/**
 * Main parts of the logic is taken from {@link org.eclipse.jdt.internal.ui.javaeditor.JavaElementImplementationHyperlink}
 *
 * @param element - Element to show implementations for
 * @param textviewer - Viewer to show hierarchy view on
 * @param region - Region where to show hierarchy view
 */
public void openImplementations(final IJavaElement element, ITextViewer textviewer, IRegion region) {
    if (element instanceof IMethod) {
        ITypeRoot typeRoot = ((IMethod) element).getTypeRoot();
        CompilationUnit ast = SharedASTProvider.getAST(typeRoot, SharedASTProvider.WAIT_YES, null);
        if (ast == null) {
            openQuickHierarchy(textviewer, element, region);
            return;
        }
        try {
            ISourceRange nameRange = ((IMethod) element).getNameRange();
            ASTNode node = NodeFinder.perform(ast, nameRange);
            ITypeBinding parentTypeBinding = null;
            if (node instanceof SimpleName) {
                ASTNode parent = node.getParent();
                if (parent instanceof MethodInvocation) {
                    Expression expression = ((MethodInvocation) parent).getExpression();
                    if (expression == null) {
                        parentTypeBinding = Bindings.getBindingOfParentType(node);
                    } else {
                        parentTypeBinding = expression.resolveTypeBinding();
                    }
                } else if (parent instanceof SuperMethodInvocation) {
                    // Directly go to the super method definition
                    openEditor(element);
                    return;
                } else if (parent instanceof MethodDeclaration) {
                    parentTypeBinding = Bindings.getBindingOfParentType(node);
                }
            }
            final IType type = parentTypeBinding != null ? (IType) parentTypeBinding.getJavaElement() : null;
            if (type == null) {
                openQuickHierarchy(textviewer, element, region);
                return;
            }
            final String earlyExitIndicator = "EarlyExitIndicator";
            final ArrayList<IJavaElement> links = Lists.newArrayList();
            IRunnableWithProgress runnable = new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    if (monitor == null) {
                        monitor = new NullProgressMonitor();
                    }
                    try {
                        String methodLabel = JavaElementLabels.getElementLabel(element, JavaElementLabels.DEFAULT_QUALIFIED);
                        monitor.beginTask(Messages.format("Searching for implementors of  ''{0}''", methodLabel), 100);
                        SearchRequestor requestor = new SearchRequestor() {

                            @Override
                            public void acceptSearchMatch(SearchMatch match) throws CoreException {
                                if (match.getAccuracy() == SearchMatch.A_ACCURATE) {
                                    IJavaElement element = (IJavaElement) match.getElement();
                                    if (element instanceof IMethod && !JdtFlags.isAbstract((IMethod) element)) {
                                        links.add(element);
                                        if (links.size() > 1) {
                                            throw new OperationCanceledException(earlyExitIndicator);
                                        }
                                    }
                                }
                            }
                        };
                        int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
                        SearchPattern pattern = SearchPattern.createPattern(element, limitTo);
                        Assert.isNotNull(pattern);
                        SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
                        SearchEngine engine = new SearchEngine();
                        engine.search(pattern, participants, SearchEngine.createHierarchyScope(type), requestor, SubMonitor.convert(monitor, 100));
                        if (monitor.isCanceled()) {
                            throw new InterruptedException();
                        }
                    } catch (OperationCanceledException e) {
                        throw new InterruptedException(e.getMessage());
                    } catch (CoreException e) {
                        throw new InvocationTargetException(e);
                    } finally {
                        monitor.done();
                    }
                }
            };
            try {
                PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
            } catch (InvocationTargetException e) {
                IStatus status = new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, Messages.format("An error occurred while searching for implementations of method ''{0}''. See error log for details.", element.getElementName()), e.getCause());
                JavaPlugin.log(status);
                ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Open Implementation", "Problems finding implementations.", status);
            } catch (InterruptedException e) {
                if (e.getMessage() != earlyExitIndicator) {
                    return;
                }
            }
            if (links.size() == 1) {
                openEditor(links.get(0));
            } else {
                openQuickHierarchy(textviewer, element, region);
            }
        } catch (JavaModelException e) {
            log.error("An error occurred while searching for implementations", e.getCause());
        } catch (PartInitException e) {
            log.error("An error occurred while searching for implementations", e.getCause());
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) JavaModelException(org.eclipse.jdt.core.JavaModelException) SimpleName(org.eclipse.jdt.core.dom.SimpleName) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IType(org.eclipse.jdt.core.IType) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod) PartInitException(org.eclipse.ui.PartInitException) ISourceRange(org.eclipse.jdt.core.ISourceRange) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) InvocationTargetException(java.lang.reflect.InvocationTargetException) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) Expression(org.eclipse.jdt.core.dom.Expression)

Example 29 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project egradle by de-jcup.

the class JDTDataAccess method findType.

/**
 * Find type
 *
 * @param className
 * @param monitor
 * @return type or <code>null</code>
 */
public IType findType(String className, IProgressMonitor monitor) {
    final IType[] result = { null };
    TypeNameMatchRequestor nameMatchRequestor = new TypeNameMatchRequestor() {

        @Override
        public void acceptTypeNameMatch(TypeNameMatch match) {
            result[0] = match.getType();
        }
    };
    int lastDot = className.lastIndexOf('.');
    char[] packageName = lastDot >= 0 ? className.substring(0, lastDot).toCharArray() : null;
    char[] typeName = (lastDot >= 0 ? className.substring(lastDot + 1) : className).toCharArray();
    SearchEngine engine = new SearchEngine();
    int packageMatchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
    try {
        engine.searchAllTypeNames(packageName, packageMatchRule, typeName, packageMatchRule, IJavaSearchConstants.TYPE, SearchEngine.createWorkspaceScope(), nameMatchRequestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
    } catch (JavaModelException e) {
        EditorUtil.INSTANCE.logError("Was not able to search all type names", e);
    }
    return result[0];
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) IType(org.eclipse.jdt.core.IType)

Example 30 with SearchEngine

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

the class ObsoleteSimpleNameRatherThanQualifiedNameCleanUp method importTypesFromPackage.

private void importTypesFromPackage(final String pkgName, final ASTNode visited) {
    TypeNameMatchRequestor importTypeCollector = new TypeNameMatchRequestor() {

        @Override
        public void acceptTypeNameMatch(final TypeNameMatch typeNameMatch) {
            boolean isTopLevelType = typeNameMatch.getType().getDeclaringType() == null;
            if (isTopLevelType) {
                if (!pkgName.equals(typeNameMatch.getPackageName())) {
                    // Sanity check failed
                    throw new IllegalStateException(// $NON-NLS-1$
                    "Expected package '" + typeNameMatch.getPackageName() + "' to be equal to '" + pkgName + // $NON-NLS-1$ //$NON-NLS-2$
                    "'");
                }
                QName qname = QName.valueOf(typeNameMatch.getFullyQualifiedName());
                types.addName(FQN.fromImport(qname, true));
            }
        }
    };
    try {
        SearchEngine searchEngine = new SearchEngine();
        // search in this package
        searchEngine.searchAllTypeNames(// search in this package
        pkgName.toCharArray(), // search in this package
        SearchPattern.R_EXACT_MATCH, // do not filter by type name
        null, // do not filter by type name
        SearchPattern.R_EXACT_MATCH, // look for all java types (class, interfaces, enums, etc.)
        IBinding.TYPE, // search everywhere
        SearchEngine.createWorkspaceScope(), // wait in case the indexer is indexing
        importTypeCollector, // wait in case the indexer is indexing
        IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, cuRewrite.getProgressMonitor());
    } catch (JavaModelException e) {
        throw new UnhandledException(visited, e);
    }
}
Also used : UnhandledException(org.autorefactor.util.UnhandledException) JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor)

Aggregations

SearchEngine (org.eclipse.jdt.core.search.SearchEngine)61 CoreException (org.eclipse.core.runtime.CoreException)31 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)28 JavaModelException (org.eclipse.jdt.core.JavaModelException)27 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)26 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)24 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)22 ArrayList (java.util.ArrayList)21 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)20 IJavaElement (org.eclipse.jdt.core.IJavaElement)14 IType (org.eclipse.jdt.core.IType)14 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)12 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)11 TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)11 IMethod (org.eclipse.jdt.core.IMethod)10 TypeNameMatchRequestor (org.eclipse.jdt.core.search.TypeNameMatchRequestor)9 SearchParticipant (org.eclipse.jdt.core.search.SearchParticipant)7 HashSet (java.util.HashSet)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 CollectingSearchRequestor (org.eclipse.jdt.internal.corext.refactoring.CollectingSearchRequestor)6