简体   繁体   English

Eclipse Java调试器使if语句条件为true

[英]Eclipse Java Debugger make if statement condition be true

I want to make a if statement become true only when I am debugging. 我只想在调试时使if语句变为true。

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" 所以我想在if语句中停止调试器并模拟为(giveMyNumber()== 3)true ,以便进入if并打印“ Is number 3”

You can use the Eclipse feature called Force Return to return the value 3 : 您可以使用称为“ 强制返回”的Eclipse功能来返回值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 . 例如,如果某个方法要返回false,则可以通过在Display View中选择一个表达式并调用Force Return来返回true值。 In the following example, elementCount is not equal to zero, and would return false (see debug hover showing the value of elementCount ). 在下面的示例中, elementCount不等于零,并且将返回false(请参阅调试悬停,其中显示elementCount的值)。

Force Return Example http://help.eclipse.org/mars/topic/org.eclipse.jdt.doc.user/reference/views/shared/images/force-return-code.png 强制返回示例http://help.eclipse.org/mars/topic/org.eclipse.jdt.doc.user/reference/views/shared/images/force-return-code.png

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 ). Display View中 ,我们可以输入要返回的值,选择它,然后使用Force Return命令强制isEmpty()方法返回该值(在下面的示例中,我们将强制isEmpty()返回该值true )。

Force Return Action in the Display View http://help.eclipse.org/mars/topic/org.eclipse.jdt.doc.user/reference/views/shared/images/force-return-action.png 在显示视图中强制返回操作http://help.eclipse.org/mars/topic/org.eclipse.jdt.doc.user/reference/views/shared/images/force-return-action.png

You can use Conditional Breakpoint. 您可以使用条件断点。 Add a breakpoint and select Breakpoint Properties... 添加断点并选择断点属性...

Take a look in image bellow. 看一下下面的图像。

条件断点

Select you breakpoint and write the condition, in your case: 在您的情况下,选择您的断点并编写条件:

giveMyNumber() == 3    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM