Skip to content

Commit afe957c

Browse files
committed
8268698: Use Objects.check{Index,FromToIndex,FromIndexSize} for java.base
Reviewed-by: mchung, rriggs
1 parent a4e5f08 commit afe957c

File tree

40 files changed

+186
-284
lines changed

40 files changed

+186
-284
lines changed

src/java.base/share/classes/java/lang/AbstractStringBuilder.java

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.stream.IntStream;
3333
import java.util.stream.StreamSupport;
3434
import jdk.internal.util.ArraysSupport;
35+
import jdk.internal.util.Preconditions;
3536

3637
import static java.lang.String.COMPACT_STRINGS;
3738
import static java.lang.String.UTF16;
@@ -409,9 +410,7 @@ public int codePointAt(int index) {
409410
*/
410411
public int codePointBefore(int index) {
411412
int i = index - 1;
412-
if (i < 0 || i >= count) {
413-
throw new StringIndexOutOfBoundsException(index);
414-
}
413+
checkIndex(i, count);
415414
if (isLatin1()) {
416415
return value[i] & 0xff;
417416
}
@@ -505,9 +504,9 @@ public int offsetByCodePoints(int index, int codePointOffset) {
505504
*/
506505
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
507506
{
508-
checkRangeSIOOBE(srcBegin, srcEnd, count); // compatible to old version
507+
Preconditions.checkFromToIndex(srcBegin, srcEnd, count, Preconditions.SIOOBE_FORMATTER); // compatible to old version
509508
int n = srcEnd - srcBegin;
510-
checkRange(dstBegin, dstBegin + n, dst.length);
509+
Preconditions.checkFromToIndex(dstBegin, dstBegin + n, dst.length, Preconditions.IOOBE_FORMATTER);
511510
if (isLatin1()) {
512511
StringLatin1.getChars(value, srcBegin, srcEnd, dst, dstBegin);
513512
} else {
@@ -677,7 +676,7 @@ public AbstractStringBuilder append(CharSequence s, int start, int end) {
677676
if (s == null) {
678677
s = "null";
679678
}
680-
checkRange(start, end, s.length());
679+
Preconditions.checkFromToIndex(start, end, s.length(), Preconditions.IOOBE_FORMATTER);
681680
int len = end - start;
682681
ensureCapacityInternal(count + len);
683682
if (s instanceof String) {
@@ -736,7 +735,7 @@ public AbstractStringBuilder append(char[] str) {
736735
*/
737736
public AbstractStringBuilder append(char[] str, int offset, int len) {
738737
int end = offset + len;
739-
checkRange(offset, end, str.length);
738+
Preconditions.checkFromToIndex(offset, end, str.length, Preconditions.IOOBE_FORMATTER);
740739
ensureCapacityInternal(count + len);
741740
appendChars(str, offset, end);
742741
return this;
@@ -914,7 +913,7 @@ public AbstractStringBuilder delete(int start, int end) {
914913
if (end > count) {
915914
end = count;
916915
}
917-
checkRangeSIOOBE(start, end, count);
916+
Preconditions.checkFromToIndex(start, end, count, Preconditions.SIOOBE_FORMATTER);
918917
int len = end - start;
919918
if (len > 0) {
920919
shift(end, -len);
@@ -997,7 +996,7 @@ public AbstractStringBuilder replace(int start, int end, String str) {
997996
if (end > count) {
998997
end = count;
999998
}
1000-
checkRangeSIOOBE(start, end, count);
999+
Preconditions.checkFromToIndex(start, end, count, Preconditions.SIOOBE_FORMATTER);
10011000
int len = str.length();
10021001
int newCount = count + len - (end - start);
10031002
ensureCapacityInternal(newCount);
@@ -1067,7 +1066,7 @@ public CharSequence subSequence(int start, int end) {
10671066
* greater than {@code end}.
10681067
*/
10691068
public String substring(int start, int end) {
1070-
checkRangeSIOOBE(start, end, count);
1069+
Preconditions.checkFromToIndex(start, end, count, Preconditions.SIOOBE_FORMATTER);
10711070
if (isLatin1()) {
10721071
return StringLatin1.newString(value, start, end - start);
10731072
}
@@ -1104,7 +1103,7 @@ public AbstractStringBuilder insert(int index, char[] str, int offset,
11041103
int len)
11051104
{
11061105
checkOffset(index, count);
1107-
checkRangeSIOOBE(offset, offset + len, str.length);
1106+
Preconditions.checkFromToIndex(offset, offset + len, str.length, Preconditions.SIOOBE_FORMATTER);
11081107
ensureCapacityInternal(count + len);
11091108
shift(index, len);
11101109
count += len;
@@ -1292,7 +1291,7 @@ public AbstractStringBuilder insert(int dstOffset, CharSequence s,
12921291
s = "null";
12931292
}
12941293
checkOffset(dstOffset, count);
1295-
checkRange(start, end, s.length());
1294+
Preconditions.checkFromToIndex(start, end, s.length(), Preconditions.IOOBE_FORMATTER);
12961295
int len = end - start;
12971296
ensureCapacityInternal(count + len);
12981297
shift(dstOffset, len);
@@ -1795,20 +1794,4 @@ private final void appendChars(CharSequence s, int off, int end) {
17951794
}
17961795
count += end - off;
17971796
}
1798-
1799-
/* IndexOutOfBoundsException, if out of bounds */
1800-
private static void checkRange(int start, int end, int len) {
1801-
if (start < 0 || start > end || end > len) {
1802-
throw new IndexOutOfBoundsException(
1803-
"start " + start + ", end " + end + ", length " + len);
1804-
}
1805-
}
1806-
1807-
/* StringIndexOutOfBoundsException, if out of bounds */
1808-
private static void checkRangeSIOOBE(int start, int end, int len) {
1809-
if (start < 0 || start > end || end > len) {
1810-
throw new StringIndexOutOfBoundsException(
1811-
"start " + start + ", end " + end + ", length " + len);
1812-
}
1813-
}
18141797
}

src/java.base/share/classes/java/lang/Character.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.HashMap;
3535
import java.util.Locale;
3636
import java.util.Map;
37+
import java.util.Objects;
3738
import java.util.Optional;
3839

3940
import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
@@ -9249,10 +9250,7 @@ static void toSurrogates(int codePoint, char[] dst, int index) {
92499250
* @since 1.5
92509251
*/
92519252
public static int codePointCount(CharSequence seq, int beginIndex, int endIndex) {
9252-
int length = seq.length();
9253-
if (beginIndex < 0 || endIndex > length || beginIndex > endIndex) {
9254-
throw new IndexOutOfBoundsException();
9255-
}
9253+
Objects.checkFromToIndex(beginIndex, endIndex, seq.length());
92569254
int n = endIndex - beginIndex;
92579255
for (int i = beginIndex; i < endIndex; ) {
92589256
if (isHighSurrogate(seq.charAt(i++)) && i < endIndex &&
@@ -9284,9 +9282,7 @@ public static int codePointCount(CharSequence seq, int beginIndex, int endIndex)
92849282
* @since 1.5
92859283
*/
92869284
public static int codePointCount(char[] a, int offset, int count) {
9287-
if (count > a.length - offset || offset < 0 || count < 0) {
9288-
throw new IndexOutOfBoundsException();
9289-
}
9285+
Objects.checkFromIndexSize(count, offset, a.length);
92909286
return codePointCountImpl(a, offset, count);
92919287
}
92929288

src/java.base/share/classes/java/lang/Integer.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,8 @@ public static int parseInt(String s, int radix)
709709
public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
710710
throws NumberFormatException {
711711
Objects.requireNonNull(s);
712+
Objects.checkFromToIndex(beginIndex, endIndex, s.length());
712713

713-
if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
714-
throw new IndexOutOfBoundsException();
715-
}
716714
if (radix < Character.MIN_RADIX) {
717715
throw new NumberFormatException("radix " + radix +
718716
" less than Character.MIN_RADIX");
@@ -892,10 +890,8 @@ public static int parseUnsignedInt(String s, int radix)
892890
public static int parseUnsignedInt(CharSequence s, int beginIndex, int endIndex, int radix)
893891
throws NumberFormatException {
894892
Objects.requireNonNull(s);
893+
Objects.checkFromToIndex(beginIndex, endIndex, s.length());
895894

896-
if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
897-
throw new IndexOutOfBoundsException();
898-
}
899895
int start = beginIndex, len = endIndex - beginIndex;
900896

901897
if (len > 0) {

src/java.base/share/classes/java/lang/Long.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -752,10 +752,8 @@ public static long parseLong(String s, int radix)
752752
public static long parseLong(CharSequence s, int beginIndex, int endIndex, int radix)
753753
throws NumberFormatException {
754754
Objects.requireNonNull(s);
755+
Objects.checkFromToIndex(beginIndex, endIndex, s.length());
755756

756-
if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
757-
throw new IndexOutOfBoundsException();
758-
}
759757
if (radix < Character.MIN_RADIX) {
760758
throw new NumberFormatException("radix " + radix +
761759
" less than Character.MIN_RADIX");
@@ -998,10 +996,8 @@ public static long parseUnsignedLong(String s, int radix)
998996
public static long parseUnsignedLong(CharSequence s, int beginIndex, int endIndex, int radix)
999997
throws NumberFormatException {
1000998
Objects.requireNonNull(s);
999+
Objects.checkFromToIndex(beginIndex, endIndex, s.length());
10011000

1002-
if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
1003-
throw new IndexOutOfBoundsException();
1004-
}
10051001
int start = beginIndex, len = endIndex - beginIndex;
10061002

10071003
if (len > 0) {

src/java.base/share/classes/java/lang/String.java

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.util.stream.Stream;
5252
import java.util.stream.StreamSupport;
5353

54+
import jdk.internal.util.Preconditions;
5455
import jdk.internal.vm.annotation.ForceInline;
5556
import jdk.internal.vm.annotation.IntrinsicCandidate;
5657
import jdk.internal.vm.annotation.Stable;
@@ -1571,9 +1572,7 @@ public int codePointAt(int index) {
15711572
*/
15721573
public int codePointBefore(int index) {
15731574
int i = index - 1;
1574-
if (i < 0 || i >= length()) {
1575-
throw new StringIndexOutOfBoundsException(index);
1576-
}
1575+
checkIndex(i, length());
15771576
if (isLatin1()) {
15781577
return (value[i] & 0xff);
15791578
}
@@ -1602,10 +1601,7 @@ public int codePointBefore(int index) {
16021601
* @since 1.5
16031602
*/
16041603
public int codePointCount(int beginIndex, int endIndex) {
1605-
if (beginIndex < 0 || beginIndex > endIndex ||
1606-
endIndex > length()) {
1607-
throw new IndexOutOfBoundsException();
1608-
}
1604+
Objects.checkFromToIndex(beginIndex, endIndex, length());
16091605
if (isLatin1()) {
16101606
return endIndex - beginIndex;
16111607
}
@@ -4556,21 +4552,15 @@ boolean isLatin1() {
45564552
* negative or greater than or equal to {@code length}.
45574553
*/
45584554
static void checkIndex(int index, int length) {
4559-
if (index < 0 || index >= length) {
4560-
throw new StringIndexOutOfBoundsException("index " + index +
4561-
", length " + length);
4562-
}
4555+
Preconditions.checkIndex(index, length, Preconditions.SIOOBE_FORMATTER);
45634556
}
45644557

45654558
/*
45664559
* StringIndexOutOfBoundsException if {@code offset}
45674560
* is negative or greater than {@code length}.
45684561
*/
45694562
static void checkOffset(int offset, int length) {
4570-
if (offset < 0 || offset > length) {
4571-
throw new StringIndexOutOfBoundsException("offset " + offset +
4572-
", length " + length);
4573-
}
4563+
Preconditions.checkFromToIndex(offset, length, length, Preconditions.SIOOBE_FORMATTER);
45744564
}
45754565

45764566
/*
@@ -4582,10 +4572,7 @@ static void checkOffset(int offset, int length) {
45824572
* or {@code offset} is greater than {@code length - count}
45834573
*/
45844574
static void checkBoundsOffCount(int offset, int count, int length) {
4585-
if (offset < 0 || count < 0 || offset > length - count) {
4586-
throw new StringIndexOutOfBoundsException(
4587-
"offset " + offset + ", count " + count + ", length " + length);
4588-
}
4575+
Preconditions.checkFromIndexSize(offset, count, length, Preconditions.SIOOBE_FORMATTER);
45894576
}
45904577

45914578
/*
@@ -4597,10 +4584,7 @@ static void checkBoundsOffCount(int offset, int count, int length) {
45974584
* {@code end}, or {@code end} is greater than {@code length}.
45984585
*/
45994586
static void checkBoundsBeginEnd(int begin, int end, int length) {
4600-
if (begin < 0 || begin > end || end > length) {
4601-
throw new StringIndexOutOfBoundsException(
4602-
"begin " + begin + ", end " + end + ", length " + length);
4603-
}
4587+
Preconditions.checkFromToIndex(begin, end, length, Preconditions.SIOOBE_FORMATTER);
46044588
}
46054589

46064590
/**

src/java.base/share/classes/java/lang/StringLatin1.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@
3939

4040
import static java.lang.String.LATIN1;
4141
import static java.lang.String.UTF16;
42+
import static java.lang.String.checkIndex;
4243
import static java.lang.String.checkOffset;
4344

4445
final class StringLatin1 {
4546

4647
public static char charAt(byte[] value, int index) {
47-
if (index < 0 || index >= value.length) {
48-
throw new StringIndexOutOfBoundsException(index);
49-
}
48+
checkIndex(index, value.length);
5049
return (char)(value[index] & 0xff);
5150
}
5251

src/java.base/share/classes/java/lang/constant/MethodTypeDescImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.security.PrivilegedAction;
3131
import java.util.Arrays;
3232
import java.util.List;
33+
import java.util.Objects;
3334

3435
import static java.util.Objects.requireNonNull;
3536

@@ -113,8 +114,9 @@ public MethodTypeDesc changeParameterType(int index, ClassDesc paramType) {
113114

114115
@Override
115116
public MethodTypeDesc dropParameterTypes(int start, int end) {
116-
if (start < 0 || start >= argTypes.length || end < 0 || end > argTypes.length || start > end)
117-
throw new IndexOutOfBoundsException();
117+
Objects.checkIndex(start, argTypes.length);
118+
Objects.checkFromToIndex(start, end, argTypes.length);
119+
118120
ClassDesc[] newArgs = new ClassDesc[argTypes.length - (end - start)];
119121
System.arraycopy(argTypes, 0, newArgs, 0, start);
120122
System.arraycopy(argTypes, end, newArgs, start, argTypes.length - end);

src/java.base/share/classes/java/lang/invoke/AbstractConstantGroup.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
import java.util.*;
2929
import jdk.internal.vm.annotation.Stable;
3030

31-
import static java.lang.invoke.MethodHandleStatics.rangeCheck1;
32-
import static java.lang.invoke.MethodHandleStatics.rangeCheck2;
33-
3431
/** Utility class for implementing ConstantGroup. */
3532
/*non-public*/
3633
abstract class AbstractConstantGroup implements ConstantGroup {
@@ -119,11 +116,11 @@ static class SubGroup extends AbstractConstantGroup {
119116
super(end - start);
120117
this.self = self;
121118
this.offset = start;
122-
rangeCheck2(start, end, size);
119+
Objects.checkFromToIndex(start, end, size);
123120
}
124121

125122
private int mapIndex(int index) {
126-
return rangeCheck1(index, size) + offset;
123+
return Objects.checkIndex(index, size) + offset;
127124
}
128125

129126
@Override
@@ -143,7 +140,7 @@ public boolean isPresent(int index) {
143140

144141
@Override
145142
public ConstantGroup subGroup(int start, int end) {
146-
rangeCheck2(start, end, size);
143+
Objects.checkFromToIndex(start, end, size);
147144
return new SubGroup(self, offset + start, offset + end);
148145
}
149146

@@ -160,7 +157,7 @@ public List<Object> asList(Object ifNotPresent) {
160157
@Override
161158
public int copyConstants(int start, int end,
162159
Object[] buf, int pos) throws LinkageError {
163-
rangeCheck2(start, end, size);
160+
Objects.checkFromToIndex(start, end, size);
164161
return self.copyConstants(offset + start, offset + end,
165162
buf, pos);
166163
}
@@ -169,7 +166,7 @@ public int copyConstants(int start, int end,
169166
public int copyConstants(int start, int end,
170167
Object[] buf, int pos,
171168
Object ifNotPresent) {
172-
rangeCheck2(start, end, size);
169+
Objects.checkFromToIndex(start, end, size);
173170
return self.copyConstants(offset + start, offset + end,
174171
buf, pos, ifNotPresent);
175172
}
@@ -189,7 +186,7 @@ private AsList(ConstantGroup self, int start, int end,
189186
this.offset = start;
190187
this.resolving = resolving;
191188
this.ifNotPresent = ifNotPresent;
192-
rangeCheck2(start, end, self.size());
189+
Objects.checkFromToIndex(start, end, self.size());
193190
}
194191
AsList(ConstantGroup self, int start, int end) {
195192
this(self, start, end, true, null);
@@ -200,7 +197,7 @@ private AsList(ConstantGroup self, int start, int end,
200197
}
201198

202199
private int mapIndex(int index) {
203-
return rangeCheck1(index, size) + offset;
200+
return Objects.checkIndex(index, size) + offset;
204201
}
205202

206203
@Override public final int size() {
@@ -223,7 +220,7 @@ public Iterator<Object> iterator() {
223220
}
224221

225222
@Override public List<Object> subList(int start, int end) {
226-
rangeCheck2(start, end, size);
223+
Objects.checkFromToIndex(start, end, size);
227224
return new AsList(self, offset + start, offset + end,
228225
resolving, ifNotPresent);
229226
}

0 commit comments

Comments
 (0)