During debugging, how to break execution after a specific line - java

I'm debugging a program and I have this line:
if (compareToName.equals(className)) {
Set<String> properties =
reflections.getResources(Pattern.compile(".*\\.properties"));
}
I have a breakpoint in the line that starts with Set<String>...
I want to know the value of properties after the execution of the line, but when I step over the line I'm over the block so the properties variable is not available in the variables window.

You're not going to get the value immediately after you step over that assignment because the value doesn't exist outside of the if statement. IntelliJ's debugger is still bound by the same rules of scope and variable lifetimes as other Java programs.
While I'm leery of this particular code snippet, getting the value isn't hard to do while in debug mode.
Alt+Left Click while hovering over a particular snippet (in this case, you'd hover over the dot to the right of the reflections bit.
Highlight the expression, then select "Evaluate Expression" (using the little calculator icon in the debug window). By default it will prepopulate that field with anything that is highlighted so that you can execute an evaluation of it.

Press Alt-F7 and enter the code you want to execute.
In your example:
reflections.getResources(Pattern.compile(".*\\.properties"));
you will see the result.

Related

IDEA: Move caret to end of line after using shortcut to assign local variable?

On Mac, after you introduce a local variable by using the context menu or keyboard shortcut, the caret automatically goes to the end of the line so you can simply press enter and start typing on a new line. On Windows it seems to want to stay at the end of your variable name. I've tried to find this in the settings and Googled for it but can't find any resources on how to also do this on Windows.
As an example if I type something like new Object(); and ask IDEA to automatically introduce a local variable for it, it will automatically go to the variable declaration to name it. On Mac, your caret is moved to the end of this line after the semicolon once you are done naming the variable. On Windows, it is placed after the variable declaration. So if you ended up having a line such as Object someObject = new Object();, the caret is placed at the beginning of the variable assignment, after the equals sign. Hitting enter will then just introduce a line break between the declaration and assignment.
It can fairly annoying to deal in situations where I need to introduce multiple variables with method calls.
Here's a gif just to illustrate what happens on Windows:
I'm aware that you could just hit END to go to the end of the line but the Mac implementation of this is massively more convenient. I assume it's hidden somewhere in the settings - but where?
As an alternative you can use SHIFT+ENTER and you will have pretty much the same result if I understand your issue corretly

debug an evaluate expression on intellij

Using evaluate expression/code fragment:
https://www.jetbrains.com/idea/help/evaluating-expressions.html
Is it possible to debug evaluated expression/code fragment on intellij?.
On eclipse if you launch a code evaluation on display window and that code has any breakpoint inside, eclipse debugger stops on that breakpoint. If you try again eclipse says it can execute inspections on nested debug session.
Intellij seems to launch expression in a different session.
My workflow on this is to stop on "whatever line" of code and add fragment I want to evaluate for a Q&D debug. Many times this leads to a debug restart.
Unfortunately it's not possible in Intellij 14 and stated in the official link you provided:
If a method invoked within the Expression Evaluation has a breakpoint inside its body, this breakpoint will be ignored.
To eliminate the problem you mentioned with frequent restarting of a debug session I use the following work-around with the drop-frame debug feature:
Step in to a method and before return use the drop-frame functionality to fall back to a previous stack frame. See the drop-frame icon's location on the screenshot below:
Now it's possible to rerun this method with different parameters without restarting the debug session (parameters can be set using Evaluate Expression dialog).
The feature is not available in IntelliJ IDEA 2019.2
The workaround I use is to update the code as follows,
Boolean shouldExecute = false;
if(shouldExecute){
//method call
}
During the debug session, I will change shouldExecute flag to true. This way I can debug the method call when needed.
Of coarse this is just a workaround, I need to remove this flag later.
Alt + F8 is the shortcut for evaluation of an expression or variable.
However, you can also select the variable, right click on it and evaluate as explained in this article.
Select the variable that you want to evaluate and press Alt + F8. Detail steps is given here:
https://www.websparrow.org/misc/shortcut-key-to-evaluate-expression-variable-in-intellij-idea

Start coding from the debugging point Eclipse

I have a code like this
String s="Test-Code-Data";
String[] splitedData = s.split("-");
I have a break point at second line. When my code reaches that point, can i start coding below that and immediately see the output. For example. I want to see the output from below code, and i type it when the execution reaches the second line only.
System.out.println(splitedData[1])
Is this possible in eclipse?
To answer your question, NO you cannot effectively alter code while the code is in execution without making Eclipse warn you that the code is 'out of sync'.
However, you can look at the actual values of your various variables in the various stages of execution of your code. And, once the execution is complete, then you can edit the code and Run the program in Debug again and keep going that way.
You can use the Inspect Variable feature to look at the value of a variable while in execution and paused at a breakpoint by selecting the variable you'd like to inspect and using the keyboard shortcut Ctrl+Shift+I to get the value of the variable.

Debugging console input using NetBeans

Is there a way in NetBeans that while you are debugging a Java program to modify or check the value that a function or variable returns. The same way you can use the console in Matlab.
I'm not speaking about the usual debugging tools variable windows etc.
Example I want to break at a method in car class and input
>car.getMileage()
and get..
>car.getMileage()
>2500
or
>car.setMileage(100)
>car.getMileage()
>100
In Netbeans there is a tab under your source code window (there by default I think) called Variables. In that window you can edit the Value field of any variable that is in scope while suspended at a breakpoint. This value should update for the java application as you change it in real time. You can invoke methods the same way, by adding a watch. Like say you had a static method getInt(); which returns some value. Just make a watch for getInt(), and the Value column will show you the return value. So for your example, make a watch for car.setMileage(100) after your breakpoint is hit. The value column will likely be 'void'. Then make another watch for car.getMileage(). 100 should be returned.
Use an IDE such as Eclipse. You can set breakpoints, set statements and execute them. This is a feature of most modern IDEs actually.
More info on the display view can be found here : http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jdt.doc.user/reference/views/debug/ref-debug_view.htm
For a nice overview of the debugging features of Eclipse, check out this post : http://www.cavdar.net/2008/09/13/5-tips-for-debugging-java-code-in-eclipse/

Inspect function of eclipse tricks me like a dummy, why does it seem to be that tricky while debugging?

Eclipse is awesome for writing java programs but today I find that it's awesome to trick new coders like me. #_#
I write a snippet as following,
public static void main(String[] args) {
for(int i=0;i<3;i++){
System.out.println("i = " + i);
}
}
then I add a breakpoint at the line of "System.out.print....", click "Debug" button, eclipse goes to the "Debug" perspective and the line of breakpoint is highlighted, then I move the cursor over variable "i", its value is "0" as expected.
And then, I select the "i++" and click "Inspect"(or press "Ctrl+Shift+I") once, I move the cursor over variable "i", its value changed to "1". I repeat "Inspect" again, the value of i changed to "2"......(its value will add by 1 every time I clicked the "Inspect")!!
Why does this happend!? I ONLY want to watch the value of "i" for debug propuse, DO NOT want to really change its value until I step into next statement. I think that "Inspect", as well as "Display" are only for viewing the variable/expression, they should not impact the value, but in this case, it doesn't work as I expect to.
Could anyone tell me what went wrong?
My eclipse version info:
Version: Indigo Service Release 2
Build id: 20120216-1857
If you inspect an expression, eclipse has to execute that expression so you can get the value. Therefore, if you inspect i++, eclipse adds one to i.
Think about it this way: If instead of i++, you inspected myFunction(i), would you expect eclipse to execute the function "myFunction" to get the value? It's the same with i++.
If you are concerned about displaying/showing values while debugging and do you want to be sure not affecting the value, you should select the variable or expression and use the "Watch" option.
This will track the variable/expression value without executing, just updating the new value each time this is run. I think is the most secure way.
As Pablo mentioned, it has to evaluate the code in order to tell you what value it returns. You could instead put a watch on "i+1" and that would give you the value you want without the side effect you don't want.
Basically, you need to be aware of any side effects of anything you launch, whether from the "main" code or from the debugging tools. As Erhannis mentioned, this is very useful at times for modifying values while debugging your code. (For example, you can verify that a tweak/fix is indeed helpful before actually tweaking your code.)
You were expecting "an isolate area" but this would be extremely hard to do, especially in an object-oriented context where many objects are linked to many other objects. Running the whole thing in parallel might sometimes work, but you'd lose the tweaking ability above. And in any context, you'd run into tons of problems with fighting over resources; e.g. both copies trying to read/write a particular file such as a log file. Also, the two execution paths could diverge, leading to incorrect/misleading watch values.
So, preventing such side effects is not really a feasible option here and would rarely be useful anyway. Just expect that the watches can both reflect and affect the code execution.

Categories

Resources