As you’d expect of a good IDE, Eclipse has a lot of
built-in search support. However, searching in Eclipse is not
accomplished via the standard Edit→ Find/Replace operation,
which enables you to search the current file. Instead, searching
displays all matches in the Search view so that you can select the
one or ones you want to jump to. In addition, the items in the Search
menu enable you to search across all files in workspaces, through the
help system, all plug-ins, and more. Wildcards (
?) work as well.
The Search dialog appears in Figure 4-7; note the four tabs there:
- File Search
- Help Search
- Java Search
- Plug-in Search
For example, take a look at the File Search tab, which enables you to
search across multiple files for specified text
(printem in this example). You can give the
pattern(s) of the files to search, such as "
*.java, *.*“, and so on, in the “File name
patterns” box, and set the scope of the search with
the radio buttons beneath that box.
The “Selected Resources” radio button in the “Scope” box is not enabled in this dialog by default. If you want to search only the current project or just a restricted number of projects without using a working set, select that project or those projects in the Package Explorer, then open the Search dialog and click the “Selected Resources” radio button.
When you click the Search button, the results appear in the Search view, shown in Figure 4-8. Double-clicking a match opens the match in the JDT editor with an arrow in the marker bar, as shown in the figure.
You also can perform Java searches with the Java Search tab in the Search dialog. This kind of search enables you to search for Java elements by kind—types, methods, packages, constructors, and fields, as shown in Figure 4-9. You can limit the search so that it matches only references, also shown in the figure.
To search for methods with a specific return type, open the Search
dialog, and click the Java Search tab. Type
> in the Search
string, click the “Method” and
“Declarations” radio buttons, and
then click Search.
Being able to perform multifile searches such as this is one of the biggest reasons for using an IDE. If you’ve been writing Java with a simple text editor, you’ll find there’s no comparison.
Besides the Search→ Search, Search→ File, Search→ Help, and Search→ Java items in the Search menu, you also can highlight an element in your code and select the following Search menu items to do a quick search:
- Search→ References
Finds references to the element
- Search→ Declarations
Finds the element’s declaration
- Search→ Implementors
Finds implementors of the element
- Search→ Read Access
Finds read accesses (fields only)
- Search→ Write Access
Finds write accesses (fields only)
These items are handy for tracking down all references to a method or variable, for example; just search for the references, and double-click the results in the Search view. To limit the scope of the search, select Workspace, Hierarchy, or Working Set from each item’s submenu.
Another quick way to search for references is to highlight an identifier and select Search→ Occurrences in File. This is more useful than Edit→ Find/Replace because it lists all matches in the Search view, enabling you to navigate to the ones you want.