Have you ever wanted to suspend execution only under a certain circumstance while debugging, such as every hundredth loop iteration? You can. Set your breakpoint, right-click it, select â€œPropertiesâ€, check â€œEnable Conditionâ€ and enter a condition in normal Java code that will be evaluated every time the breakpoint is hit to determine whether or not to suspend the JVM.
Sometimes things work on everybodyâ€™s box.. except Bobâ€™s. Youâ€™ve watched him produce the issue and have tried to reproduce it on your development machine, but it just ainâ€™t happeninâ€™ for anybody but him. Itâ€™s time for a remote debugging session. Start up the JVM on Bobâ€™s box like so..
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,suspend=n,server=y
Within Eclipse IDE on your development machine, select Run -> Debug… and create a new â€œRemote Java Applicationâ€ configuration. Select the project that contains the source code, enter Bobâ€™s boxes hostname (or IP address) and the port his JVM is listening on (8000 in this case). Click the â€œDebugâ€ button to close the dialog and your â€œDebugâ€ view should now show a new session, connected to Bobâ€™s JVM, and the threads of your application just as if you were debugging it locally. You can set breakpoints and use all the other features described in this article, or impress your mom by starting another instance of your application locally and step through both Bobâ€™s and your own JVM instance simultaneously, side-by-side!
Dynamic Variable & Code Replacement
If youâ€™re debugging a tricky piece of SQL in a servlet that isnâ€™t working right, itâ€™s tempting to abide by the typical write, compile, deploy, run, debug, repeat cycle every time you need to tweak the String. This is tedious and time consuming. Instead, set a breakpoint someplace after the String is defined but before it is used. When you hit it, right-click the variable in the â€œVariablesâ€ view and select â€œChange Valueâ€¦â€. You can figure it out from there. (Note: This doesnâ€™t seem to work for â€œfinalâ€ variables, and so-so with statically declared things.)
If you modify and save a class while you have a debugging connection established, Eclipse will try its best to update the code on the remote JVM without having to restart it. This doesnâ€™t always work (especially if there are stack frames using the changed code), but youâ€™ll be notified if the remote JVM could not be updated. This is especially useful when testing a complex algorithm in a situation that is a pain in the arse to set up.
Custom Code Templates
Typing â€œfore[autocomplete]â€ in a block in Java code will add a â€œforeachâ€ code template that you can fill in using the TAB key. These are customizable, and you can even add your own. For example, I prefix all generated local variable and Iterator declarations with the â€œfinalâ€ keyword, and have added my own custom templates for inserting my most frequently used Java 5 annotations. Go to Window -> Preferencesâ€¦ -> Java -> Editor -> Template and have some fun.
All of the refactoring features of Ecilpse IDE are worth learning. I canâ€™t think of one that isnâ€™t. Once you have a method defined and used, there isnâ€™t much reason to manually modify the declaration. â€œMoveâ€, â€œChange Method Signatureâ€¦â€, â€œPull Upâ€¦â€, â€œPush Downâ€¦â€ etc. give you basic, commonly used tools, and actions such as â€œExtract Interfaceâ€¦â€, â€œUser Supertype Where Possibleâ€¦â€ etc. provide support for larger jobs. Understanding what all these do helps relieve some of the pain felt when refactoring heavily used, public APIs, and helps keep you focused on the bigger picture by taking care of the details.