Search in sources :

Example 81 with SearchEngine

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

the class JavaSearchScopePackageLister method getPackages.

@Override
public String[] getPackages(boolean includeNonSource, IPackageFilter filter) throws PackageListException {
    final List<IJavaElement> packageList = new LinkedList<IJavaElement>();
    final SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            IJavaElement enclosingElement = (IJavaElement) match.getElement();
            String name = enclosingElement.getElementName();
            if (name.length() > 0) {
                // Do not include default pkg
                packageList.add(enclosingElement);
            }
        }
    };
    final SearchPattern pattern = SearchPattern.createPattern("*", IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE);
    IRunnableWithProgress operation = new IRunnableWithProgress() {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, monitor);
            } catch (CoreException e) {
                throw new InvocationTargetException(e);
            }
        }
    };
    try {
        runContext.run(true, true, operation);
    } catch (InvocationTargetException e) {
        throw new PackageListException(e.getCause());
    } catch (InterruptedException e) {
        throw new PackageListException("Operation interrupted");
    }
    // Remove non-source and excludes
    Set<String> packageNames = new LinkedHashSet<String>();
    for (Iterator<IJavaElement> iter = packageList.iterator(); iter.hasNext(); ) {
        boolean omit = false;
        IJavaElement element = iter.next();
        if (!includeNonSource) {
            IPackageFragment pkgFragment = (IPackageFragment) element;
            try {
                if (pkgFragment.getCompilationUnits().length == 0) {
                    omit = true;
                }
            } catch (JavaModelException e) {
                throw new PackageListException(e);
            }
        }
        if (filter != null && !filter.select(element.getElementName())) {
            omit = true;
        }
        if (!omit) {
            packageNames.add(element.getElementName());
        }
    }
    return packageNames.toArray(new String[0]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) LinkedList(java.util.LinkedList) InvocationTargetException(java.lang.reflect.InvocationTargetException) 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) SearchPattern(org.eclipse.jdt.core.search.SearchPattern)

Example 82 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project sts4 by spring-projects.

the class OpenFullyQualifiedNameInEditor method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    String fqName = event.getParameter(FQ_NAME);
    String projectName = event.getParameter(PROJECT_NAME);
    String packageName = "";
    String typeName = fqName;
    int idx = fqName.lastIndexOf('.');
    if (idx >= 0) {
        packageName = fqName.substring(0, idx);
        typeName = idx < fqName.length() - 1 ? fqName.substring(idx + 1) : "";
    }
    if (!typeName.isEmpty()) {
        SearchEngine engine = new SearchEngine((WorkingCopyOwner) null);
        List<TypeNameMatch> matches = new ArrayList<>();
        String searchedTypeName = getSearchedTypeName(typeName);
        final boolean isInnerType = searchedTypeName != typeName;
        TypeNameMatchRequestor requestor = new TypeNameMatchRequestor() {

            @Override
            public void acceptTypeNameMatch(TypeNameMatch match) {
                if (isInnerType) {
                    if (match.getFullyQualifiedName().equals(fqName.replace("$", "."))) {
                        matches.add(match);
                    }
                } else {
                    matches.add(match);
                }
            }
        };
        try {
            IJavaSearchScope searchScope = createSearchScope(projectName);
            engine.searchAllTypeNames(packageName.toCharArray(), SearchPattern.R_EXACT_MATCH, searchedTypeName.toCharArray(), SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, searchScope, requestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor());
            if (matches.isEmpty()) {
                BootLanguageServerPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, BootLanguageServerPlugin.ID, "Cannot find type: " + fqName));
            } else {
                if (matches.size() > 1) {
                    BootLanguageServerPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, BootLanguageServerPlugin.ID, "More than one type is defined for: " + fqName));
                }
                try {
                    IType type = matches.get(0).getType();
                    IEditorPart editorPart = JavaUI.openInEditor(type);
                    JavaUI.revealInEditor(editorPart, (IJavaElement) type);
                } catch (JavaModelException ex) {
                    // $NON-NLS-1$
                    throw new ExecutionException("Error opening java element in editor", ex);
                } catch (PartInitException ex) {
                    // $NON-NLS-1$
                    throw new ExecutionException("Error opening java element in editor", ex);
                }
            }
        } catch (JavaModelException e) {
            BootLanguageServerPlugin.getDefault().getLog().log(e.getStatus());
        }
    }
    return null;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) ArrayList(java.util.ArrayList) IEditorPart(org.eclipse.ui.IEditorPart) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) IType(org.eclipse.jdt.core.IType) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) PartInitException(org.eclipse.ui.PartInitException) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 83 with SearchEngine

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

the class JavaTypeFinder method findProposals.

