debug an evaluate expression on intellij - java

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

Related

Debugging (setting break point) Ilog Jrules in evaluation part

I'm working with Jrules and ODM.
One of my evaluations fails due to a null pointer, and I want to know which particular condition failed.
How can I do this? The Jrules tutorials shows setting debug point in the action part, but not the evaluation (if...) part.
Is there a direct way to step condition by condition, so that I can locate which particular condition (evaluation) failed?
You can debug in the actions by putting some print statements in the initialaction and finalaction of each rules.
initialaction{
System.err.println("Debug here"+variable);
};
you can also add a sysout in the setter of the variable or loops used for actions and can see the system.out.log file inside sample server folder
(location: it depends in which drive we have installed the file) to trace what see what value is passing through that variable.
Thanks
Mohammed Khaleel

Can the execution point of a Java application be modified or specified while debugging under Android Studio

I am using Android Studio IDE (v1.5.1) and its Gradle debugger to step thru my Java application. I can single-step, step-over, step-out, break, set breakpoints etc., but I cannot find a way to manually set the next instruction/statement to be executed or alter the execution flow.
An example of this feature is Visual Studio's "Set next statement" under the DEBUG menu. Another example is MSDOS's g =address where you can specify the next instruction to be executed.
Does the Android Studio Debugger provide a means to change or specify the execution point of the target application?
While this is not possible, I usually workaround in the case of simple blocks of code that I might want (or not) to jump.
First you catch the block in a if statement:
int foo = bar.toFoo();
...
boolean doThis = Boolean.TRUE;
(B) if (doThis) {
...
// stuff that I might want to jump
...
}
Here (B) is a breakpoint. Now when it is reached, I just click Evaluate expression and, if needed, evaluate doThis = false;.
One might argue that a decent compiler should get rid of doThis, but it actually works in Android Studio 1.5, probably thanks to Boolean.TRUE instead of true.
If willing to, Evaluate expression will also let you execute full blocks of code while stuck at the breakpoint.

During debugging, how to break execution after a specific line

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.

Eclipse Breakpoint System.out

typically I set a breakpoint in my Java application when I want to observe the run.
However sometimes I just want to know if a method is called or not. Therefore a breakpoint does not help me, and I insert a "systrace" statement
System.out.println("method signature");
I thought it would be a nice feature If I could set a breakpoint and when the breakpoint is reached to just print out the systrace message and continue the run.
Do you know if this is possible?
You have to make it a conditional breakpoint with a following condition:
System.out.printf(Thread.currentThread().getStackTrace()[1].getMethodName() + "\n") == null
Works fine in my Eclipse. I'm using printf to make printing code evaluate to boolean. Not sure if there's a way to automate inserting this code into a breakpoint.

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/

Categories

Resources