Skip to content

8353197: Document preconditions for JavaLangAccess methods #24982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/java.base/share/classes/java/io/DataInputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, Alibaba Group Holding Limited. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -595,11 +595,11 @@ public static final String readUTF(DataInput in) throws IOException {
int chararr_count=0;

in.readFully(bytearr, 0, utflen);
int ascii = JLA.countPositives(bytearr, 0, utflen);
int ascii = JLA.uncheckedCountPositives(bytearr, 0, utflen);
if (ascii == utflen) {
String str;
if (trusted) {
str = JLA.newStringNoRepl(bytearr, StandardCharsets.ISO_8859_1);
str = JLA.uncheckedNewStringNoRepl(bytearr, StandardCharsets.ISO_8859_1);
} else {
str = new String(bytearr, 0, utflen, StandardCharsets.ISO_8859_1);
}
Expand All @@ -621,7 +621,7 @@ public static final String readUTF(DataInput in) throws IOException {
}

if (ascii != 0) {
JLA.inflateBytesToChars(bytearr, 0, chararr, 0, ascii);
JLA.uncheckedInflateBytesToChars(bytearr, 0, chararr, 0, ascii);
count += ascii;
chararr_count += ascii;
}
Expand Down
6 changes: 3 additions & 3 deletions src/java.base/share/classes/java/io/ObjectInputStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -3569,7 +3569,7 @@ private String readUTFBody(long utflen) throws IOException {
if (utflen > 0 && utflen < Integer.MAX_VALUE) {
// Scan for leading ASCII chars
int avail = end - pos;
int ascii = JLA.countPositives(buf, pos, Math.min(avail, (int)utflen));
int ascii = JLA.uncheckedCountPositives(buf, pos, Math.min(avail, (int)utflen));
if (ascii == utflen) {
// Complete match, consume the buf[pos ... pos + ascii] range and return.
// Modified UTF-8 and ISO-8859-1 are both ASCII-compatible encodings bytes
Expand All @@ -3582,7 +3582,7 @@ private String readUTFBody(long utflen) throws IOException {
// Avoid allocating a StringBuilder if there's enough data in buf and
// cbuf is large enough
if (avail >= utflen && utflen <= CHAR_BUF_SIZE) {
JLA.inflateBytesToChars(buf, pos, cbuf, 0, ascii);
JLA.uncheckedInflateBytesToChars(buf, pos, cbuf, 0, ascii);
pos += ascii;
int cbufPos = readUTFSpan(ascii, utflen - ascii);
return new String(cbuf, 0, cbufPos);
Expand Down
18 changes: 9 additions & 9 deletions src/java.base/share/classes/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -2109,22 +2109,22 @@ public Stream<ModuleLayer> layers(ClassLoader loader) {
return ModuleLayer.layers(loader);
}

public int countPositives(byte[] bytes, int offset, int length) {
public int uncheckedCountPositives(byte[] bytes, int offset, int length) {
return StringCoding.countPositives(bytes, offset, length);
}
public int countNonZeroAscii(String s) {
return StringCoding.countNonZeroAscii(s);
}
public String newStringNoRepl(byte[] bytes, Charset cs) throws CharacterCodingException {
public String uncheckedNewStringNoRepl(byte[] bytes, Charset cs) throws CharacterCodingException {
return String.newStringNoRepl(bytes, cs);
}
public char getUTF16Char(byte[] bytes, int index) {
public char uncheckedGetUTF16Char(byte[] bytes, int index) {
return StringUTF16.getChar(bytes, index);
}
public void putCharUTF16(byte[] bytes, int index, int ch) {
public void uncheckedPutCharUTF16(byte[] bytes, int index, int ch) {
StringUTF16.putChar(bytes, index, ch);
}
public byte[] getBytesNoRepl(String s, Charset cs) throws CharacterCodingException {
public byte[] uncheckedGetBytesNoRepl(String s, Charset cs) throws CharacterCodingException {
return String.getBytesNoRepl(s, cs);
}

Expand All @@ -2136,15 +2136,15 @@ public byte[] getBytesUTF8NoRepl(String s) {
return String.getBytesUTF8NoRepl(s);
}

public void inflateBytesToChars(byte[] src, int srcOff, char[] dst, int dstOff, int len) {
public void uncheckedInflateBytesToChars(byte[] src, int srcOff, char[] dst, int dstOff, int len) {
StringLatin1.inflate(src, srcOff, dst, dstOff, len);
}

public int decodeASCII(byte[] src, int srcOff, char[] dst, int dstOff, int len) {
public int uncheckedDecodeASCII(byte[] src, int srcOff, char[] dst, int dstOff, int len) {
return String.decodeASCII(src, srcOff, dst, dstOff, len);
}

public int encodeASCII(char[] src, int srcOff, byte[] dst, int dstOff, int len) {
public int uncheckedEncodeASCII(char[] src, int srcOff, byte[] dst, int dstOff, int len) {
return StringCoding.implEncodeAsciiArray(src, srcOff, dst, dstOff, len);
}

Expand Down Expand Up @@ -2180,7 +2180,7 @@ public long stringConcatMix(long lengthCoder, char value) {
return StringConcatHelper.mix(lengthCoder, value);
}

public Object stringConcat1(String[] constants) {
public Object uncheckedStringConcat1(String[] constants) {
return new StringConcatHelper.Concat1(constants);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, Alibaba Group Holding Limited. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -1258,7 +1258,7 @@ private static MethodHandle generate(Lookup lookup, MethodType args, String[] co

// 1 argument use built-in method
if (args.parameterCount() == 1) {
Object concat1 = JLA.stringConcat1(constants);
Object concat1 = JLA.uncheckedStringConcat1(constants);
var handle = lookup.findVirtual(concat1.getClass(), METHOD_NAME, concatArgs);
return handle.bindTo(concat1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/math/BigDecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -4142,7 +4142,7 @@ private String layoutChars(boolean sci) {
buf[highIntSize] = '.';
DecimalDigits.putPairLatin1(buf, highIntSize + 1, lowInt);
try {
return JLA.newStringNoRepl(buf, StandardCharsets.ISO_8859_1);
return JLA.uncheckedNewStringNoRepl(buf, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/nio/file/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -3043,7 +3043,7 @@ public static String readString(Path path, Charset cs) throws IOException {
byte[] ba = readAllBytes(path);
if (path.getClass().getModule() != Object.class.getModule())
ba = ba.clone();
return JLA.newStringNoRepl(ba, cs);
return JLA.uncheckedNewStringNoRepl(ba, cs);
}

/**
Expand Down Expand Up @@ -3362,7 +3362,7 @@ public static Path writeString(Path path, CharSequence csq, Charset cs, OpenOpti
Objects.requireNonNull(csq);
Objects.requireNonNull(cs);

byte[] bytes = JLA.getBytesNoRepl(String.valueOf(csq), cs);
byte[] bytes = JLA.uncheckedGetBytesNoRepl(String.valueOf(csq), cs);
if (path.getClass().getModule() != Object.class.getModule())
bytes = bytes.clone();
write(path, bytes, options);
Expand Down
14 changes: 7 additions & 7 deletions src/java.base/share/classes/java/util/HexFormat.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, Alibaba Group Holding Limited. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -462,7 +462,7 @@ private String formatOptDelimiter(byte[] bytes, int fromIndex, int toIndex) {
}
try {
// Return a new string using the bytes without making a copy
return jla.newStringNoRepl(rep, StandardCharsets.ISO_8859_1);
return jla.uncheckedNewStringNoRepl(rep, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down Expand Up @@ -696,7 +696,7 @@ public String toHexDigits(byte value) {
rep[0] = (byte)toHighHexDigit(value);
rep[1] = (byte)toLowHexDigit(value);
try {
return jla.newStringNoRepl(rep, StandardCharsets.ISO_8859_1);
return jla.uncheckedNewStringNoRepl(rep, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down Expand Up @@ -732,7 +732,7 @@ public String toHexDigits(short value) {
rep[3] = (byte)toLowHexDigit((byte)value);

try {
return jla.newStringNoRepl(rep, StandardCharsets.ISO_8859_1);
return jla.uncheckedNewStringNoRepl(rep, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down Expand Up @@ -760,7 +760,7 @@ public String toHexDigits(int value) {
rep[7] = (byte)toLowHexDigit((byte)value);

try {
return jla.newStringNoRepl(rep, StandardCharsets.ISO_8859_1);
return jla.uncheckedNewStringNoRepl(rep, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down Expand Up @@ -796,7 +796,7 @@ public String toHexDigits(long value) {
rep[15] = (byte)toLowHexDigit((byte)value);

try {
return jla.newStringNoRepl(rep, StandardCharsets.ISO_8859_1);
return jla.uncheckedNewStringNoRepl(rep, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down Expand Up @@ -824,7 +824,7 @@ public String toHexDigits(long value, int digits) {
value = value >>> 4;
}
try {
return jla.newStringNoRepl(rep, StandardCharsets.ISO_8859_1);
return jla.uncheckedNewStringNoRepl(rep, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/util/UUID.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public String toString() {
HexDigits.put4(buf, 28, i3 >> 16);
HexDigits.put4(buf, 32, i3);
try {
return jla.newStringNoRepl(buf, StandardCharsets.ISO_8859_1);
return jla.uncheckedNewStringNoRepl(buf, StandardCharsets.ISO_8859_1);
} catch (CharacterCodingException cce) {
throw new AssertionError(cce);
}
Expand Down
6 changes: 3 additions & 3 deletions src/java.base/share/classes/java/util/zip/ZipCoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -256,7 +256,7 @@ int checkedHash(byte[] a, int off, int len) throws Exception {
return 0;
}
int end = off + len;
int asciiLen = JLA.countPositives(a, off, len);
int asciiLen = JLA.uncheckedCountPositives(a, off, len);
if (asciiLen != len) {
// Non-ASCII, fall back to decoding a String
// We avoid using decoder() here since the UTF8ZipCoder is
Expand All @@ -279,7 +279,7 @@ private boolean hasTrailingSlash(byte[] a, int end) {
@Override
byte compare(String str, byte[] b, int off, int len, boolean matchDirectory) {
try {
byte[] encoded = JLA.getBytesNoRepl(str, UTF_8.INSTANCE);
byte[] encoded = JLA.uncheckedGetBytesNoRepl(str, UTF_8.INSTANCE);
int mismatch = Arrays.mismatch(encoded, 0, encoded.length, b, off, off+len);
if (mismatch == -1) {
return EXACT_MATCH;
Expand Down
Loading