In this post, we’ll look at different ways to find a class under Eclipse.
2. Supported Eclipse Versions
All the examples provided in this article are executed under Eclipse Luna Service Release 2 (4.4.2).
The key bindings used in examples are same for all eclipse versions. We are assuming that you are using default key bindings provided by Eclipse.
Shortcuts used in examples can be OS dependent. For example, in case of MacOS Cmd will be used instead of the Ctrl key.
3. Using Open Type Dialog
Eclipse provides an Open Type dialog to search for types. You can open this dialog from Navigate menu or by using shortcut Shift+Ctrl+T. Typically, a type can be a Class, Interface, Enum or Annotation.
Open Type dialog shows results when you type a name prefix or provide a wildcard or a camel case pattern. Let’s see some examples.
3.1. Name Prefix Search
Let’s enter the name prefix of the class CloudTech as input. As a result, we’ll get a list of classes whose names start with CloudTech:
3.2. Wildcard Search
Sometimes, we only remember a part of a class name. In that case, we can use a ‘*’ wildcard to represent the unknown name section. We can use Cloud*Pro to find all types starting with Cloud and having Pro after it:
3.3. Camel Case Search
We can also use Camel case search. Let’s enter CTPA to find all types containing upper-case letters:
Note that you can even mix wildcards and camel case search, to get the most out of this functionality.
4. Using Open Type in Hierarchy Dialog
You can open this dialog from the Navigate menu, or by using the shortcut Shift+Ctrl+H.
It works in the same way as Open Type dialog, but it also provides a Type Hierarchy view.
5. Using Java Search
Eclipse provides a Search dialog that can be used to search for declarations. You can open this dialog from the Search menu, or by using a shortcut Ctrl+H. Now, let’s open the Java Search tab since we want to search for Java elements.
We can filter different kinds of Java elements using Search For filter. We will continue with default Type as it is suitable for searching class.
Under Limit To the default is All occurrences. In our case, we don’t need References and Implementors, so we’ll select the Declarations filter to show only Java declarations.
We can use the Scope and Search In options to define the search scope. For now, let’s leave the default values.
Let’s enter CTPA to search for camel case types. After submitting the search, we’ll get a list of types including CloudTechProApp.
6. Using Open Declaration
Sometimes, we need to open a declaration while editing a Java file. We can open a declaration by clicking on Open Declaration item under Navigate menu, or by using the key binding F3. Another option is to hold down the Ctrl key and to click on the class you want to open.
To make it work, we need to highlight the Java element in the editor, by placing the cursor over it:
This command also works with methods and fields.
7. Using Open Type Hierarchy
Similar to the previous example, hitting F4 or selecting Open Type Hierarchy item under Navigate menu while the cursor is on the type in the editor, will show that class in the Type Hierarchy view:
In this article, we’ve seen several ways how to find a class using Eclipse, how to see the class in the hierarchy view, and how to trigger those options using menu and keyboard shortcuts.