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

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

Introduction

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

Prototype

public static SearchPattern createPattern(IJavaElement element, int limitTo, int matchRule) 

Source Link

Document

Returns a search pattern based on a given Java element.

Usage

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

License:Open Source License

/**
 * Blocks current thread until search is done
 */// ww  w. j  a  v  a 2 s . com
public List<IJavaElement> getReferences(IProgressMonitor monitor) throws CoreException {

    pattern = SearchPattern.createPattern(searchRoot.getJavaElement(),
            IJavaSearchConstants.REFERENCES | ~IJavaSearchConstants.IMPORT_DECLARATION_TYPE_REFERENCE,
            // | IJavaSearchConstants.IGNORE_DECLARING_TYPE,
            SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_ERASURE_MATCH);

    if (pattern == null) {
        // this is packages???
        // System.out.println("Search patter is null for: " + elt.getElementName());
        return new ArrayList<IJavaElement>();
    }

    scope = conf.createScope(searchRoot.getJavaElement());
    JavaReferencesRequestor searchRequestor = new JavaReferencesRequestor();

    return search(searchRequestor, monitor);
}

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

License:Open Source License

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

void rename(String proj, String bid, String file, int start, int end, String name, String handle,
        String newname, boolean keeporig, boolean getters, boolean setters, boolean dohier, boolean qual,
        boolean refs, boolean dosimilar, boolean textocc, boolean doedit, String filespat, IvyXmlWriter xw)
        throws BedrockException {
    ICompilationUnit icu = our_plugin.getCompilationUnit(proj, file);

    IJavaElement[] elts;//w w w.  j  a v a  2 s.  com
    try {
        elts = icu.codeSelect(start, end - start);
    } catch (JavaModelException e) {
        throw new BedrockException("Bad location: " + e, e);
    }

    IJavaElement relt = null;
    for (IJavaElement ije : elts) {
        if (handle != null && !handle.equals(ije.getHandleIdentifier()))
            continue;
        if (name != null && !name.equals(ije.getElementName()))
            continue;
        relt = ije;
        break;
    }
    if (relt == null)
        throw new BedrockException("Item to rename not found");

    BedrockPlugin.logD("RENAME CHECK " + relt.getElementType() + " " + relt.getParent().getElementType());

    switch (relt.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        throw new BedrockException("Compilation unit renaming not supported yet");
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT:
        throw new BedrockException("Package renaming not supported yet");
    case IJavaElement.FIELD:
    case IJavaElement.LOCAL_VARIABLE:
    case IJavaElement.TYPE_PARAMETER:
        break;
    case IJavaElement.METHOD:
        IMethod mthd = (IMethod) relt;
        try {
            if (mthd.isConstructor())
                throw new BedrockException("Constructor renaming not supported yet");
        } catch (JavaModelException e) {
        }
        break;
    case IJavaElement.TYPE:
        IJavaElement pelt = relt.getParent();
        if (pelt.getElementType() == IJavaElement.COMPILATION_UNIT) {
            ITypeRoot xcu = (ITypeRoot) pelt;
            if (relt == xcu.findPrimaryType()) {
                throw new BedrockException("Compilation unit renaming based on type not supported yet");
            }
        }
        break;
    default:
        throw new BedrockException("Invalid element type to rename");
    }

    SearchPattern sp = SearchPattern.createPattern(relt, IJavaSearchConstants.ALL_OCCURRENCES,
            SearchPattern.R_EXACT_MATCH);

    List<ICompilationUnit> worku = new ArrayList<ICompilationUnit>();
    for (IJavaElement je : BedrockJava.getAllProjects()) {
        our_plugin.getWorkingElements(je, worku);
    }
    ICompilationUnit[] work = new ICompilationUnit[worku.size()];
    work = worku.toArray(work);

    int fg = IJavaSearchScope.SOURCES | IJavaSearchScope.REFERENCED_PROJECTS;
    IJavaSearchScope scp = SearchEngine.createJavaSearchScope(work, fg);

    SearchEngine se = new SearchEngine(work);
    SearchParticipant[] parts = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    FindHandler fh = new FindHandler(xw, null);

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

    BedrockPlugin.logD("RENAME RESULT = " + xw.toString());
}

