Non-line dependent conditional breakpoints
Is it possible to set a breakpoint so that the program stops executing after an instruction has made certain condition true
?
A class has a variable named currency
and I want to make a breakpoint to make the program stop at any line after currency == 20
. I do not know the line number where currency
is changed, so this would 开发者_开发问答be like putting a breakpoint in every line of my class [ed. where currency
gets changed].
Is there any way to accomplish that (besides adding a breakpoint at each line)?
I am using NetBeans, but a solution in any other IDE is welcome.
Here is a link on setting conditional breakpoints in NetBeans: http://www.java-hair.com/?p=61
The relevant text:
In NetBeans, create a breakpoint, the right click on the little pink square that signifies the break. Click on “Customize”. When the customize dialog comes up, check “Condition” and fill in the condition. Above is an example of this dialog in NetBeans.
They also cover Eclipse and JDeveloper.
Edit: in response to your comment - no, this can't be done the way you want it. The way a debugger sets a breakpoint is by changing a byte in the instruction where the breakpoint is set. When the instruction is evaluated, the presence of that byte transfers control to the debugger, who replaces the byte with whatever was there before. When execution is resumed, the debugger moves the instruction pointer to the instruction where the breakpoint was set.
A conditional breakpoint on a single line is then easy to implement - when control transfers to the debugger, he simply checks the condition, and if it is not true then he resumes execution automatically.
So, how would it work if you didn't have to attach the conditional breakpoint to a line? It would have to be attached to every single line (just as you surmised you would need to do to achieve this effect otherwise). I haven't had the need to do this, but I imagine it would be undesirable as it would slow program execution considerably. A better approach might be to use your IDE to search through the code for all instances of currency to see where it might be set, and put conditional breakpoints around there.
Change the way you write software.
Rather than:
public class BreakEncapsulation {
private float currency = 0.0;
public BreakEncapsluation() {
this.currency += 5;
}
public void updateCurrency() {
this.currency = 20.0;
}
public void increaseCurrencyByRate() {
this.currency += 2;
}
public void decreaseCurrencyByRate() {
this.currency -= 2;
}
}
Do this:
public class HonourEncapsulation {
private float currency;
public HonourEncapsluation() {
setCurrency( getCurrency() + 5 );
}
public void updateCurrency() {
setCurrency( 20.0 );
}
public void increaseCurrencyByRate() {
setCurrency( getCurrency() + getRate() );
}
public void decreaseCurrencyByRate() {
setCurrency( getCurrency() - getRate() );
}
public void setCurrency( float currency ) {
System.out.println( "Old currency = " + this.currency );
// Set the break point here. This is the only spot in the entire system
// where the value of currency changes. This idea follows the DRY
// principle: every piece of information should have exactly one
// occurrence.
//
this.currency = currency;
System.out.println( "New currency = " + this.currency );
}
private float getCurrency() { return this.currency; }
private float getRate() { return 2.0; }
}
Not only does this help with maintaining your code (no code duplication), it keeps a world of possibilities open. It might not help you with your current issue, but it will help you avoid a number of problems in the future.
This is a technique called encapsulation, and is closely related to information hiding.
This is possible in both Intellj IDEA and Eclipse, along with other types of condtional break points. This functionality is a function of your IDE not the JVM.
In Eclipse it is possible to set a conditional breakpoint to be checked when a variable is changed.
It is not possible for general conditions, though, to be broken "the very moment this complex conditional becomes true"
精彩评论