Community
Participate
Working Groups
I am trying to get the names of all different types and classes used in the .java file currently open in the Java-Editor. For that purpose I used the Java Search Engine, in the way described in the Eclipse-Help (JDT Plug-in Developer Guide -> Programmer's Guide -> JDT Core -> Using the Java search engine). I created some Searchpatterns, which should search for types, but my results are methods, and not a single type. My code is as follows: ClassSearchJob extends Job { IWorkbenchWindow currentAktivWorkbench; public ClassSearchJob(String name) { super(name); } protected IStatus run(IProgressMonitor monitor) { IJavaElement element = null; IWorkbenchPage aktivPage = currentAktivWorkbench.getActivePage(); IEditorPart part = (IEditorPart) aktivPage.getActiveEditor(); IEditorInput editorInput= part.getEditorInput(); if (editorInput != null) { element = (IJavaElement)editorInput.getAdapter(IJavaElement.class); } ClassSearchRequestor requestor = new ClassSearchRequestor(); IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {element}); SearchPattern pattern = SearchPattern.createPattern("*", IJavaSearchConstants.CLASS, IJavaSearchConstants.ALL_OCCURRENCES, SearchPattern.R_EXACT_MATCH ); SearchEngine searchEngine = new SearchEngine(); try { searchEngine.search(pattern, new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}, scope, requestor, null); } catch (CoreException ce) {} Vector results = requestor.matches; for (Enumeration enu = results.elements(); enu.hasMoreElements();) { System.out.println(enu.nextElement().toString()); } return Status.OK_STATUS; } } Some comments on my code: currentAktivWorkbench is set by the UI-Thread that creates the job. The ClassSearchRequestor used above has empty methods "beginReporting()" and "endReporting()". The method "acceptSearchMatch(SearchMatch match)" adds the SearchMatch to a public Vector called "matches". The job is running on "BUILD" priority.
It should work properly as we test these pattern parameter and get expected results... I'll have a look at your sample and try to find out where the problem is.
Dear Mr. Fusier, In one of the newsgroups I posted my problem last week, yesterday came an explanation. Somebody showed me a sentence in the JDT Programmers Guide (Page 82): “For example, searching for references to a method could find such a reference in an initializer. The initializer that contains this method reference is the element of the search match. “ This explains why I only get methods as results (they contain the types I search). In conclusion the “Bug” I found is not a Bug, but at most a missing feature. (Since I don’t know how to get the names of the types out of the methods, and I don’t see a way doing that with the Java search engine) I hope you haven’t spend to much time on it, Thilo Brause
Close as INVALID. SearchMatch allow you to retrieve the name of the reference or declaration in the source using offset and length. However, for class declaration names it is better to use searchAllTypeNames method...