In this video tutorial, I will show you how to use Eclipse to search and navigate your source code. I’ll discuss the following topics:
– Searching for plain text
– Searching by Java type
– Searching by method name
– Opening a method declaration
– Find references for a method
– Tracing the call hierarchy
Please subscribe to this channel 🙂
[includeme file=”wp-content/uploads/2015/05/eclipse-tutorial-toc.html”]
Video Transcript
Time – 00:00
Hi, this is Chad (shod) for luv2code.com. Welcome back to another tutorial on Eclipse. In this video you will learn how to use Eclipse to navigate and search through your source code. We’ll cover the following topics. You’ll learn how to search and find code, you’ll also learn how to find references for a class or method, we’ll also discuss how to open a method declaration, and finally, you’ll learn how to view the call stack.
Time – 00:25
Let’s get started. A common thing you need to do whenever you’re using job or development is searching for strings or searching for text. Eclipse allows you to search by simply using the CTRL + F feature, and this brings up the find and replace window. You can type in whatever text that you want to search for and Eclipse will go through and find it. You can set various options, case sensitive, whole word, and this works like a normal text editor. I’m searching for demo and I’m going to just walk through and it’ll show me all instances for demo in this actual file. If you want to make use of replace you can say, “Replace the word demo with foobar and then go through and do replace find or replace all, and again, it works like a normal text editor.
Time – 01:11
Also, when you have a large code base you may need to search for text in other files. Other than just a local file you need to search all of the files in your project. Eclipse offers a very robust search mechanism. You can search for plain text or Java specific constructs. To open the search item, select Search, Search. If you can see this brings up the search dialog. This search dialog has a lot of information. Let’s go through it one piece at a time.
Time – 01:41
There is a file search where you can search for plain text across multiple files, you can give the actual text you’re searching and the file name pattern. You can also search for task that are available in Mylyn. Mylyn is a to-do list or task list item. There is also a Git search. Git is a repository or version control system and it can search against a repo. There is also the Java search, where you can search for a specific Java constructs like types, packages or fields, that is our constructors.
Time – 02:16
All right. Let’s try out the Java search. In this example, I want to search for a class that makes use of the J button class. I’ll set the type here, and I’ll say the string I’m searching for is JButton, and that’s the actual type. I’m searching for the JButton that’s used in all of the files within this project. I hit search and at the bottom it gives me the search results. It’ll show you all of the files that are making use of JButton and you can easily expand and explore and drill down and get the actual details for it.
Time – 02:55
All right. Another thing I want to do here is I actually want to search by method name. I want to find methods that begin with a certain string. We, here I’ll just pull this down a bit. I’ll go back to my search and I’ll choose the search again. This will bring up the search window. I have the Java search. I’m going to search for an actual method name and I’m going to find a method name that starts with create.
Time – 03:19
I’ll say, create star. I’m going to use the wild card to match on any string and then I’ll hit search. At the bottom it will give me a list again, of all the methods, I’m sorry, all of the classes or files that have a method called create inside of it. So I can go through and I’ll see the match there. I’m going down some more, and I’ll see more matches. You may see other methods here referenced. It means that these methods actually use the create method inside of it. If I double click on one of these I’m going to go in and see that this method is using a create call accordingly. It’s just a very nice way of going through and finding all methods that have the name create or calls to methods with a name create.
Time – 04:02
The next feature will cover is how to navigate the Java source code as far as drilling down into an actual method. Let’s say for example, you have a line of code and here we have this J panel, p1 equals create vertical panel. There is some method that’s going to execute and create this panel for us. What exactly does this method do? We can actually open the declaration for the method and find out the actual internal details, so I can highlight this file, I’m sorry, highlight this method. Right click, say, Open Declaration. It’ll take me to the actual coding for this method. It’s just a very nice feature to navigate the code by looking at the actual declaration for a given method.
Time – 04:42
Another nice feature is that, as you’re moving through your code you may find a given method, and you want to find out who is calling that actual method. We can actually find the references for a method. What I’ll do is I’ll move up here, I’ll find this method called create horizontal panel. This is a method to finding the class but who is calling this method? I can find the references by right clicking, going to Reference, Work Space, and this will show us all of the references, basically who is calling this method in this given work space. At the bottom we’ll have search results. We have seven references. It’ll show us the actual methods and we can double click those items and go directly to the code and see how this actual method is being called.
Time – 05:29
One last feature that is very useful in navigating a code base is the call hierarchy. This basically gives you the sequence of method calls that are made for a given method. You can find the call hierarchy simply right clicking a method and saying, Open, Call Hierarchy. This will give you the actual results here and if you expand all of the nodes then you’ll eventually see the main method. Basically, the step through it you have the main method that makes a call to this constructor which in turn makes a call to add checkboxes.
Time – 06:00
This wraps up our discussion. In this video you learned how to use Eclipse to navigate and search through your source code. Please subscribe through our channel to view more videos on Eclipse and Java. Also, visit our website luv2code.com to download Java source code.