Click on "IT Ranch" above and select "Set as Star" to deliver technical dry goods daily!
Author: bojiangzhou https://www.cnblogs.com/chiangchou
Debug is used to track the running process of the code. Usually an exception occurs during the running of the program. Debug mode can be used to analyze the location of the exception and the change of the parameters during the running. Usually we can also enable the Debug mode to track the running process of the code to learn the source code of the tripartite framework.
So learn how to use Debug in Intellij IDEA, mainly including the following:
First look at the interface in Debug mode in IDEA.
The following is the interface after starting the Debug mode in IDEA and entering the breakpoint. My case is Windows, which may be different from the Mac icons. Simply talk about the 8 places marked in the figure below:
Check Show debug window on breakpoint in the settings, then the Debug window will be automatically activated after the request enters the breakpoint
If there is no toolbar or status bar displayed at the bottom of your IDEA, you can open it in View, and it will be convenient for us to display the toolbar. You can try these four options yourself.
There are debugging corresponding functions in the menu bar Run, and the corresponding shortcut keys can be viewed at the same time .
The Debug function mainly corresponds to the two groups of buttons 4 and 5 in Figure 1:
1. 1. let's talk about the first group of buttons, there are 8 buttons in total, from left to right as follows:
2. The second group of buttons, a total of 7 buttons, from top to bottom are as follows:
Update the program, On'Update' actions, what you do when you perform the update operation, generally select'Update classes and resources', that is, update classes and resource files.
Generally, it is better to use with hot deployment plug-ins, such as JRebel, so that you don't have to restart the service every time you change the code. How to activate JRebel is attached in the last chapter.
The following On frame deactivation is triggered when the IDEA window loses focus, that is, when you switch from idea to browser, idea will automatically do things for you. Generally, you can set Do nothing. Frequent switching will consume more resources.
In the Debug process, it is very necessary to track and view the changes of variables. Here are a few places where you can view variables in IDEA , I believe most people understand.
1. As follows, in IDEA, the current variable value will be displayed after the line where the parameter is located.
2. Hover the cursor on the parameter to display the current variable information. Click to open the details as shown in Figure 3.3. I usually use this method, which is quick and convenient.
3. View in Variables. All variables in the current method are displayed here.
4. In Watches, click New Watch and enter the variables to be viewed. Or you can drag it from Variables to Watche to view.
If you find that you don't have Watches, it may be in the place below.
The aforementioned calculation expression is shown in Figure 4.1, the button, Evaluate Expression (Alt + F8). You can use this operation to calculate the value of an expression during debugging without printing information.
1. Press Alt + F8 or the button, or you can select an expression and then Alt + F8, the calculation expression window will pop up , as shown below, press Enter or click Evaluate to calculate the value of the expression.
This expression can be not only a general variable or parameter, but also a method. When you call several methods in a line of code, you can view the return value of a method in this way.
2. Set the variable. In the calculation expression box, you can change the value of the variable , so that sometimes it is convenient for us to debug various values.
Think about it, there are several methods in a line of code, how to choose only one method to enter. As mentioned before, using Step Into (Alt + F7) or Force Step Into (Alt + Shift + F7) to enter the method, but these two operations will be entered in sequence according to the method call sequence, which is more troublesome.
Then smart step into is very convenient, smart step into, this function can be seen in Run, Smart Step Into (Shift + F7), as shown in Figure 5.1
Press Shift + F7, it will automatically locate the current breakpoint line, and list the methods that need to be entered, as shown in Figure 5.2, click on the method to enter the method.
If there is only one method, enter directly, similar to Force Step Into.
By setting the breakpoint condition, it stops at the breakpoint when the condition is met, otherwise it runs directly.
Usually, when we are traversing a relatively large collection or array, a breakpoint is set in the loop, should we look at the values of the variables one by one? It must be very tiring, maybe you missed this and it's worth doing it again.
1. Right-click on the breakpoint to directly set the conditions of the current breakpoint , as shown in Figure 6.1. The breakpoint will only take effect when I set exist to true.
2. Click View Breakpoints (Ctrl + Shift + F8) to view all breakpoints.
Java Line Breakpoints shows all breakpoints. Check Condition on the right to set the breakpoint conditions.
Check Log message to console, the current breakpoint line will be output to the console, as shown in Figure 6.3
Check Evaluate and log, you can execute this line of code to calculate the value of the expression and output the result to the console.
3. Let's talk about the Filters on the right . These are not commonly used in general, so let's briefly talk about the meaning.
Instance filters: Instance filter, enter the instance ID (the instance ID in Figure 6.5), but I have not succeeded here. I don’t know the reason. Anyone who knows will leave a comment.
Class filters: class filter, filter according to class name, also unsuccessful...
Pass count: used in loops, if the breakpoint is in the loop, you can set this value, and stop at the breakpoint after how many loops, and then the loop will stop at the breakpoint.
4. Abnormal breakpoints. By setting an abnormal breakpoint , when an exception that needs to be intercepted occurs in the program, the abnormal line will be automatically located.
As shown in Figure 6.6, click the + sign to add Java Exception Breakpoints and add exception breakpoints. Then enter the exception class that needs a breakpoint, as shown in Figure 6.7, and then you can see the added exception breakpoint in Java Exception Breakpoints.
I added a NullPointerException breakpoint here, as shown in Figure 6.8. After a null pointer exception occurs, it is automatically located in the null pointer exception line.
Under normal circumstances, we debug in a thread, step by step. But sometimes you will find that when you want to initiate another request during Debugging, it is impossible to proceed?
That's because IDEA's default blocking level during Debug is ALL, which will block other threads, and will only go to other threads when the current debugging thread is finished. You can select Thread in View Breakpoints, as shown in Figure 7.1, and then click Make Default to set it as the default option.
Switch thread. In the drop-down list of Frames in Figure 7.2, you can switch the current thread. As shown below, I have two Debug threads. Switching the other one will enter another Debug thread.
When debugging, do you want to go through the process again without initiating a request again?
1. First understand the method call stack , as shown in Figure 8.1, first request to enter the insertDemo method of DemoController, and then call the insert method. We don't care about other invokes. The top method is the method where the current breakpoint is located.
2. Breakpoint rollback
The so-called breakpoint rollback is actually to roll back to the beginning of the previous method call. In IDEA , the test cannot roll back line by line or return to the previous breakpoint, but return to the previous method.
There are two ways to roll back, one is the Drop Frame button (Figure 8.2), which is gradually rolled back according to the called method, including other methods of the tripartite class library (cancel the Show All Frames button, and the tripartite class library methods will be displayed, as shown in the figure. 8.3).
In the second way, select the method to be rolled back on the call stack method, right-click and select Drop Frame (Figure 8.4), go back to the last method call of the method, and then press F9 (Resume Program) again, you can see The program enters the breakpoint of the method.
But one thing to note is that the breakpoint rollback can only go through the process again. Some parameters/data states that have changed before cannot be rolled back to the previous state, such as objects, collections, and updated database data Wait.
Want to interrupt the request during Debug, don't go through the rest of the process?
Sometimes, after seeing that the input parameters are wrong, we don't want to go through the following process. How can we interrupt this request (the following process will delete the database data...)? Should we close the service and restart the program? Well, I did that before.
To be precise, I have not found a way to directly interrupt the request (except to close the service), but Force Return, that is, forced return, can be used to avoid the subsequent process, as shown in Figure 9.1.
Click Force Return, and the Return Value window pops up. The return type of my method is Map. Therefore, I return results directly to force the return, so that no subsequent processes will be performed. Or you can new HashMap<>().
At present, I have been using JRebel as a hot deployment tool, and the effect is fairly satisfactory. Modifying Java code or xml configuration files can be hot updated. Occasionally, the hot update does not take effect when the service has been opened for a long time or there are many changed files, and the service needs to be restarted.
Here is just a brief description of a free (non-cracked) way of obtaining permanent usage rights I saw on the Internet. I'm not sure when this method can't be used.
If there is anything wrong or a better way, please leave a message!
Recently, I organize personal study notes into a book and share them in PDF. Follow me, reply to the following code, you can get the Baidu disk address, no routine to receive! • 001: "Java Concurrency and High Concurrency Solutions" study notes; • 002: "Deep JVM Kernel-Principles, Diagnosis and Optimization" study notes; • 003: "Java Interview Collection" • 004: "Docker Open Source Book" • 005: "Kubernetes Open Source Book" • 006: "DDD Crash (Domain Driven Design Crash)" • 007: All • 008: Add technical discussion group
• spike combat system architecture and analysis • optimize the Boot your the Spring • 7 Ge IntelliJ IDEA plug-ins necessary to improve the coding efficiency • Goodbye micro-services, from more than 100 questions to a child superstar • system is running slow, CPU 100%, Full Too many GC times, this trick will help you get it all! • 8 common SQL misuses