Mikel
Mikel

Reputation: 23

Eclipse Java Debugger make if statement condition be true

I want to make a if statement become true only when I am debugging.

Example:

if(giveMyNumber() == 3){ 
     System.out.println("Is number 3");   
}

public int giveMyNumber(){
  return 1;
}

So i want to stop de debugger in if statement and simulate as (giveMyNumber() == 3) is true in order to go inside the if and print "Is number 3"

Upvotes: 2

Views: 5041

Answers (2)

Andreas
Andreas

Reputation: 159106

You can use the Eclipse feature called Force Return to return the value 3:

Forcing an early return from a non-void method requires an expression to be evaluated. For example, if a method was going to return false you could return a value of true by selecting an expression in the Display View and invoking Force Return. In the following example, elementCount is not equal to zero, and would return false (see debug hover showing the value of elementCount).

Force Return Example
(source: eclipse.org)

From the Display View, we could enter the value we want returned, select it and use the Force Return command to force the method isEmpty() to return with that value (in the following example we will force isEmpty() to return with the value true).

Force Return Action in the Display View
(source: eclipse.org)

Upvotes: 3

josivan
josivan

Reputation: 2063

You can use Conditional Breakpoint. Add a breakpoint and select Breakpoint Properties...

Take a look in image bellow.

Conditional Breakpoint

Select you breakpoint and write the condition, in your case:

giveMyNumber() == 3    

Upvotes: 0

Related Questions