Example usage for org.eclipse.jdt.core.search SearchEngine search

List of usage examples for org.eclipse.jdt.core.search SearchEngine search

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.search SearchEngine search.

Prototype

public void search(SearchPattern pattern, SearchParticipant[] participants, IJavaSearchScope scope,
        SearchRequestor requestor, IProgressMonitor monitor) throws CoreException 

Source Link

Document

Searches for matches of a given search pattern.

Usage

From source file:org.talend.designer.runtime.visualization.internal.actions.OpenDeclarationAction.java

License:Open Source License

/**
 * Searches the source for the given class name.
 * /*from w w  w  . java  2 s.c  o  m*/
 * @param name The class name
 * @return The source
 * @throws CoreException
 */
IType doSearchSource(String name) throws CoreException {
    final List<IType> results = new ArrayList<IType>();

    // create requester
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object element = match.getElement();
            if (element instanceof IType) {
                results.add((IType) element);
            }
        }
    };

    String baseName = name.replace('$', '.');

    // create search engine and pattern
    SearchEngine engine = new SearchEngine();
    SearchPattern pattern = SearchPattern.createPattern(baseName, IJavaSearchConstants.TYPE,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);

    // search the source for the given name
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
            SearchEngine.createWorkspaceScope(), requestor, null);

    if (results.size() > 0) {
        // at most one source should be found
        return results.get(0);
    }

    return null;
}

From source file:org.testng.eclipse.ui.OpenEditorAtLineAction.java

License:Open Source License

protected IJavaElement findElement(IJavaProject project, String className) throws CoreException {
    IJavaElement element = project.findType(className);

    //fix for bug 37333
    if (element == null) {
        SearchPattern pattern = SearchPattern.createPattern(className, IJavaSearchConstants.TYPE,
                IJavaSearchConstants.DECLARATIONS,
                SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_ERASURE_MATCH);
        IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { project }, false);
        NonPublicClassInCUCollector requestor = new NonPublicClassInCUCollector();

        SearchEngine searchEngine = new SearchEngine();
        searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
                scope, requestor, new NullProgressMonitor());

        element = requestor.fFound;/*  w w w.  j a  v  a 2 s  .  com*/
    }

    return element;
}

From source file:potes.cucumberjvm.eclipseplugin.CucumberLanguage.java

License:Apache License

void checkProject(SearchEngine searchEngine, IJavaProject project, IJavaElement element)
        throws JavaModelException, CoreException {
    IType givenType = project.findType("cucumber.annotation.en.Given");
    if (givenType != null) {
        SearchPattern pattern = SearchPattern.createPattern("cucumber.annotation.*.*",
                IJavaSearchConstants.TYPE, IJavaSearchConstants.IMPORT_DECLARATION_TYPE_REFERENCE,
                SearchPattern.R_PATTERN_MATCH);

        IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(
                element == null ? project.getChildren() : new IJavaElement[] { element }, element == null);
        SearchParticipant[] participants = new SearchParticipant[] {
                SearchEngine.getDefaultSearchParticipant() };
        AnnotationFinder annotationFinder = new AnnotationFinder(project, searchEngine, searchScope,
                participants);/*w  w w.  j a v a 2  s .  c o  m*/
        searchEngine.search(pattern, participants, searchScope, annotationFinder, null);

        searchNonLanguageAnnotation(searchEngine, project, searchScope, participants, "Before");
        searchNonLanguageAnnotation(searchEngine, project, searchScope, participants, "After");
        searchNonLanguageAnnotation(searchEngine, project, searchScope, participants, "Order");
    }
}

From source file:potes.cucumberjvm.eclipseplugin.CucumberLanguage.java

License:Apache License

private void searchNonLanguageAnnotation(SearchEngine searchEngine, IJavaProject project,
        IJavaSearchScope searchScope, SearchParticipant[] participants, String name)
        throws JavaModelException, CoreException {
    SearchPattern pattern;//  w w w .j a  v a  2 s  . c om
    IType type = project.findType("cucumber.annotation." + name);
    pattern = SearchPattern.createPattern(type, IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE);
    searchEngine.search(pattern, participants, searchScope, requestor, null);
}

From source file:qwickie.util.DocumentHelper.java

License:Apache License

/**
 * marks all occurrences of the given string
 */// w  w  w .j  a  v  a2s . c o  m
public static void markOccurrence(final ITextEditor textEditor, final String string) {
    if (string == null) {
        return;
    }
    SearchPattern pattern = SearchPattern.createPattern(string, IJavaSearchConstants.FIELD,
            IJavaSearchConstants.ALL_OCCURRENCES, SearchPattern.R_EXACT_MATCH);

    SearchRequestor requestor = new SearchRequestor() {
        @Override
        public void acceptSearchMatch(final SearchMatch match) {
            IAnnotationModel model = textEditor.getDocumentProvider()
                    .getAnnotationModel(textEditor.getEditorInput());
            Annotation annotation = new Annotation("org.eclipse.jdt.ui.occurrences", false,
                    "wicket id constant");
            model.addAnnotation(annotation, new Position(match.getOffset(), match.getLength()));
        }
    };

    SearchEngine searchEngine = new SearchEngine();
    try {
        searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
                SearchEngine.createWorkspaceScope(), requestor, new NullProgressMonitor());
    } catch (CoreException e) {
    }
}

