Example usage for org.eclipse.jdt.core.search SearchPattern createOrPattern

List of usage examples for org.eclipse.jdt.core.search SearchPattern createOrPattern

Introduction

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

Prototype

public static SearchPattern createOrPattern(SearchPattern leftPattern, SearchPattern rightPattern) 

Source Link

Document

Returns a search pattern that combines the given two patterns into an "or" pattern.

Usage

From source file:de.loskutov.dh.search.SearchHelper.java

License:Open Source License

private static SearchPattern createAnyFieldPattern(IType[] types) {
    if (types.length == 0) {
        return createAnyFieldPattern();
    }/*w  ww .  j  a va 2s  . c  o m*/
    SearchPattern result = null;
    for (IType type : types) {
        SearchPattern searchPattern = SearchPattern.createPattern(type.getFullyQualifiedName() + ".*",
                IJavaSearchConstants.FIELD, IJavaSearchConstants.DECLARATIONS,
                SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE);
        if (result == null) {
            result = searchPattern;
        } else {
            result = SearchPattern.createOrPattern(result, searchPattern);
        }
    }
    return result;
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockJava.java

License:Open Source License

/********************************************************************************/

void handleFindAll(String proj, String file, int start, int end, boolean defs, boolean refs, boolean impls,
        boolean equiv, boolean exact, boolean system, boolean typeof, boolean ronly, boolean wonly,
        IvyXmlWriter xw) throws BedrockException {
    IJavaProject ijp = getJavaProject(proj);
    IPath fp = new Path(file);

    int limit = 0;
    if (defs && refs)
        limit = IJavaSearchConstants.ALL_OCCURRENCES;
    else if (defs)
        limit = IJavaSearchConstants.DECLARATIONS;
    else if (refs)
        limit = IJavaSearchConstants.REFERENCES;
    int flimit = limit;
    if (ronly)//from   w  ww  .ja  v a 2  s.c o  m
        flimit = IJavaSearchConstants.READ_ACCESSES;
    else if (wonly)
        flimit = IJavaSearchConstants.WRITE_ACCESSES;

    int mrule = -1;
    if (equiv)
        mrule = SearchPattern.R_EQUIVALENT_MATCH;
    else if (exact)
        mrule = SearchPattern.R_EXACT_MATCH;

    SearchPattern pat = null;
    IJavaSearchScope scp = null;

    ICompilationUnit icu = our_plugin.getProjectManager().getCompilationUnit(proj, file);
    if (icu == null)
        throw new BedrockException("Compilation unit not found for " + fp);
    icu = getCompilationElement(icu);

    ICompilationUnit[] working = null;
    FindFilter filter = null;

    IJavaElement cls = null;
    char[] packagename = null;
    char[] typename = null;

    try {
        BedrockPlugin.logD("Getting search scopes");
        IJavaElement[] pelt;
        if (ijp != null)
            pelt = new IJavaElement[] { ijp };
        else
            pelt = getAllProjects();
        working = getWorkingElements(pelt);
        int fg = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
        if (system)
            fg |= IJavaSearchScope.SYSTEM_LIBRARIES | IJavaSearchScope.APPLICATION_LIBRARIES;
        scp = SearchEngine.createJavaSearchScope(pelt, fg);

        BedrockPlugin.logD("Locating item to search for");
        IJavaElement[] elts = icu.codeSelect(start, end - start);

        if (typeof) {
            Set<IJavaElement> nelt = new LinkedHashSet<IJavaElement>();
            for (int i = 0; i < elts.length; ++i) {
                IType typ = null;
                String tnm = null;
                switch (elts[i].getElementType()) {
                case IJavaElement.FIELD:
                    tnm = ((IField) elts[i]).getTypeSignature();
                    break;
                case IJavaElement.LOCAL_VARIABLE:
                    tnm = ((ILocalVariable) elts[i]).getTypeSignature();
                    break;
                case IJavaElement.METHOD:
                    typ = ((IMethod) elts[i]).getDeclaringType();
                    break;
                default:
                    nelt.add(elts[i]);
                    break;
                }
                if (typ != null)
                    nelt.add(typ);
                else if (tnm != null && ijp != null) {
                    IJavaElement elt = ijp.findElement(tnm, null);
                    if (elt != null) {
                        nelt.add(elt);
                        typ = null;
                    } else {
                        while (tnm.startsWith("[")) {
                            String xtnm = tnm.substring(1);
                            if (xtnm == null)
                                break;
                            tnm = xtnm;
                        }
                        int ln = tnm.length();
                        String xtnm = tnm;
                        if (tnm.startsWith("L") && tnm.endsWith(";")) {
                            xtnm = tnm.substring(1, ln - 1);
                        } else if (tnm.startsWith("Q") && tnm.endsWith(";")) {
                            xtnm = tnm.substring(1, ln - 1);
                        }
                        if (xtnm != null)
                            tnm = xtnm;
                        int idx1 = tnm.lastIndexOf(".");
                        if (idx1 > 0) {
                            String pkgnm = tnm.substring(0, idx1);
                            xtnm = tnm.substring(idx1 + 1);
                            if (xtnm != null)
                                tnm = xtnm;
                            pkgnm = pkgnm.replace('$', '.');
                            packagename = pkgnm.toCharArray();
                        }
                        tnm = tnm.replace('$', '.');
                        typename = tnm.toCharArray();
                    }

                    if (typename != null) {
                        BedrockPlugin.logD("Handling type names");
                        FindTypeHandler fth = new FindTypeHandler(ijp);
                        SearchEngine se = new SearchEngine(working);
                        se.searchAllTypeNames(packagename, SearchPattern.R_EXACT_MATCH, typename,
                                SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, scp, fth,
                                IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
                        nelt.addAll(fth.getFoundItems());
                    }
                }
            }
            IJavaElement[] nelts = new IJavaElement[nelt.size()];
            elts = nelt.toArray(nelts);
        }

        if (elts.length == 1 && !typeof) {
            xw.begin("SEARCHFOR");
            switch (elts[0].getElementType()) {
            case IJavaElement.FIELD:
                xw.field("TYPE", "Field");
                break;
            case IJavaElement.LOCAL_VARIABLE:
                xw.field("TYPE", "Local");
                break;
            case IJavaElement.METHOD:
                xw.field("TYPE", "Function");
                break;
            case IJavaElement.TYPE:
            case IJavaElement.TYPE_PARAMETER:
                xw.field("TYPE", "Class");
                cls = elts[0];
                break;
            }
            xw.text(elts[0].getElementName());
            xw.end("SEARCHFOR");
        }
        int etyp = -1;
        for (int i = 0; i < elts.length; ++i) {
            SearchPattern sp;
            int xlimit = limit;
            switch (elts[i].getElementType()) {
            case IJavaElement.FIELD:
            case IJavaElement.LOCAL_VARIABLE:
                xlimit = flimit;
                break;
            case IJavaElement.TYPE:
                if (impls)
                    xlimit = IJavaSearchConstants.IMPLEMENTORS;
                break;
            case IJavaElement.METHOD:
                if (impls)
                    xlimit |= IJavaSearchConstants.IGNORE_DECLARING_TYPE;
                break;
            }
            if (mrule < 0)
                sp = SearchPattern.createPattern(elts[i], xlimit);
            else
                sp = SearchPattern.createPattern(elts[i], xlimit, mrule);
            if (pat == null)
                pat = sp;
            else
                pat = SearchPattern.createOrPattern(pat, sp);
            if (etyp < 0)
                etyp = elts[i].getElementType();
        }

        if (etyp == IJavaElement.METHOD) {
            if (impls) {
                if (defs)
                    filter = new ImplementFilter(elts);
            } else if (defs && !refs)
                filter = new ClassFilter(elts);
        }

    } catch (JavaModelException e) {
        BedrockPlugin.logE("SEARCH PROBLEM: " + e);
        e.printStackTrace();
        throw new BedrockException("Can't find anything to search for", e);
    }

    if (pat == null) {
        BedrockPlugin.logW("Nothing to search for");
        return;
    }

    BedrockPlugin.logD("Setting up search");
    SearchEngine se = new SearchEngine(working);
    SearchParticipant[] parts = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    FindHandler fh = new FindHandler(xw, filter, false);

    BedrockPlugin.logD(
            "BEGIN SEARCH " + pat + " " + parts.length + " " + " " + scp + " :: COPIES: " + working.length);

    try {
        se.search(pat, parts, scp, fh, null);
    } catch (Throwable e) {
        throw new BedrockException("Problem doing find all search for " + pat + ": " + e, e);
    }

    if (cls != null && defs) { // need to add the actual class definition
        BedrockUtil.outputJavaElement(cls, false, xw);
    }
}

From source file:org.codehaus.groovy.eclipse.core.search.SyntheticAccessorSearchRequestor.java

License:Apache License

private SearchPattern createPattern(IJavaElement element) throws JavaModelException {
    List<IJavaElement> toSearch = new ArrayList<IJavaElement>(4);
    toSearch.add(findSyntheticMember(element, "is"));
    toSearch.add(findSyntheticMember(element, "get"));
    toSearch.add(findSyntheticMember(element, "set"));
    toSearch.add(findSyntheticProperty(element));
    SearchPattern pattern = null;/*from  ww  w . j a v a2 s.c  o  m*/
    for (IJavaElement searchElt : toSearch) {
        if (searchElt != null) {
            SearchPattern newPattern = SearchPattern.createPattern(searchElt,
                    IJavaSearchConstants.ALL_OCCURRENCES | IJavaSearchConstants.IGNORE_RETURN_TYPE);
            if (pattern == null) {
                pattern = newPattern;
            } else {
                pattern = SearchPattern.createOrPattern(pattern, newPattern);
            }
        }
    }
    return pattern;
}

From source file:org.eclipse.che.plugin.java.testing.JavaTestFinder.java

License:Open Source License

private List<String> findClassesInContainer(IJavaElement container, String testMethodAnnotation,
        String testClassAnnotation) {
    List<String> result = new LinkedList<>();
    IRegion region = getRegion(container);
    try {/*from w ww. jav a 2  s  .c  o m*/
        ITypeHierarchy hierarchy = JavaCore.newTypeHierarchy(region, null, null);
        IType[] allClasses = hierarchy.getAllClasses();

        // search for all types with references to RunWith and Test and all subclasses
        HashSet<IType> candidates = new HashSet<>(allClasses.length);
        SearchRequestor requestor = new AnnotationSearchRequestor(hierarchy, candidates);

        IJavaSearchScope scope = SearchEngine.createJavaSearchScope(allClasses, IJavaSearchScope.SOURCES);
        int matchRule = SearchPattern.R_CASE_SENSITIVE;

        SearchPattern testPattern = SearchPattern.createPattern(testMethodAnnotation,
                IJavaSearchConstants.ANNOTATION_TYPE, IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE,
                matchRule);

        SearchPattern runWithPattern = isNullOrEmpty(testClassAnnotation) ? testPattern
                : SearchPattern.createPattern(testClassAnnotation, IJavaSearchConstants.ANNOTATION_TYPE,
                        IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE, matchRule);

        SearchPattern annotationsPattern = SearchPattern.createOrPattern(runWithPattern, testPattern);
        SearchParticipant[] searchParticipants = new SearchParticipant[] {
                SearchEngine.getDefaultSearchParticipant() };
        new SearchEngine().search(annotationsPattern, searchParticipants, scope, requestor, null);

        // find all classes in the region
        for (IType candidate : candidates) {
            if (isAccessibleClass(candidate) && !Flags.isAbstract(candidate.getFlags())
                    && region.contains(candidate)) {
                result.add(candidate.getFullyQualifiedName());
            }
        }
    } catch (CoreException e) {

        LOG.info("Can't build project hierarchy.", e);
    }

    return result;
}

From source file:org.eclipse.jdt.core.search.SearchEngine.java

License:Open Source License

/**
 * Returns a search pattern that combines the given two patterns into a "or" pattern.
 * The search result will match either the left pattern or the right pattern.
 *
 * @param leftPattern the left pattern/*from ww  w.  j  a  va  2  s .  c  o m*/
 * @param rightPattern the right pattern
 * @return a "or" pattern
 * @deprecated Use {@link SearchPattern#createOrPattern(SearchPattern, SearchPattern)} instead.
 */
public static ISearchPattern createOrSearchPattern(ISearchPattern leftPattern, ISearchPattern rightPattern) {
    SearchPattern left = ((SearchPatternAdapter) leftPattern).pattern;
    SearchPattern right = ((SearchPatternAdapter) rightPattern).pattern;
    SearchPattern pattern = SearchPattern.createOrPattern(left, right);
    return new SearchPatternAdapter(pattern);
}

From source file:org.eclipse.jst.jsf.core.jsfappconfig.AnnotationJSFAppConfigProvider.java

License:Open Source License

private SearchPattern orPattern(SearchPattern pattern, IJavaElement element) {
    if (element == null) {
        return pattern;
    }//from  www  .j a va2  s . c o m
    if (pattern == null) {
        return SearchPattern.createPattern(element, IJavaSearchConstants.REFERENCES);
    }
    return SearchPattern.createOrPattern(pattern,
            SearchPattern.createPattern(element, IJavaSearchConstants.REFERENCES));
}

From source file:org.eclipse.rap.ui.internal.launch.rwt.shortcut.EntryPointSearchEngine.java

License:Open Source License

private void search(IProgressMonitor monitor) throws CoreException {
    monitor.beginTask("Searching for entry points...", 100);
    try {/* w  ww .j av  a 2s .co m*/
        int matchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
        SearchParticipant[] participants = getSearchParticipants();
        IProgressMonitor searchMonitor = new SubProgressMonitor(monitor, 100);
        SearchEngine searchEngine = new SearchEngine();
        SearchPattern pattern1 = SearchPattern.createPattern("createUI() int", //$NON-NLS-1$
                IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, matchRule);
        SearchPattern pattern2 = SearchPattern.createPattern("createContents( Composite ) void", //$NON-NLS-1$
                IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, matchRule);
        SearchPattern pattern = SearchPattern.createOrPattern(pattern1, pattern2);
        searchEngine.search(pattern, participants, searchScope, entryPointCollector, searchMonitor);
    } finally {
        monitor.done();
    }
}

From source file:org.grails.ide.eclipse.core.junit.Grails20AwareTestFinder.java

License:Open Source License

private void searchForTests(IJavaElement element, final Set result, IProgressMonitor pm) throws CoreException {
    //Loosely based on a copy of org.eclipse.jdt.internal.junit.launcher.JUnit4TestFinder.findTestsInContainer(IJavaElement, Set, IProgressMonitor)
    //Modifed to search just for Grails test classes marked by @TestFor annotations
    try {//w  w w.j  a v  a  2 s.  c om
        pm.beginTask(JUnitMessages.JUnit4TestFinder_searching_description, 4);

        IRegion region = CoreTestSearchEngine.getRegion(element);

        IJavaSearchScope scope = SearchEngine.createJavaSearchScope(region.getElements(),
                IJavaSearchScope.SOURCES);
        int matchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
        SearchPattern testForPattern = SearchPattern.createPattern(TEST_FOR_ANNOT_NAME,
                IJavaSearchConstants.ANNOTATION_TYPE, IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE,
                matchRule);
        SearchPattern testPattern = SearchPattern.createPattern("*Tests", IJavaSearchConstants.TYPE,
                IJavaSearchConstants.DECLARATIONS, matchRule);

        SearchPattern theSearchPattern = SearchPattern.createOrPattern(testForPattern, testPattern);
        SearchParticipant[] searchParticipants = new SearchParticipant[] {
                SearchEngine.getDefaultSearchParticipant() };

        SearchRequestor requestor = new SearchRequestor() {
            @Override
            public void acceptSearchMatch(SearchMatch match) throws CoreException {
                if (match.getAccuracy() == SearchMatch.A_ACCURATE && !match.isInsideDocComment()) {
                    Object element = match.getElement();
                    if (element instanceof IType && isGrail20Test((IType) element)) {
                        result.add(element);
                    }
                }
            }
        };
        new SearchEngine().search(theSearchPattern, searchParticipants, scope, requestor,
                new SubProgressMonitor(pm, 2));
    } finally {
        pm.done();
    }
}

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.search.JavaElementsSearcher.java

License:Open Source License

/**
 * Search for methods annotated with one of the given annotations, in the search scope.
 * //  w ww  .ja v  a2  s.  c  om
 * @param annotationNames
 *            the annotations fully qualified names
 * @param searchScope
 *            the search scope
 * @param progressMonitor
 *            the progress monitor
 * @return the matching methods
 * @throws CoreException
 *             in case of underlying exception
 */
private static Set<IMethod> searchForAnnotatedMethods(final List<String> annotationNames,
        final IJavaSearchScope searchScope, final IProgressMonitor progressMonitor) throws CoreException {
    final JavaMemberSearchResultCollector collector = new JavaMemberSearchResultCollector(IJavaElement.METHOD,
            searchScope);
    SearchPattern pattern = null;
    for (String annotationName : annotationNames) {
        // TODO : apply on METHOD instead of TYPE ?
        SearchPattern subPattern = SearchPattern.createPattern(annotationName,
                IJavaSearchConstants.ANNOTATION_TYPE, IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE,
                SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
        if (pattern == null) {
            pattern = subPattern;
        } else {
            pattern = SearchPattern.createOrPattern(pattern, subPattern);
        }
    }
    // perform search, results are added/filtered by the custom
    // searchRequestor defined above
    new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
            searchScope, collector, progressMonitor);
    return collector.getResult();
}

From source file:org.jboss.tools.ws.jaxrs.core.internal.metamodel.search.JavaElementsSearcher.java

License:Open Source License

/**
 * Search for fields annotated with one of the given annotations, in the search scope.
 * /*w ww.  ja  v a  2 s .c o m*/
 * @param annotationNames
 *            the annotations fully qualified names
 * @param searchScope
 *            the search scope
 * @param progressMonitor
 *            the progress monitor
 * @return the matching fields
 * @throws CoreException
 *             in case of underlying exception
 */
private static Set<IField> searchForAnnotatedFields(final List<String> annotationNames,
        final IJavaSearchScope searchScope, final IProgressMonitor progressMonitor) throws CoreException {
    final JavaMemberSearchResultCollector collector = new JavaMemberSearchResultCollector(IJavaElement.FIELD,
            searchScope);
    SearchPattern pattern = null;
    for (String annotationName : annotationNames) {
        // TODO : apply on METHOD instead of TYPE ?
        SearchPattern subPattern = SearchPattern.createPattern(annotationName,
                IJavaSearchConstants.ANNOTATION_TYPE, IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE,
                SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
        if (pattern == null) {
            pattern = subPattern;
        } else {
            pattern = SearchPattern.createOrPattern(pattern, subPattern);
        }
    }
    // perform search, results are added/filtered by the custom
    // searchRequestor defined above
    new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
            searchScope, collector, progressMonitor);
    return collector.getResult();
}