From source file:nz.ac.massey.cs.care.refactoring.executers.IntroduceFactoryRefactoring.java

License:Open Source License

/**
 * @param ctor//  www  .j  a v  a  2s . com
 * @param methodBinding
 * @return a <code>SearchPattern</code> that finds all calls to the constructor
 * identified by the argument <code>methodBinding</code>.
 */
private SearchPattern createSearchPattern(IMethod ctor, IMethodBinding methodBinding) {
    Assert.isNotNull(methodBinding, RefactoringCoreMessages.IntroduceFactory_noBindingForSelectedConstructor);

    if (ctor != null)
        return SearchPattern.createPattern(ctor, IJavaSearchConstants.REFERENCES,
                SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
    else { // perhaps a synthetic method? (but apparently not always... hmmm...)
        // Can't find an IMethod for this method, so build a string pattern instead
        StringBuffer buf = new StringBuffer();

        buf.append(methodBinding.getDeclaringClass().getQualifiedName()).append("(");//$NON-NLS-1$
        for (int i = 0; i < fArgTypes.length; i++) {
            if (i != 0)
                buf.append(","); //$NON-NLS-1$
            buf.append(fArgTypes[i].getQualifiedName());
        }
        buf.append(")"); //$NON-NLS-1$
        return SearchPattern.createPattern(buf.toString(), IJavaSearchConstants.CONSTRUCTOR,
                IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
    }
}

From source file:org.eclipse.ajdt.internal.ui.refactoring.RippleMethodFinder2.java

License:Open Source License

private void findAllDeclarations(IProgressMonitor monitor, WorkingCopyOwner owner) throws CoreException {
    fDeclarations = new ArrayList();

    class MethodRequestor extends SearchRequestor {
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            IMethod method = (IMethod) match.getElement();
            boolean isBinary = method.isBinary();
            if (fBinaryRefs != null || !(fExcludeBinaries && isBinary)) {
                fDeclarations.add(method);
            }/*from  w  w w .  j a v a  2  s. c o  m*/
            if (isBinary && fBinaryRefs != null) {
                fDeclarationToMatch.put(method, match);
            }
        }
    }

    int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE
            | IJavaSearchConstants.IGNORE_RETURN_TYPE;
    int matchRule = SearchPattern.R_ERASURE_MATCH | SearchPattern.R_CASE_SENSITIVE;
    SearchPattern pattern = SearchPattern.createPattern(fMethod, limitTo, matchRule);
    SearchParticipant[] participants = SearchUtils.getDefaultSearchParticipants();
    IJavaSearchScope scope = RefactoringScopeFactory.createRelatedProjectsScope(fMethod.getJavaProject(),
            IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES
                    | IJavaSearchScope.SYSTEM_LIBRARIES);
    MethodRequestor requestor = new MethodRequestor();
    SearchEngine searchEngine = owner != null ? new SearchEngine(owner) : new SearchEngine();

    searchEngine.search(pattern, participants, scope, requestor, monitor);
}

From source file:org.eclipse.jst.jsf.taglibprocessing.attributevalues.JavaClassType.java

License:Open Source License

private List getTypes() {
    IJavaProject jp = getJavaProject();//from   w ww. ja v a 2s. com
    if (jp == null)
        return null;

    List elems = new ArrayList();
    elems.addAll(getInterfaces(jp));
    IType sc = getSuperClass(jp);
    if (sc != null)
        elems.add(sc);

    if (elems.size() > 0) {
        SearchRequestor requestor = new Searcher();
        SearchEngine engine = new SearchEngine();

        IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { jp });//, IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.);
        SearchPattern combined = SearchPattern.createPattern((IJavaElement) elems.get(0),
                IJavaSearchConstants.IMPLEMENTORS, 0);

        //          Until this bug is fixed, stub it out...  only the first interface/superclass will be used.
        //                     https://bugs.eclipse.org/bugs/show_bug.cgi?id=142044
        //                     for(int i=1;i<elems.size();i++){
        //                        final SearchPattern other = SearchPattern.createPattern((IJavaElement)elems.get(i), IJavaSearchConstants.IMPLEMENTORS, 0);
        //                        combined = SearchPattern.createAndPattern(combined, other);
        //                     }

        try {
            engine.search(combined, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
                    scope, requestor, null);

        } catch (CoreException e) {
            //ignore
        }

        return ((Searcher) requestor).getResults();
    }

    return Collections.EMPTY_LIST;
}

