IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { yourProject }); //IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); // Use this if you dont have the IProject in hand SearchPattern searchPattern = SearchPattern.createPattern(field, IJavaSearchConstants.REFERENCES); SearchRequestor requestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) { System.out.println(match.getElement()); } }; SearchEngine searchEngine = new SearchEngine(); searchEngine.search(searchParttern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, new NullProgressMonitor());
SearchRequestor findMethod = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { // analyze search match. } }; SearchEngine engine = new SearchEngine(); IJavaSearchScope workspaceScope = SearchEngine .createWorkspaceScope(); SearchPattern pattern = SearchPattern .createPattern( "org.eclipse.e4.core.internal.contexts.EclipseContext.strategy", IJavaSearchConstants.FIELD, IJavaSearchConstants.REFERENCES, SearchPattern.R_EXACT_MATCH); SearchParticipant[] participant = new SearchParticipant[] { SearchEngine .getDefaultSearchParticipant() }; engine.search(pattern, participant, workspaceScope, findMethod, new NullProgressMonitor());
/** * Searches for all main methods in the given scope. * Valid styles are IJavaElementSearchConstants.CONSIDER_BINARIES and * IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS */ public IType[] searchMainMethods(IProgressMonitor pm, IJavaSearchScope scope, int style) throws CoreException { List typesFound= new ArrayList(200); SearchPattern pattern= SearchPattern.createPattern("main(String[]) void", //$NON-NLS-1$ IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); SearchRequestor requestor= new MethodCollector(typesFound, style); new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return (IType[]) typesFound.toArray(new IType[typesFound.size()]); }
private static IType[] findTypes(String typeName, IProgressMonitor monitor) throws CoreException { final List results= new ArrayList(); SearchRequestor collector= new SearchRequestor() { public void acceptSearchMatch(SearchMatch match) throws CoreException { Object element= match.getElement(); if (element instanceof IType) results.add(element); } }; SearchEngine engine= new SearchEngine(); SearchPattern pattern= SearchPattern.createPattern(typeName, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH); engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createWorkspaceScope(), collector, monitor); return (IType[]) results.toArray(new IType[results.size()]); }
/** * @return all package fragments in <code>scope</code> with same name as <code>fPackage</code>, excluding fPackage */ private IPackageFragment[] getNamesakePackages(IJavaSearchScope scope, IProgressMonitor pm) throws CoreException { SearchPattern pattern= SearchPattern.createPattern(fPackage.getElementName(), IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); final HashSet packageFragments= new HashSet(); SearchRequestor requestor= new SearchRequestor() { public void acceptSearchMatch(SearchMatch match) throws CoreException { IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(match); if (enclosingElement instanceof IPackageFragment) { IPackageFragment pack= (IPackageFragment) enclosingElement; if (! fPackage.equals(pack)) packageFragments.add(pack); } } }; new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return (IPackageFragment[]) packageFragments.toArray(new IPackageFragment[packageFragments.size()]); }
/** * Searches for all main methods in the given scope. * Valid styles are IJavaElementSearchConstants.CONSIDER_BINARIES and * IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS * @param pm progress monitor * @param scope the search scope * @param style search style constants (see {@link IJavaElementSearchConstants}) * @return the types found * @throws CoreException */ public IType[] searchMainMethods(IProgressMonitor pm, IJavaSearchScope scope, int style) throws CoreException { List<IType> typesFound= new ArrayList<>(200); SearchPattern pattern= SearchPattern.createPattern("main(String[]) void", //$NON-NLS-1$ IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); SearchRequestor requestor= new MethodCollector(typesFound, style); new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return typesFound.toArray(new IType[typesFound.size()]); }
/** * Searches for all main methods in the given scope. * Valid styles are IJavaElementSearchConstants.CONSIDER_BINARIES and * IJavaElementSearchConstants.CONSIDER_EXTERNAL_JARS * @param pm progress monitor * @param scope the search scope * @param style search style constants (see {@link IJavaElementSearchConstants}) * @return the types found * @throws CoreException */ public IType[] searchMainMethods(IProgressMonitor pm, IJavaSearchScope scope, int style) throws CoreException { List<IType> typesFound= new ArrayList<>(200); SearchPattern pattern= SearchPattern.createPattern("main(String[]) void", //$NON-NLS-1$ IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); SearchRequestor requestor= new MethodCollector(typesFound, style); new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return typesFound.toArray(new IType[typesFound.size()]); }
private IMethod[] searchForDeclarationsOfClashingMethods(IProgressMonitor pm) throws CoreException { final List results= new ArrayList(); SearchPattern pattern= createNewMethodPattern(); IJavaSearchScope scope= RefactoringScopeFactory.create(getMethod().getJavaProject()); SearchRequestor requestor= new SearchRequestor() { public void acceptSearchMatch(SearchMatch match) throws CoreException { Object method= match.getElement(); if (method instanceof IMethod) // check for bug 90138: [refactoring] [rename] Renaming method throws internal exception results.add(method); else JavaPlugin.logErrorMessage("Unexpected element in search match: " + match.toString()); //$NON-NLS-1$ } }; new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return (IMethod[]) results.toArray(new IMethod[results.size()]); }
private static SearchResultGroup[] internalSearch(SearchEngine searchEngine, SearchPattern pattern, IJavaSearchScope scope, CollectingSearchRequestor requestor, IProgressMonitor monitor, RefactoringStatus status) throws JavaModelException { try { searchEngine.search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, monitor); } catch (CoreException e) { throw new JavaModelException(e); } return groupByCu(requestor.getResults(), status); }
private IMethod[] searchForDeclarationsOfClashingMethods(IProgressMonitor pm) throws CoreException { final List<IMethod> results= new ArrayList<>(); SearchPattern pattern= createNewMethodPattern(); IJavaSearchScope scope= RefactoringScopeFactory.create(getMethod().getJavaProject()); SearchRequestor requestor= new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object method= match.getElement(); if (method instanceof IMethod) // check for bug 90138: [refactoring] [rename] Renaming method throws internal exception results.add((IMethod) method); else JavaPlugin.logErrorMessage("Unexpected element in search match: " + match.toString()); //$NON-NLS-1$ } }; new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return results.toArray(new IMethod[results.size()]); }
private IMethod[] searchForDeclarationsOfClashingMethods(IProgressMonitor pm) throws CoreException { final List<IMethod> results= new ArrayList<>(); SearchPattern pattern= createNewMethodPattern(); IJavaSearchScope scope= RefactoringScopeFactory.create(getMethod().getJavaProject()); SearchRequestor requestor= new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object method= match.getElement(); if (method instanceof IMethod) { results.add((IMethod) method); } else { JavaLanguageServerPlugin.logError("Unexpected element in search match: " + match.toString()); //$NON-NLS-1$ } } }; new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return results.toArray(new IMethod[results.size()]); }
private IMethod[] searchForDeclarationsOfClashingMethods(IProgressMonitor pm) throws CoreException { final List<IMethod> results= new ArrayList<>(); SearchPattern pattern= createNewMethodPattern(); IJavaSearchScope scope= RefactoringScopeFactory.create(getMethod().getJavaProject()); SearchRequestor requestor= new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object method= match.getElement(); if (method instanceof IMethod) // check for bug 90138: [refactoring] [rename] Renaming method throws internal exception results.add((IMethod) method); else JavaPlugin.logErrorMessage("Unexpected element in search match: " + match.toString()); //$NON-NLS-1$ } }; new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm); return results.toArray(new IMethod[results.size()]); }
private static SearchResultGroup[] internalSearch(SearchEngine searchEngine, SearchPattern pattern, IJavaSearchScope scope, CollectingSearchRequestor requestor, IProgressMonitor monitor, RefactoringStatus status) throws JavaModelException { try { searchEngine.search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, monitor); } catch (CoreException e) { throw new JavaModelException(e); } return groupByCu(requestor.getResults(), status); }
private static SearchResultGroup[] internalSearch(SearchEngine searchEngine, SearchPattern pattern, IJavaSearchScope scope, CollectingSearchRequestor requestor, IProgressMonitor monitor, RefactoringStatus status) throws JavaModelException { try { searchEngine.search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, monitor); } catch (CoreException e) { throw new JavaModelException(e); } return groupByCu(requestor.getResults(), status); }
private static SearchResultGroup[] internalSearch(SearchEngine searchEngine, SearchPattern pattern, IJavaSearchScope scope, CollectingSearchRequestor requestor, IProgressMonitor monitor, RefactoringStatus status) throws JavaModelException { try { searchEngine.search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, monitor); } catch (CoreException e) { throw new JavaModelException(e); } return groupByCu(requestor.getResults(), status); }
private void findAllDeclarations(IProgressMonitor monitor, WorkingCopyOwner owner) throws CoreException { fDeclarations = new ArrayList<>(); class MethodRequestor extends SearchRequestor { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { IMethod method = (IMethod) match.getElement(); boolean isBinary = method.isBinary(); if (!isBinary) { fDeclarations.add(method); } } } 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); MethodRequestor requestor = new MethodRequestor(); SearchEngine searchEngine = owner != null ? new SearchEngine(owner) : new SearchEngine(); searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), requestor, monitor); }
public static void findSuiteMethods(IJavaElement element, Set result, IProgressMonitor pm) throws CoreException { // fix for bug 36449 JUnit should constrain tests to selected project // [JUnit] IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] { element }, IJavaSearchScope.SOURCES); SearchRequestor requestor= new SuiteMethodTypesCollector(result); int matchRule= SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_ERASURE_MATCH; SearchPattern suitePattern= SearchPattern.createPattern("suite() Test", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, matchRule); //$NON-NLS-1$ SearchParticipant[] participants= new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }; new SearchEngine().search(suitePattern, participants, scope, requestor, pm); }
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; } return element; }
public static void findSuiteMethods(IJavaElement element, Set<IType> result, IProgressMonitor pm) throws CoreException { // fix for bug 36449 JUnit should constrain tests to selected project // [JUnit] IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] { element }, IJavaSearchScope.SOURCES); SearchRequestor requestor= new SuiteMethodTypesCollector(result); int matchRule= SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_ERASURE_MATCH; SearchPattern suitePattern= SearchPattern.createPattern("suite() Test", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, matchRule); //$NON-NLS-1$ SearchParticipant[] participants= new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }; new SearchEngine().search(suitePattern, participants, scope, requestor, pm); }
private void searchForTypesUsed(SearchEngine engine, IJavaElement parent, IType[] types, IJavaSearchScope scope) throws CoreException { for (IType type : types) { if (type.isAnonymous()) continue; TypeReferenceSearchRequestor requestor = new TypeReferenceSearchRequestor(); engine.search(SearchPattern.createPattern(type, IJavaSearchConstants.REFERENCES), new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}, scope, requestor, null); if (requestor.containMatches()) { TypeDeclarationSearchRequestor decRequestor = new TypeDeclarationSearchRequestor(); engine.search(SearchPattern.createPattern(type, IJavaSearchConstants.DECLARATIONS), new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}, SearchEngine.createJavaSearchScope(new IJavaElement[] {parent}), decRequestor, null); Match match = decRequestor.getMatch(); if (match != null) fSearchResult.addMatch(match); } } }