From source file:rs.snpe.android.refactoring.core.AndroidPackageRenameParticipant.java

License:Open Source License

@Override
protected boolean initialize(final Object element) {
    isPackage = false;//from ww  w.  j av  a2  s  .c o  m
    try {
        if (element instanceof IPackageFragment) {
            packageFragment = (IPackageFragment) element;
            if (!packageFragment.containsJavaResources())
                return false;
            IJavaProject javaProject = (IJavaProject) packageFragment.getAncestor(IJavaElement.JAVA_PROJECT);
            IProject project = javaProject.getProject();
            IResource manifestResource = project
                    .findMember(AndroidConstants.WS_SEP + AndroidConstants.FN_ANDROID_MANIFEST);

            if (manifestResource == null || !manifestResource.exists()
                    || !(manifestResource instanceof IFile)) {
                Activator.logInfo("Invalid or missing the " + AndroidConstants.FN_ANDROID_MANIFEST + " in the "
                        + project.getName() + " project.");
                return false;
            }
            androidManifest = (IFile) manifestResource;
            String packageName = packageFragment.getElementName();
            AndroidManifestParser parser;
            try {
                parser = AndroidManifestParser.parseForData(androidManifest);
            } catch (CoreException e) {
                Activator.logInfo("Invalid or missing the " + AndroidConstants.FN_ANDROID_MANIFEST + " in the "
                        + project.getName() + " project.");
                return false;
            }
            javaPackage = parser.getPackage();
            oldName = packageName;
            newName = getArguments().getNewName();
            if (oldName == null || newName == null) {
                return false;
            }

            if (javaPackage != null && javaPackage.equals(packageName)) {
                isPackage = true;
            }
            androidElements = addAndroidElements();
            try {
                final IType type = javaProject.findType(AndroidConstants.CLASS_VIEW);
                SearchPattern pattern = SearchPattern.createPattern("*", IJavaSearchConstants.TYPE,
                        IJavaSearchConstants.DECLARATIONS, SearchPattern.R_REGEXP_MATCH);
                IJavaSearchScope scope = SearchEngine
                        .createJavaSearchScope(new IJavaElement[] { packageFragment });
                final HashSet<IType> elements = new HashSet<IType>();
                SearchRequestor requestor = new SearchRequestor() {
                    public void acceptSearchMatch(SearchMatch match) throws CoreException {
                        Object elem = match.getElement();
                        if (elem instanceof IType) {
                            IType eType = (IType) elem;
                            IType[] superTypes = JavaModelUtil.getAllSuperTypes(eType,
                                    new NullProgressMonitor());
                            for (int i = 0; i < superTypes.length; i++) {
                                if (superTypes[i].equals(type)) {
                                    elements.add(eType);
                                    break;
                                }
                            }
                        }

                    }
                };
                SearchEngine searchEngine = new SearchEngine();
                searchEngine.search(pattern,
                        new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope,
                        requestor, null);
                List<String> views = new ArrayList<String>();
                for (IType elem : elements) {
                    views.add(elem.getFullyQualifiedName());
                }
                if (views.size() > 0) {
                    String[] classNames = views.toArray(new String[0]);
                    addLayoutChanges(project, classNames);
                }
            } catch (CoreException e) {
                Activator.log(e);
            }

            return isPackage || androidElements.size() > 0 || fileChanges.size() > 0;
        }
    } catch (JavaModelException ignore) {
    }
    return false;
}

From source file:tod.plugin.TODPluginUtils.java

License:Open Source License

/**
 * Searches for declarations of the given name and kind in the whole workspace. 
 *//*  www  . j a  v  a  2s.com*/
public static List searchDeclarations(List<IJavaProject> aJavaProject, String aName, int aKind)
        throws CoreException {
    // Recursively add referenced projects (because Eclipse doesn't recursively include non-exported referenced projects) 
    Set<IJavaProject> theProjects = new HashSet<IJavaProject>();
    Stack<IJavaProject> theWorkingList = new ArrayStack<IJavaProject>();
    for (IJavaProject theProject : aJavaProject)
        theWorkingList.push(theProject);

    while (!theWorkingList.isEmpty()) {
        IJavaProject theProject = theWorkingList.pop();
        theProjects.add(theProject);

        IJavaModel theJavaModel = theProject.getJavaModel();
        IClasspathEntry[] theEntries = theProject.getResolvedClasspath(true);
        for (IClasspathEntry theEntry : theEntries)
            if (theEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IJavaProject theReferencedProject = theJavaModel
                        .getJavaProject(theEntry.getPath().lastSegment());
                if (theReferencedProject.exists() && !theProjects.contains(theReferencedProject))
                    theWorkingList.push(theReferencedProject);
            }
    }

    SearchPattern thePattern = SearchPattern.createPattern(aName.replace('$', '.'), aKind,
            IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);

    IJavaSearchScope theScope = SearchEngine
            .createJavaSearchScope(theProjects.toArray(new IJavaElement[theProjects.size()]), true);

    SearchEngine theSearchEngine = new SearchEngine();
    SimpleResultCollector theCollector = new SimpleResultCollector();

    theSearchEngine.search(thePattern, PARTICIPANTS, theScope, theCollector, null);

    return theCollector.getResults();
}