Skip to content

Commit 39fb4e2

Browse files
committed
Code & tests refactoring
1 parent 88d904d commit 39fb4e2

File tree

2 files changed

+85
-61
lines changed

2 files changed

+85
-61
lines changed

org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/test/stepping/StatementSteppingTests.java

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.debug.test.stepping;
1515

16+
import org.eclipse.core.runtime.ISafeRunnable;
17+
import org.eclipse.debug.core.DebugEvent;
1618
import org.eclipse.debug.core.model.ILineBreakpoint;
1719
import org.eclipse.jdt.debug.core.IJavaStackFrame;
1820
import org.eclipse.jdt.debug.core.IJavaThread;
21+
import org.eclipse.jdt.debug.testplugin.DebugElementKindEventWaiter;
22+
import org.eclipse.jdt.debug.testplugin.DebugEventWaiter;
1923
import org.eclipse.jdt.debug.tests.AbstractDebugTest;
2024
import org.eclipse.jdt.internal.debug.ui.IJDIPreferencesConstants;
2125
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
2226
import org.eclipse.jface.preference.IPreferenceStore;
2327

2428
public class StatementSteppingTests extends AbstractDebugTest {
2529

26-
private boolean fOriginalState;
30+
private boolean originalPreferenceValue;
2731

2832
public StatementSteppingTests(String name) {
2933
super(name);
@@ -32,42 +36,52 @@ public StatementSteppingTests(String name) {
3236
@Override
3337
protected void setUp() throws Exception {
3438
super.setUp();
35-
fOriginalState = getPrefStore().getBoolean(IJDIPreferencesConstants.PREF_STATEMENT_LEVEL_STEPPING);
36-
if (!fOriginalState) {
39+
originalPreferenceValue = getPrefStore().getBoolean(IJDIPreferencesConstants.PREF_STATEMENT_LEVEL_STEPPING);
40+
if (!originalPreferenceValue) {
3741
getPrefStore().setValue(IJDIPreferencesConstants.PREF_STATEMENT_LEVEL_STEPPING, true);
3842
}
3943
}
4044

45+
@Override
46+
protected void tearDown() throws Exception {
47+
if (originalPreferenceValue != getPrefStore().getBoolean(IJDIPreferencesConstants.PREF_STATEMENT_LEVEL_STEPPING)) {
48+
getPrefStore().setValue(IJDIPreferencesConstants.PREF_STATEMENT_LEVEL_STEPPING, originalPreferenceValue);
49+
}
50+
super.tearDown();
51+
}
52+
4153
/**
4254
* Tests a step over with lots of arguments
4355
*/
4456
public void testLotsOfMultilineArguments() throws Exception {
4557

4658
String typeName = "StatementStep";
4759
ILineBreakpoint bp = createLineBreakpoint(21, typeName);
48-
IJavaThread thread = null;
60+
IJavaThread t = null;
4961
try {
50-
thread = launchToLineBreakpoint(typeName, bp, true);
62+
IJavaThread thread = launchToLineBreakpoint(typeName, bp, true);
63+
t = thread;
5164
IJavaStackFrame stackFrame = (IJavaStackFrame) thread.getTopStackFrame();
52-
thread.stepOver();
53-
Thread.sleep(1000);
65+
66+
runAndWaitForSuspendEvent(() -> thread.stepOver());
5467
assertEquals("Should be at line 26", 26, stackFrame.getLineNumber());
5568

56-
thread.stepInto();
57-
Thread.sleep(1000);
69+
runAndWaitForSuspendEvent(() -> thread.stepInto());
70+
5871
stackFrame = (IJavaStackFrame) thread.getTopStackFrame();
5972
assertEquals("Should be at line 36", 36, stackFrame.getLineNumber());
6073

61-
thread.stepReturn();
62-
Thread.sleep(1000);
74+
runAndWaitForSuspendEvent(() -> thread.stepReturn());
75+
6376
stackFrame = (IJavaStackFrame) thread.getTopStackFrame();
6477
assertEquals("Should be at line 22", 22, stackFrame.getLineNumber());
6578

6679
thread.stepOver();
67-
Thread.sleep(1000);
80+
runAndWaitForSuspendEvent(() -> thread.stepOver());
81+
6882
assertEquals("Should be at line 28", 28, stackFrame.getLineNumber());
6983
} finally {
70-
terminateAndRemove(thread);
84+
terminateAndRemove(t);
7185
removeAllBreakpoints();
7286
}
7387
}
@@ -76,20 +90,19 @@ public void testStepOverWithSingleMultilineArgument() throws Exception {
7690

7791
String typeName = "StatementStepArgument";
7892
ILineBreakpoint bp = createLineBreakpoint(28, typeName);
79-
IJavaThread thread = null;
93+
IJavaThread t = null;
8094
try {
81-
thread = launchToLineBreakpoint(typeName, bp, true);
95+
IJavaThread thread = launchToLineBreakpoint(typeName, bp, true);
96+
t = thread;
8297
IJavaStackFrame stackFrame = (IJavaStackFrame) thread.getTopStackFrame();
83-
thread.stepOver();
84-
Thread.sleep(1000);
98+
runAndWaitForSuspendEvent(() -> thread.stepOver());
8599
assertEquals("Should be at line 30", 30, stackFrame.getLineNumber());
86100

87-
thread.stepOver();
88-
Thread.sleep(1000);
101+
runAndWaitForSuspendEvent(() -> thread.stepOver());
89102
assertEquals("Should be at line 34", 34, stackFrame.getLineNumber());
90103

91104
} finally {
92-
terminateAndRemove(thread);
105+
terminateAndRemove(t);
93106
removeAllBreakpoints();
94107
}
95108
}
@@ -98,38 +111,33 @@ public void testStepOverWithNestedMultiLineArgs() throws Exception {
98111

99112
String typeName = "StatementStepNested";
100113
ILineBreakpoint bp = createLineBreakpoint(20, typeName);
101-
IJavaThread thread = null;
114+
IJavaThread t = null;
102115
try {
103-
thread = launchToLineBreakpoint(typeName, bp, true);
116+
IJavaThread thread = launchToLineBreakpoint(typeName, bp, true);
117+
t = thread;
104118
IJavaStackFrame stackFrame = (IJavaStackFrame) thread.getTopStackFrame();
105-
thread.stepOver();
106-
Thread.sleep(1000);
119+
runAndWaitForSuspendEvent(() -> thread.stepOver());
107120
assertEquals("Should be at line 27", 27, stackFrame.getLineNumber());
108121

109-
thread.stepInto();
110-
Thread.sleep(1000);
122+
runAndWaitForSuspendEvent(() -> thread.stepInto());
111123
stackFrame = (IJavaStackFrame) thread.getTopStackFrame();
112124
assertEquals("Should be at line 38", 38, stackFrame.getLineNumber());
113125

114-
thread.stepReturn();
115-
Thread.sleep(1000);
126+
runAndWaitForSuspendEvent(() -> thread.stepReturn());
116127
stackFrame = (IJavaStackFrame) thread.getTopStackFrame();
117128
assertEquals("Should be at line 26", 26, stackFrame.getLineNumber());
118129

119-
thread.stepOver();
120-
Thread.sleep(1000);
130+
runAndWaitForSuspendEvent(() -> thread.stepOver());
121131
assertEquals("Should be at line 24", 24, stackFrame.getLineNumber());
122132

123-
thread.stepOver();
124-
Thread.sleep(1000);
133+
runAndWaitForSuspendEvent(() -> thread.stepOver());
125134
assertEquals("Should be at line 22", 22, stackFrame.getLineNumber());
126135

127-
thread.stepOver();
128-
Thread.sleep(1000);
136+
runAndWaitForSuspendEvent(() -> thread.stepOver());
129137
assertEquals("Should be at line 34", 34, stackFrame.getLineNumber());
130138

131139
} finally {
132-
terminateAndRemove(thread);
140+
terminateAndRemove(t);
133141
removeAllBreakpoints();
134142
}
135143
}
@@ -141,11 +149,14 @@ protected IPreferenceStore getPrefStore() {
141149
return JDIDebugUIPlugin.getDefault().getPreferenceStore();
142150
}
143151

152+
private void runAndWaitForSuspendEvent(ISafeRunnable runnable) throws Exception {
153+
DebugEventWaiter waiter = new DebugElementKindEventWaiter(DebugEvent.SUSPEND, IJavaThread.class);
154+
runnable.run();
155+
waiter.waitForEvent();
156+
}
157+
144158
@Override
145-
protected void tearDown() throws Exception {
146-
super.tearDown();
147-
if (fOriginalState != getPrefStore().getBoolean(IJDIPreferencesConstants.PREF_STATEMENT_LEVEL_STEPPING)) {
148-
getPrefStore().setValue(IJDIPreferencesConstants.PREF_STATEMENT_LEVEL_STEPPING, fOriginalState);
149-
}
159+
protected boolean enableUIEventLoopProcessingInWaiter() {
160+
return true;
150161
}
151162
}

org.eclipse.jdt.debug/model/org/eclipse/jdt/internal/debug/core/model/JDIThread.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3123,35 +3123,48 @@ private boolean isSyntheticAndNotAvailable(Location currentLocation, Location pr
31233123
* if line information is not available
31243124
*/
31253125
private boolean skipImmediateInstructionsOnStepping(Location toBeStepped) throws AbsentInformationException {
3126-
if (fStepOverLocation != null || fOriginalStepLocation != null) {
3127-
byte[] codes = toBeStepped.method().bytecodes();
3128-
int currIndx = (int) toBeStepped.codeIndex();
3129-
if (currIndx == -1) {
3126+
if (fStepOverLocation == null && fOriginalStepLocation == null) {
3127+
return false;
3128+
}
3129+
int currIndx = (int) toBeStepped.codeIndex();
3130+
if (currIndx == -1) {
3131+
return false;
3132+
}
3133+
Method method = toBeStepped.method();
3134+
int currOpCode = bytecodeToOpcode(method.bytecodes()[currIndx]);
3135+
if (currOpCode < INTERMEDIATE_OPCODE_START || currOpCode > INTERMEDIATE_OPCODE_END) {
3136+
return false;
3137+
}
3138+
List<Location> locs = method.allLineLocations();
3139+
int index = locs.indexOf(toBeStepped);
3140+
if (index >= 0 && index < locs.size() - 1) {
3141+
Location nextLoc = locs.get(index + 1);
3142+
int nxtIndx = (int) nextLoc.codeIndex();
3143+
if (nxtIndx < 0) {
31303144
return false;
31313145
}
3132-
int currOpCode = codes[currIndx] & 0xFF;
3133-
3134-
if (currOpCode >= INTERMEDIATE_OPCODE_START && currOpCode <= INTERMEDIATE_OPCODE_END) {
3135-
Location nextLoc = null;
3136-
List<Location> locs = toBeStepped.method().allLineLocations();
3137-
int index = locs.indexOf(toBeStepped);
3138-
if (index >= 0 && index < locs.size() - 1) {
3139-
nextLoc = locs.get(index + 1);
3140-
int nxtIndx = (int) nextLoc.codeIndex();
3141-
if (nxtIndx == -1) {
3142-
return false;
3143-
}
3144-
int diff = nxtIndx - currIndx;
3145-
if (diff >= 1 && diff <= 3) {
3146-
return true;
3147-
}
3148-
}
3146+
int diff = nxtIndx - currIndx;
3147+
// TODO: explain condition below
3148+
if (diff >= 1 && diff <= 3) {
3149+
return true;
31493150
}
31503151
}
31513152
return false;
31523153
}
31533154
}
31543155

3156+
/**
3157+
* In Java, byte is a signed type with a range of -128 to 127. When we read a bytecode instruction
3158+
* from Method.bytecodes(), the raw byte value can be negative for opcodes &#8805; 0x80 (128).
3159+
* For example, opcode 0xC4 (wide) would be stored as -60 in a signed byte, but we need it as 196 to
3160+
* correctly compare it against opcode constants.
3161+
*
3162+
* @return the unsigned value in range 0–255
3163+
*/
3164+
private static int bytecodeToOpcode(byte b) {
3165+
return b & 0xFF;
3166+
}
3167+
31553168
/**
31563169
* Handler for step over requests.
31573170
*/

0 commit comments

Comments
 (0)