Skip to content

Commit b7ae22a

Browse files
committed
Replace asserts with in terminal model
Make the checks happen always and throw detailed IllegalArgumentException in case of problematic parameter value.
1 parent 0efa2ef commit b7ae22a

File tree

5 files changed

+81
-88
lines changed

5 files changed

+81
-88
lines changed

terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/SnapshotChanges.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2007, 2018 Wind River Systems, Inc. and others.
2+
* Copyright (c) 2007, 2026 Wind River Systems, Inc. and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -66,15 +66,6 @@ public SnapshotChanges(int windowStart, int windowSize) {
6666

6767
}
6868

69-
/**
70-
* This is used in asserts to throw an {@link RuntimeException}.
71-
* This is useful for tests.
72-
* @return never -- throws an exception
73-
*/
74-
private boolean throwRuntimeException() {
75-
throw new RuntimeException();
76-
}
77-
7869
/**
7970
* @param line
8071
* @param size
@@ -299,7 +290,9 @@ private void doNotTrackScrollingAnymore() {
299290
* @param shift must be negative!
300291
*/
301292
private void scrollChangesLinesWithNegativeShift(int line, int n, int shift) {
302-
assert shift < 0 || throwRuntimeException();
293+
if (shift >= 0) {
294+
throw new IllegalArgumentException("Shift parameter value must be negative, actual value:" + shift); //$NON-NLS-1$
295+
}
303296
// scroll the region
304297
// don't run out of bounds!
305298
int m = Math.min(line + n + shift, getChangedLineLength() + shift);

terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataFastScroll.java

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2007, 2018 Wind River Systems, Inc. and others.
2+
* Copyright (c) 2007, 2026 Wind River Systems, Inc. and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -37,7 +37,7 @@ public TerminalTextDataFastScroll(ITerminalTextData data, int maxHeight) {
3737
fData = data;
3838
fData.setDimensions(maxHeight, fData.getWidth());
3939
if (maxHeight > 2) {
40-
assert shiftOffset(-2) || throwRuntimeException();
40+
moveOffset(-2);
4141
}
4242
}
4343

@@ -49,15 +49,6 @@ public TerminalTextDataFastScroll() {
4949
this(new TerminalTextDataStore(), 1);
5050
}
5151

52-
/**
53-
* This is used in asserts to throw an {@link RuntimeException}.
54-
* This is useful for tests.
55-
* @return never -- throws an exception
56-
*/
57-
private boolean throwRuntimeException() {
58-
throw new RuntimeException();
59-
}
60-
6152
/**
6253
*
6354
* @param line
@@ -72,21 +63,14 @@ int getPositionOfLine(int line) {
7263
* @param delta
7364
*/
7465
void moveOffset(int delta) {
75-
assert Math.abs(delta) < fMaxHeight || throwRuntimeException();
66+
if (Math.abs(delta) >= fMaxHeight) {
67+
throw new IllegalArgumentException(
68+
"Parameter 'delta' absolute value (" + delta + ") must be less than maxHeight(" + fMaxHeight + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
69+
}
7670
fOffset = (fMaxHeight + fOffset + delta) % fMaxHeight;
7771

7872
}
7973

80-
/**
81-
* Test method to shift the offset for testing (if assert ==true)
82-
* @param shift TODO
83-
* @return true
84-
*/
85-
private boolean shiftOffset(int shift) {
86-
moveOffset(shift);
87-
return true;
88-
}
89-
9074
@Override
9175
public void addLine() {
9276
if (getHeight() < fMaxHeight) {
@@ -117,21 +101,25 @@ public void copyLine(ITerminalTextData source, int sourceLine, int destLine) {
117101

118102
@Override
119103
public void copyRange(ITerminalTextData source, int sourceStartLine, int destStartLine, int length) {
120-
assert (destStartLine >= 0 && destStartLine + length <= fHeight) || throwRuntimeException();
104+
if (destStartLine < 0 || destStartLine + length > fHeight) {
105+
throw new IllegalArgumentException(
106+
"Value of 'destStartLine'+'length' parameters must be valid line (range [0-" //$NON-NLS-1$
107+
+ getHeight() + "). Parameter values: 'destStartLine'=" + destStartLine + ", 'size'=" //$NON-NLS-1$//$NON-NLS-2$
108+
+ length);
109+
}
121110
for (int i = 0; i < length; i++) {
122111
fData.copyLine(source, i + sourceStartLine, getPositionOfLine(i + destStartLine));
123112
}
124113
}
125114

126115
@Override
127116
public char getChar(int line, int column) {
128-
assert (line >= 0 && line < fHeight) || throwRuntimeException();
117+
validateLineParameter(line);
129118
return fData.getChar(getPositionOfLine(line), column);
130119
}
131120

132121
@Override
133122
public char[] getChars(int line) {
134-
assert (line >= 0 && line < fHeight) || throwRuntimeException();
135123
return fData.getChars(getPositionOfLine(line));
136124
}
137125

@@ -142,7 +130,7 @@ public int getHeight() {
142130

143131
@Override
144132
public LineSegment[] getLineSegments(int line, int startCol, int numberOfCols) {
145-
assert (line >= 0 && line < fHeight) || throwRuntimeException();
133+
validateLineParameter(line);
146134
return fData.getLineSegments(getPositionOfLine(line), startCol, numberOfCols);
147135
}
148136

@@ -153,13 +141,13 @@ public int getMaxHeight() {
153141

154142
@Override
155143
public TerminalStyle getStyle(int line, int column) {
156-
assert (line >= 0 && line < fHeight) || throwRuntimeException();
144+
validateLineParameter(line);
157145
return fData.getStyle(getPositionOfLine(line), column);
158146
}
159147

160148
@Override
161149
public TerminalStyle[] getStyles(int line) {
162-
assert (line >= 0 && line < fHeight) || throwRuntimeException();
150+
validateLineParameter(line);
163151
return fData.getStyles(getPositionOfLine(line));
164152
}
165153

@@ -181,7 +169,10 @@ private void cleanLines(int line, int len) {
181169

182170
@Override
183171
public void scroll(int startLine, int size, int shift) {
184-
assert (startLine >= 0 && startLine + size <= fHeight) || throwRuntimeException();
172+
if (startLine + size > getHeight()) {
173+
throw new IllegalArgumentException("Value of 'startLine'+'size' parameters must be valid line (range [0-" //$NON-NLS-1$
174+
+ getHeight() + "). Parameter values: 'startLine'=" + startLine + ", 'size'=" + size); //$NON-NLS-1$ //$NON-NLS-2$
175+
}
185176
if (shift >= fMaxHeight || -shift >= fMaxHeight) {
186177
cleanLines(startLine, fMaxHeight - startLine);
187178
return;
@@ -216,26 +207,30 @@ public void scroll(int startLine, int size, int shift) {
216207

217208
@Override
218209
public void setChar(int line, int column, char c, TerminalStyle style) {
219-
assert (line >= 0 && line < fHeight) || throwRuntimeException();
210+
validateLineParameter(line);
220211
fData.setChar(getPositionOfLine(line), column, c, style);
221212
}
222213

223214
@Override
224215
public void setChars(int line, int column, char[] chars, int start, int len, TerminalStyle style) {
225-
assert (line >= 0 && line < fHeight) || throwRuntimeException();
216+
validateLineParameter(line);
226217
fData.setChars(getPositionOfLine(line), column, chars, start, len, style);
227218
}
228219

229220
@Override
230221
public void setChars(int line, int column, char[] chars, TerminalStyle style) {
231-
assert (line >= 0 && line < fHeight) || throwRuntimeException();
222+
validateLineParameter(line);
232223
fData.setChars(getPositionOfLine(line), column, chars, style);
233224
}
234225

235226
@Override
236227
public void setDimensions(int height, int width) {
237-
assert height >= 0 || throwRuntimeException();
238-
assert width >= 0 || throwRuntimeException();
228+
if (height < 0) {
229+
throw new IllegalArgumentException("Parameter 'height' can't be negative value:" + height); //$NON-NLS-1$
230+
}
231+
if (width < 0) {
232+
throw new IllegalArgumentException("Parameter 'width' can't be negative value:" + width); //$NON-NLS-1$
233+
}
239234
if (height > fMaxHeight) {
240235
setMaxHeight(height);
241236
}
@@ -247,7 +242,10 @@ public void setDimensions(int height, int width) {
247242

248243
@Override
249244
public void setMaxHeight(int maxHeight) {
250-
assert maxHeight >= fHeight || throwRuntimeException();
245+
if (maxHeight < fHeight) {
246+
throw new IllegalArgumentException("Parameter 'maxHeight' (value '" + maxHeight //$NON-NLS-1$
247+
+ "') must't be less than 'fHeight' (value '" + fHeight + "')"); //$NON-NLS-1$ //$NON-NLS-2$
248+
}
251249
// move everything to offset0
252250
int start = getPositionOfLine(0);
253251
if (start != 0) {
@@ -264,7 +262,7 @@ public void setMaxHeight(int maxHeight) {
264262
}
265263
// copy the buffer back to our data
266264
fData.copy(buffer);
267-
shiftOffset(-start);
265+
moveOffset(-start);
268266
} else {
269267
fData.setDimensions(maxHeight, fData.getWidth());
270268
}
@@ -293,13 +291,20 @@ public void setCursorLine(int line) {
293291

294292
@Override
295293
public boolean isWrappedLine(int line) {
296-
assert (line >= 0 && line < fHeight) || throwRuntimeException();
294+
validateLineParameter(line);
297295
return fData.isWrappedLine(getPositionOfLine(line));
298296
}
299297

298+
private void validateLineParameter(int line) {
299+
if (line < 0 || line >= fHeight) {
300+
throw new IllegalArgumentException(
301+
"Parameter 'line' must be >= 0 and less than 'width' (current value '" + fHeight + "')"); //$NON-NLS-1$ //$NON-NLS-2$
302+
}
303+
}
304+
300305
@Override
301306
public void setWrappedLine(int line) {
302-
assert (line >= 0 && line < fHeight) || throwRuntimeException();
307+
validateLineParameter(line);
303308
fData.setWrappedLine(getPositionOfLine(line));
304309
}
305310

terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataSnapshot.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2007, 2018 Wind River Systems, Inc. and others.
2+
* Copyright (c) 2007, 2026 Wind River Systems, Inc. and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -64,15 +64,6 @@ class TerminalTextDataSnapshot implements ITerminalTextDataSnapshot {
6464
fInterestWindowSize = -1;
6565
}
6666

67-
/**
68-
* This is used in asserts to throw an {@link RuntimeException}.
69-
* This is useful for tests.
70-
* @return never -- throws an exception
71-
*/
72-
private boolean throwRuntimeException() {
73-
throw new RuntimeException();
74-
}
75-
7667
@Override
7768
public void detach() {
7869
fTerminal.removeSnapshot(this);
@@ -285,8 +276,12 @@ public int getInterestWindowStartLine() {
285276

286277
@Override
287278
public void setInterestWindow(int startLine, int size) {
288-
assert startLine >= 0 || throwRuntimeException();
289-
assert size >= 0 || throwRuntimeException();
279+
if (startLine < 0) {
280+
throw new IllegalArgumentException("Parameter 'startLine' can't be negative value:" + startLine); //$NON-NLS-1$
281+
}
282+
if (size < 0) {
283+
throw new IllegalArgumentException("Parameter 'size' can't be negative value:" + size); //$NON-NLS-1$
284+
}
290285
fInterestWindowStartLine = startLine;
291286
fInterestWindowSize = size;
292287
fSnapshot.setWindow(startLine, size);

terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataStore.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2007, 2018 Wind River Systems, Inc. and others.
2+
* Copyright (c) 2007, 2026 Wind River Systems, Inc. and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -41,15 +41,6 @@ public TerminalTextDataStore() {
4141
fWidth = 0;
4242
}
4343

44-
/**
45-
* This is used in asserts to throw an {@link RuntimeException}.
46-
* This is useful for tests.
47-
* @return never -- throws an exception
48-
*/
49-
private boolean throwRuntimeException() {
50-
throw new RuntimeException();
51-
}
52-
5344
@Override
5445
public int getWidth() {
5546
return fWidth;
@@ -62,8 +53,12 @@ public int getHeight() {
6253

6354
@Override
6455
public void setDimensions(int height, int width) {
65-
assert height >= 0 || throwRuntimeException();
66-
assert width >= 0 || throwRuntimeException();
56+
if (height < 0) {
57+
throw new IllegalArgumentException("Parameter 'height' can't be negative value:" + height); //$NON-NLS-1$
58+
}
59+
if (width < 0) {
60+
throw new IllegalArgumentException("Parameter 'width' can't be negative value:" + width); //$NON-NLS-1$
61+
}
6762
// just extend the region
6863
if (height > fChars.length) {
6964
int h = 4 * height / 3;
@@ -145,7 +140,10 @@ public LineSegment[] getLineSegments(int line, int column, int len) {
145140

146141
@Override
147142
public char getChar(int line, int column) {
148-
assert column < fWidth || throwRuntimeException();
143+
if (column >= fWidth) {
144+
throw new IllegalArgumentException(
145+
"Parameter 'column' must be >= 0 and less than 'width' (current value '" + fWidth + "')"); //$NON-NLS-1$ //$NON-NLS-2$
146+
}
149147
if (fChars[line] == null || column >= fChars[line].length) {
150148
return 0;
151149
}
@@ -154,7 +152,10 @@ public char getChar(int line, int column) {
154152

155153
@Override
156154
public TerminalStyle getStyle(int line, int column) {
157-
assert column < fWidth || throwRuntimeException();
155+
if (column >= fWidth) {
156+
throw new IllegalArgumentException(
157+
"Parameter 'column' must be >= 0 and less than 'width' (current value '" + fWidth + "')"); //$NON-NLS-1$ //$NON-NLS-2$
158+
}
158159
if (fStyle[line] == null || column >= fStyle[line].length) {
159160
return null;
160161
}
@@ -200,7 +201,10 @@ public void setChars(int line, int column, char[] chars, int start, int len, Ter
200201

201202
@Override
202203
public void scroll(int startLine, int size, int shift) {
203-
assert startLine + size <= getHeight() || throwRuntimeException();
204+
if (startLine + size > getHeight()) {
205+
throw new IllegalArgumentException("Value of 'startLine'+'size' parameters must be valid line (range [0-" //$NON-NLS-1$
206+
+ getHeight() + "). Parameter values: 'startLine'=" + startLine + ", 'size'=" + size); //$NON-NLS-1$ //$NON-NLS-2$
207+
}
204208
if (shift < 0) {
205209
// move the region up
206210
// shift is negative!!

terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/model/TerminalTextDataWindow.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2007, 2018 Wind River Systems, Inc. and others.
2+
* Copyright (c) 2007, 2026 Wind River Systems, Inc. and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -37,15 +37,6 @@ public TerminalTextDataWindow() {
3737
this(new TerminalTextDataStore());
3838
}
3939

40-
/**
41-
* This is used in asserts to throw an {@link RuntimeException}.
42-
* This is useful for tests.
43-
* @return never -- throws an exception
44-
*/
45-
private boolean throwRuntimeException() {
46-
throw new RuntimeException();
47-
}
48-
4940
/**
5041
* @param line
5142
* @return true if the line is within the window
@@ -161,7 +152,10 @@ public void copyLine(ITerminalTextData source, int sourceLine, int destLine) {
161152

162153
@Override
163154
public void scroll(int startLine, int size, int shift) {
164-
assert (startLine >= 0 && startLine + size <= fHeight) || throwRuntimeException();
155+
if (startLine < 0 || startLine + size > fHeight) {
156+
throw new IllegalArgumentException("Value of 'startLine'+'size' parameters must be valid line (range [0-" //$NON-NLS-1$
157+
+ fHeight + "). Parameter values: 'startLine'=" + startLine + ", 'size'=" + size); //$NON-NLS-1$ //$NON-NLS-2$
158+
}
165159
int n = size;
166160
int start = startLine - fWindowStartLine;
167161
// if start outside our range, cut the length to copy
@@ -202,7 +196,9 @@ public void setChars(int line, int column, char[] chars, TerminalStyle style) {
202196

203197
@Override
204198
public void setDimensions(int height, int width) {
205-
assert height >= 0 || throwRuntimeException();
199+
if (height < 0) {
200+
throw new IllegalArgumentException("Parameter 'height' can't be negative value:" + height); //$NON-NLS-1$
201+
}
206202
fData.setDimensions(fWindowSize, width);
207203
fHeight = height;
208204
}

0 commit comments

Comments
 (0)