10 Tips for debugging Java programs with Eclipse __java

Source: Internet
Author: User

Respect the author, respect the original

This article turns from: http://www.admin10000.com/document/1718.html -Breakpoint View: Conditional breakpoint

This is useful if you are interested in only one part of the application. For example, if you want to check the program during the 13th cycle, or debug some functionality in an abstract parent class, you are only concerned with one specific implementation. You can set the condition in the breakpoint view, or the right menu ("Breakpoint Properties") marked by the blue breakpoint next to the code. You can pause the program when the conditional code fragment is true, or suspend the program when the value of the code fragment changes.


-Variable view: Demonstrating logical Structure

If you need to view the values of a Map object or list object in a variable view, the default settings for Eclipse are generally not as easy. Assuming that you are using HashMap, you need to click through each entity entry and face various HASHMAP implementation details. However, there is a button called "Show Logical Structure" on the variable view. It is very useful, especially if the information displayed by your object's ToString () method is not friendly. My boss showed me this feature a couple of weeks ago. You know, he often deals with PowerPoint or Excel. How embarrassing for a developer like me.

"Show Logical Structure" is not turned on

Open "Show Logical Structure"

-Variable view: Changing the value of the variable ...

When you need to change the input information slightly, do not need to restart the debugging session, just enter the new information in a table, you can in the debugging phase directly modify the value of your variable. There are times when you can save some time and you can use this feature to simulate some weird situations more simply.

-Display View

Do you know the "Display view"? You can activate it by "window"-> "Show View"-> "Display" during debugging. Now, your eclipse should be a blank new view. You can use this view to enter or calculate some new code. The code is executed in the context of the current debug location, which means that you can use all variables or even content assistants. To execute your code, simply mark it and use the right-click menu or Ctrl+u (execute) or ctrl+shift+i (check).

-Navigation: Drop to Frame

I think anyone knows "step in", "Step Over", and may also know "step return". These are basic navigational features when debugging. I want to mention two advanced methods of navigation that I like very much. The first one is "Drop to Frame". With this feature you can roll back in time, and you can fall back directly to a frame in the running Java stack frame. When I debug, I accidentally miss a line of concern, I often use this fallback to a frame. Using the "Drop to Frame" feature, I can simply rerun a frame of code.

-Navigation: Step Into Selection

The second one is "Step into Selection". It's also very simple, but a lot of people use one. To use it, you just press Ctrl+alt and click the name of the method you want to run to. Very convenient, very fast. Compared to the "step into" this is very convenient, think you like to trace into a function with a lot of parameters, if you have to enter into the ' step ', exit each parameter calculation process to enter the function you really want to go. "Run To Line" is also a great feature. Just put the cursor in front of which line you want to run, and then press "Ctrl+r" on it.

-Navigator: Using the keyboard

If you avoid using the mouse, you will be able to operate more quickly. You should at least have the following shortcut key: f5– Step into the f6– step and skip the "f7–" step and Return to "f8–" to continue the ctrl+shift+b– "add Breakpoints" ctrl+shift+i– "check" -breakpoints View: Watchpoints

What happens if we change this variable?! Sometimes creating a watchpoint is useful for debugging. The debugger stops, regardless of whether the observed field is modified or read-You can configure the decision yourself. You just double-click a field, and then you can see the watchpoint in the breakpoint view, and you can edit its properties. You can even have an access number, which means the debugger stops when the number of times a variable is accessed reaches that amount. This is true for common breakpoints as well.

-friendly readable objects

A variable view is a value that uses the object's ToString method to actually correspond. For this reason, it can be very useful for debugging if you provide a friendly ToString method implementation. The default ToString implementation for Java.lang.Object in Javadoc is also recommended: Returns a String representing the object. Usually the <code>toString</code> method returns such a string, "literally" that object. The return result must be a concise but rich message that can represent the object, and is friendly to read. We recommend that all subclasses override this method.

You can refer to the Tostringbuilder in Commons-lang. It provides functionality to write (referencing from Javadoc) a "good and coherent" tostring method.

The default ToString

The default Tostringbuilder example

Tostringbuilder Example – Multiple lines of text style

If you can't modify the ToString implementation, such as if you are using a framework or an external API, you can have another option to create a "Detail Formatter" in Eclipse. You need to right-click an object in the variable view and click on "New Detail Formatter ...". You can then enter some code to show the object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.