Bug 113514 - [search] Strange results in Java search engine
Summary: [search] Strange results in Java search engine
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2 M3   Edit
Assignee: Frederic Fusier CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-10-24 06:04 EDT by Thilo Brause CLA
Modified: 2005-10-25 04:10 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thilo Brause CLA 2005-10-24 06:04:10 EDT
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.
Comment 1 Frederic Fusier CLA 2005-10-24 06:22:17 EDT
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.
Comment 2 Frederic Fusier CLA 2005-10-25 04:05:23 EDT
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
Comment 3 Frederic Fusier CLA 2005-10-25 04:10:42 EDT
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...