From source file:org.eclipse.objectteams.otdt.tests.AbstractJavaModelTests.java

License:Open Source License

protected void search(IJavaElement element, int limitTo, int matchRule, IJavaSearchScope scope,
        SearchRequestor requestor) throws CoreException {
    SearchPattern pattern = SearchPattern.createPattern(element, limitTo, matchRule);
    assertNotNull("Pattern should not be null", pattern);
    new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
            scope, requestor, null);// ww w  .  ja  v a2s.  com
}

From source file:org.grails.ide.eclipse.editor.gsp.search.SearchInGSPs.java

License:Open Source License

private ITypeRequestor createRequestor(IGSPSearchRequestor gspRequestor, GroovyCompilationUnit unit,
        SearchRequestor requestor) {//ww  w. j  a va 2 s .  c om
    // don't know what the match rule should be, so make it exact match
    SearchPattern pattern = SearchPattern.createPattern(gspRequestor.elementToSearchFor(),
            gspRequestor.limitTo(), SearchPattern.R_CASE_SENSITIVE);
    TypeRequestorFactory factory = new TypeRequestorFactory();
    return factory.createRequestor(new MockPossibleMatch(unit), pattern, requestor);
}

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

License:Open Source License

/**
 * Returns all related {@link IType} from the given {@code otherTypes} argument that reference the given {@code sourceType} and the other way around. The result excludes the given {@code sourceType} itself. 
 * @param sourceType the type that is referenced
 * @param otherTypes the other types that may be related to the given 'sourceType'
 * @param progressMonitor the progress monitor
 * @return the set of related types/* w w w. ja  va  2s  . c  om*/
 * @throws CoreException 
 */
public static Set<IType> findRelatedTypes(final IType sourceType, final List<IType> otherTypes,
        final IProgressMonitor progressMonitor) throws CoreException {
    if (sourceType == null || otherTypes == null || otherTypes.isEmpty()) {
        return Collections.emptySet();
    }
    // search #1: occurrences of given 'sourceType' in given 'otherTypes'
    final IJavaSearchScope firstSearchScope = createSearchScope(otherTypes);
    final JavaMemberSearchResultCollector firstCollector = new JavaMemberSearchResultCollector(
            IJavaElement.TYPE, firstSearchScope);
    final SearchPattern firstPattern = SearchPattern.createPattern(sourceType,
            IJavaSearchConstants.ALL_OCCURRENCES, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    new SearchEngine().search(firstPattern,
            new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, firstSearchScope,
            firstCollector, progressMonitor);
    final Set<IType> globalResult = new HashSet<IType>();
    final Set<IType> firstSearchResult = firstCollector.getResult();
    globalResult.addAll(firstSearchResult);
    // set of searches #2: references of given 'otherTypes' in 'sourceType' (one search per given entry in 'types')
    final IJavaSearchScope secondSearchScope = createSearchScope(sourceType);
    for (IType otherType : otherTypes) {
        if (otherType.equals(sourceType)) {
            continue;
        }
        final SearchPattern otherPattern = SearchPattern.createPattern(otherType,
                IJavaSearchConstants.ALL_OCCURRENCES,
                SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
        final JavaMemberSearchResultCollector otherCollector = new JavaMemberSearchResultCollector(
                IJavaElement.TYPE, secondSearchScope);
        new SearchEngine().search(otherPattern,
                new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, secondSearchScope,
                otherCollector, progressMonitor);
        if (otherCollector.hasResult()) {
            globalResult.add(otherType);
        }
    }
    globalResult.remove(sourceType);
    return globalResult;
}