/**
 * @param resource
 * @param replacementStart
 * @param replacementLength
 * @param searchFor IJavaSearchConstants.TYPE, IJavaSearchConstants.CLASS
 * @return
 */
private static ICompletionProposal[] findProposals(IResource resource, int replacementStart, int replacementLength, int searchFor, boolean ignoreAbstractClasses) {
    JavaTypeNameRequestor requestor = new JavaTypeNameRequestor();
    requestor.setJSPOffset(replacementStart);
    requestor.setReplacementLength(replacementLength);
    requestor.setIgnoreAbstractClasses(ignoreAbstractClasses);
    try {
        IJavaElement[] elements = new IJavaElement[] { getJavaProject(resource) };
        IJavaSearchScope scope = SearchEngine.createJavaSearchScope(elements);
        new SearchEngine().searchAllTypeNames(null, null, SearchPattern.R_PATTERN_MATCH | SearchPattern.R_PREFIX_MATCH, searchFor, scope, requestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
    } catch (CoreException exc) {
        Logger.logException(exc);
    } catch (Exception exc) {
        // JavaModel
        Logger.logException(exc);
    }
    return requestor.getProposals();
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) CoreException(org.eclipse.core.runtime.CoreException)

Example 84 with SearchEngine

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

the class PortletURLHyperlinkDetector method _findPortletMethods.

private IMethod[] _findPortletMethods(IDocument document, String nameValue) {
    IMethod[] retval = null;
    IFile file = DOMUtils.getFile(document);
    if ((file != null) && file.exists()) {
        IJavaProject project = JavaCore.create(file.getProject());
        if ((project != null) && project.exists()) {
            try {
                IType portlet = project.findType("javax.portlet.Portlet");
                if (portlet != null) {
                    List<IMethod> methods = new ArrayList<>();
                    SearchRequestor requestor = new ActionMethodCollector(methods);
                    IJavaSearchScope scope = SearchEngine.createStrictHierarchyScope(project, portlet, true, false, null);
                    SearchPattern search = SearchPattern.createPattern(nameValue, IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
                    SearchParticipant[] searchParticipant = { SearchEngine.getDefaultSearchParticipant() };
                    new SearchEngine().search(search, searchParticipant, scope, requestor, new NullProgressMonitor());
                    retval = methods.toArray(new IMethod[0]);
                }
            } catch (JavaModelException jme) {
            } catch (CoreException ce) {
            }
        }
    }
    return retval;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) IType(org.eclipse.jdt.core.IType) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IJavaProject(org.eclipse.jdt.core.IJavaProject) 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) IMethod(org.eclipse.jdt.core.IMethod)

Example 85 with SearchEngine

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

the class ServiceMethodHyperlinkDetector method _getServiceWrapperMethod.

private IMethodWrapper _getServiceWrapperMethod(IMethod method) {
    IMethodWrapper retval = null;
    try {
        IJavaElement methodClass = method.getParent();
        IType methodClassType = method.getDeclaringType();
        String methodClassName = methodClass.getElementName();
        if (methodClassName.endsWith("Util") && JdtFlags.isPublic(method) && JdtFlags.isStatic(method)) {
            String packageName = methodClassType.getPackageFragment().getElementName();
            String baseServiceName = methodClassName.substring(0, methodClassName.length() - 4);
            /*
				 * as per liferay standard wrapper type will be in service package with Wrapper suffix
				 * e.g com.example.service.FooUtil.getBar() --> com.example.service.FooWrapper.getBar()
				 */
            String fullyQualifiedName = packageName + "." + baseServiceName + "Wrapper";
            IType wrapperType = _findType(methodClass, fullyQualifiedName);
            if (wrapperType != null) {
                IMethod[] wrapperBaseMethods = wrapperType.findMethods(method);
                if (ListUtil.isNotEmpty(wrapperBaseMethods)) {
                    // look for classes that implement this wrapper
                    List<IMethod> overrides = new ArrayList<>();
                    SearchRequestor requestor = new WrapperMethodCollector(overrides, method);
                    IJavaSearchScope scope = SearchEngine.createStrictHierarchyScope(null, wrapperType, true, false, null);
                    SearchPattern search = SearchPattern.createPattern(method.getElementName(), IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
                    new SearchEngine().search(search, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, new NullProgressMonitor());
                    if (overrides.size() > 1) {
                        retval = new IMethodWrapper(wrapperBaseMethods[0], true);
                    } else if (overrides.size() == 1) {
                        retval = new IMethodWrapper(overrides.get(0), false);
                    }
                }
            }
        }
    } catch (Exception e) {
    }
    return retval;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ArrayList(java.util.ArrayList) JavaModelException(org.eclipse.jdt.core.JavaModelException) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) IType(org.eclipse.jdt.core.IType) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) 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)

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