List of usage examples for org.eclipse.jdt.core.search SearchEngine search
public void search(SearchPattern pattern, SearchParticipant[] participants, IJavaSearchScope scope, SearchRequestor requestor, IProgressMonitor monitor) throws CoreException
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 a2 s . com*/ 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.eclipse.recommenders.rcp.JavaElementResolver.java
License:Open Source License
private Optional<IType> resolveType(final ITypeName recType) { // TODO woah, what a hack just to find a nested/anonymous type... this // definitely needs refactoring! ensureIsNotNull(recType);//from ww w. j ava2s . c o m if (recType.isArrayType()) { // TODO see https://bugs.eclipse.org/bugs/show_bug.cgi?id=339806 // should throw an exception? or return an Array type? log(ERROR_ARRAY_TYPE_IN_JAVA_ELEMENT_RESOLVER, recType); return absent(); } if (recType.isNestedType()) { final ITypeName declaringType = recType.getDeclaringType(); final String simpleName = StringUtils.substringAfterLast(recType.getIdentifier(), "$"); //$NON-NLS-1$ final IType parent = resolveType(declaringType).orNull(); if (parent != null) { try { for (final IType nested : parent.getTypes()) { final String key = nested.getKey(); if (key.equals(recType.getIdentifier() + ';')) { return fromNullable(nested); } } for (final IMethod m : parent.getMethods()) { for (final IJavaElement children : m.getChildren()) { if (children instanceof IType) { final IType nested = (IType) children; if (nested.getKey().endsWith(simpleName + ';')) { return of(nested); } final String key = nested.getKey(); if (key.equals(recType.getIdentifier() + ';')) { return fromNullable(nested); } } } } } catch (final Exception x) { return absent(); } } return absent(); } final IType[] res = new IType[1]; final IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); final SearchEngine search = new SearchEngine(); final String srcTypeName = Names.vm2srcTypeName(recType.getIdentifier()); final SearchPattern pattern = SearchPattern.createPattern(srcTypeName, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_FULL_MATCH); try { search.search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, new SearchRequestor() { @Override public void acceptSearchMatch(final SearchMatch match) throws CoreException { IType element = (IType) match.getElement(); // with the current settings the engine matches 'Lnull' with 'Ljava/lang/ref/ReferenceQueue$Null' if (toRecType(element).equals(recType)) { res[0] = element; } } }, null); } catch (final CoreException e) { throwUnhandledException(e); } return fromNullable(res[0]); }
From source file:org.eclipse.recommenders.utils.rcp.JavaElementResolver.java
License:Open Source License
private Optional<IType> resolveType(final ITypeName recType) { // TODO woah, what a hack just to find a nested/anonymous type... this // definitely needs refactoring! ensureIsNotNull(recType);//from w w w . j av a 2 s . c o m if (recType.isArrayType()) { // TODO see https://bugs.eclipse.org/bugs/show_bug.cgi?id=339806 // should throw an exception? or return an Array type? System.err.println("array type in JavaElementResolver. Decision bug 339806 pending...?"); return absent(); } if (recType.isNestedType()) { final ITypeName declaringType = recType.getDeclaringType(); final String simpleName = StringUtils.substringAfterLast(recType.getIdentifier(), "$"); final IType parent = resolveType(declaringType).orNull(); if (parent != null) { try { for (final IType nested : parent.getTypes()) { final String key = nested.getKey(); if (key.equals(recType.getIdentifier() + ";")) { return fromNullable(nested); } } // int count = 0; for (final IMethod m : parent.getMethods()) { for (final IJavaElement children : m.getChildren()) { if (children instanceof IType) { final IType nested = (IType) children; // count++; if (nested.getKey().endsWith(simpleName + ";")) { return of(nested); } // if (String.valueOf(count).equals(simpleName)) { // return of(nested); // } final String key = nested.getKey(); if (key.equals(recType.getIdentifier() + ";")) { return fromNullable(nested); } } } } } catch (final Exception x) { // final IType type = // parent.getType(recType.getClassName()); return absent(); } } return absent(); } final IType[] res = new IType[1]; final IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); final SearchEngine search = new SearchEngine(); final String srcTypeName = Names.vm2srcTypeName(recType.getIdentifier()); final SearchPattern pattern = SearchPattern.createPattern(srcTypeName, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_FULL_MATCH); try { search.search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, new SearchRequestor() { @Override public void acceptSearchMatch(final SearchMatch match) throws CoreException { IType element = (IType) match.getElement(); // with the current settings the engine matches 'Lnull' with 'Ljava/lang/ref/ReferenceQueue$Null' if (toRecType(element).equals(recType)) { res[0] = element; } } }, null); } catch (final CoreException e) { throwUnhandledException(e); } return fromNullable(res[0]); }
From source file:org.eclipse.wb.internal.core.utils.jdt.core.CodeUtils.java
License:Open Source License
/** * @return references of given {@link IJavaElement} in given {@link IJavaSearchScope}. */// w w w . jav a 2 s. co m private static List<IJavaElement> searchReferences(IJavaSearchScope scope, IJavaElement element) throws Exception { final List<IJavaElement> references = Lists.newArrayList(); SearchRequestor requestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) { if (match instanceof ReferenceMatch) { ReferenceMatch refMatch = (ReferenceMatch) match; IJavaElement matchElement = (IJavaElement) refMatch.getElement(); { IJavaElement localElement = refMatch.getLocalElement(); if (localElement != null) { matchElement = localElement; } } references.add(matchElement); } } }; // do search SearchPattern pattern = SearchPattern.createPattern(element, IJavaSearchConstants.REFERENCES); SearchEngine searchEngine = new SearchEngine(); searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, new NullProgressMonitor()); // done return references; }
From source file:org.eclipse.xtext.xbase.ui.navigation.JvmImplementationOpener.java
License:Open Source License
/** * Main parts of the logic is taken from {@link org.eclipse.jdt.internal.ui.javaeditor.JavaElementImplementationHyperlink} * * @param element - Element to show implementations for * @param textviewer - Viewer to show hierarchy view on * @param region - Region where to show hierarchy view *//* www .j a v a2 s . c o m*/ public void openImplementations(final IJavaElement element, ITextViewer textviewer, IRegion region) { if (element instanceof IMethod) { ITypeRoot typeRoot = ((IMethod) element).getTypeRoot(); CompilationUnit ast = SharedASTProvider.getAST(typeRoot, SharedASTProvider.WAIT_YES, null); if (ast == null) { openQuickHierarchy(textviewer, element, region); return; } try { ISourceRange nameRange = ((IMethod) element).getNameRange(); ASTNode node = NodeFinder.perform(ast, nameRange); ITypeBinding parentTypeBinding = null; if (node instanceof SimpleName) { ASTNode parent = node.getParent(); if (parent instanceof MethodInvocation) { Expression expression = ((MethodInvocation) parent).getExpression(); if (expression == null) { parentTypeBinding = Bindings.getBindingOfParentType(node); } else { parentTypeBinding = expression.resolveTypeBinding(); } } else if (parent instanceof SuperMethodInvocation) { // Directly go to the super method definition openEditor(element); return; } else if (parent instanceof MethodDeclaration) { parentTypeBinding = Bindings.getBindingOfParentType(node); } } final IType type = parentTypeBinding != null ? (IType) parentTypeBinding.getJavaElement() : null; if (type == null) { openQuickHierarchy(textviewer, element, region); return; } final String earlyExitIndicator = "EarlyExitIndicator"; final ArrayList<IJavaElement> links = Lists.newArrayList(); IRunnableWithProgress runnable = new IRunnableWithProgress() { @Override public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { if (monitor == null) { monitor = new NullProgressMonitor(); } try { String methodLabel = JavaElementLabels.getElementLabel(element, JavaElementLabels.DEFAULT_QUALIFIED); monitor.beginTask( Messages.format("Searching for implementors of ''{0}''", methodLabel), 100); SearchRequestor requestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { if (match.getAccuracy() == SearchMatch.A_ACCURATE) { IJavaElement element = (IJavaElement) match.getElement(); if (element instanceof IMethod && !JdtFlags.isAbstract((IMethod) element)) { links.add(element); if (links.size() > 1) { throw new OperationCanceledException(earlyExitIndicator); } } } } }; int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE; SearchPattern pattern = SearchPattern.createPattern(element, limitTo); Assert.isNotNull(pattern); SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }; SearchEngine engine = new SearchEngine(); engine.search(pattern, participants, SearchEngine.createHierarchyScope(type), requestor, new SubProgressMonitor(monitor, 100)); if (monitor.isCanceled()) { throw new InterruptedException(); } } catch (OperationCanceledException e) { throw new InterruptedException(e.getMessage()); } catch (CoreException e) { throw new InvocationTargetException(e); } finally { monitor.done(); } } }; try { PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable); } catch (InvocationTargetException e) { IStatus status = new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, Messages.format( "An error occurred while searching for implementations of method ''{0}''. See error log for details.", element.getElementName()), e.getCause()); JavaPlugin.log(status); ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Open Implementation", "Problems finding implementations.", status); } catch (InterruptedException e) { if (e.getMessage() != earlyExitIndicator) { return; } } if (links.size() == 1) { openEditor(links.get(0)); } else { openQuickHierarchy(textviewer, element, region); } } catch (JavaModelException e) { log.error("An error occurred while searching for implementations", e.getCause()); } catch (PartInitException e) { log.error("An error occurred while searching for implementations", e.getCause()); } } }
From source file:org.eclipselabs.collage.model.resourceid.JavaClassFileIdentifier.java
License:Open Source License
@Override public IEditorPart openInEditor() throws CoreException { synchronized (foundElementLock) { if (foundElement == null) { SearchEngine engine = new SearchEngine(); SearchPattern pattern = SearchPattern.createPattern(className, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); ClassFileSearchRequestor requestor = new ClassFileSearchRequestor(); engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createWorkspaceScope(), requestor, new NullProgressMonitor()); foundElement = requestor.getElement(); }//ww w . j a va 2 s . c om if (foundElement != null) { return JavaUI.openInEditor(foundElement); } } CollageUtilities.showError(CollageActivator.PLUGIN_NAME, "Unable to open an editor for " + shortName + "."); return null; }
From source file:org.fusesource.ide.camel.editor.globalconfiguration.beans.BeanConfigUtil.java
License:Open Source License
public boolean hasMethod(String methodName, IType type) { if (type == null) { return false; }//from ww w .j a va2s . c o m SearchPattern pattern = SearchPattern.createPattern(methodName, IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH); IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { type }); CountingSearchRequestor matchCounter = new CountingSearchRequestor(); SearchEngine search = new SearchEngine(); try { search.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, matchCounter, null); } catch (CoreException ce) { CamelEditorUIActivator.pluginLog() .logError(UIMessages.beanConfigUtilMethodSelectionErrorNoTypeFound + ce.getMessage(), ce); } return matchCounter.getNumMatch() > 0; }
From source file:org.fusesource.ide.jvmmonitor.internal.ui.actions.OpenDeclarationAction.java
License:Open Source License
/** * Searches the source for the given class name. * //from ww w . j ava 2 s . com * @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.gemoc.execution.sequential.javaengine.PlainK3ExecutionEngine.java
License:Open Source License
/** * search the bundle that contains the Main class. The search is done in the workspace scope (ie. if it is defined * in the current workspace it will find it * // ww w . j av a2 s . c o m * @return the name of the bundle containing the Main class or null if not found */ private IType getITypeMainByWorkspaceScope(String className) { SearchPattern pattern = SearchPattern.createPattern(className, IJavaSearchConstants.CLASS, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH); IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); final List<IType> binaryType = new ArrayList<IType>(); SearchRequestor requestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { binaryType.add((IType) match.getElement()); } }; SearchEngine engine = new SearchEngine(); try { engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, null); } catch (CoreException e1) { throw new RuntimeException("Error while searching the bundle: " + e1.getMessage()); // return new Status(IStatus.ERROR, Activator.PLUGIN_ID, ); } return binaryType.isEmpty() ? null : binaryType.get(0); }
From source file:org.grails.ide.eclipse.refactoring.rename.type.ServiceReferenceUpdater.java
License:Open Source License
@Override public void createChanges(final ParticipantChangeManager changes, final RefactoringStatus status, IProgressMonitor pm) {// w ww . j av a 2s. com Assert.isNotNull(changes); try { IJavaSearchScope scope = RefactoringUtils.getSearchScope(project.getJavaProject()); SearchEngine engine = new SearchEngine(); final String fieldName = GrailsNameUtils.getPropertyName(renaming.getTarget().getFullyQualifiedName()); final String fieldNewName = GrailsNameUtils.getPropertyName(renaming.getNewName()); SearchPattern fieldByNamePat = SearchPattern.createPattern(fieldName, IJavaSearchConstants.FIELD, IJavaSearchConstants.ALL_OCCURRENCES, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE); SearchRequestor req = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { try { if (match instanceof FieldDeclarationMatch || match instanceof FieldReferenceMatch) { Object el = match.getElement(); if (el instanceof IJavaElement) { IJavaElement jel = (IJavaElement) el; ICompilationUnit cu = (ICompilationUnit) jel .getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { TextChange cuChange = changes.getCUChange(cu); final int offset = match.getOffset(); final int length = match.getLength(); String text = cu.getBuffer().getText(offset, length); if (text.equals(fieldName)) { //Only perform the edit if the text we are about to replace is what we expect! cuChange.addEdit(new ReplaceEdit(offset, length, fieldNewName)); } } } } } catch (Exception e) { //Ignore this match and log the exception... //but keep processing other matches! GrailsCoreActivator.log(e); } } }; engine.search(fieldByNamePat, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, req, pm); } catch (CoreException e) { GrailsCoreActivator.log(e); } }