Skip to content

Commit 0d0564c

Browse files
authored
Fix JDK 26 compatibility in CharsetsTest for ArrayEncoder API change (#6902)
Adapt CharsetsTest to support both JDK 26+ (new encodeFromUTF16 API) and earlier JDK versions (old encode API) using version-aware method selection. JDK 26 removed the unused sun.nio.cs.ArrayEncoder::encode method and replaced it with encodeFromUTF16 which accepts UTF-16 encoded byte arrays instead of char arrays (JDK-8363925). Changes: - Add JavaVersion utility for JDK version detection - Update static initialization to load appropriate method based on JDK version - Modify encode_data to use encodeFromUTF16 for JDK 26+ and encode for earlier versions - Convert char[] to UTF-16 LE byte array when using new API Files Modified: - functional/MBCS_Tests/charsets/src/CharsetsTest.java Files Created: - functional/MBCS_Tests/charsets/src/JavaVersion.java Fixes test failures caused by JDK 26's ArrayEncoder API change (JDK-8363925). Reference: https://bugs.openjdk.org/browse/JDK-8363925 Signed-off-by: Rishabh Thakur <rishabh@ibm.com>
1 parent 9885da2 commit 0d0564c

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

functional/MBCS_Tests/charsets/src/CharsetsTest.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class CharsetsTest {
3232
static Class<?> ArrayEncoder = null;
3333
static Method ArrayDecoder_decode = null;
3434
static Method ArrayEncoder_encode = null;
35+
static Method ArrayEncoder_encodeFromUTF16 = null;
3536

3637
static int err_cnt = 0;
3738

@@ -41,9 +42,20 @@ public class CharsetsTest {
4142
ArrayEncoder = Class.forName("sun.nio.cs.ArrayEncoder");
4243
ArrayDecoder_decode = ArrayDecoder.getDeclaredMethod("decode",
4344
byte[].class, int.class, int.class, char[].class);
44-
ArrayEncoder_encode = ArrayEncoder.getDeclaredMethod("encode",
45-
char[].class, int.class, int.class, byte[].class);
45+
ArrayDecoder_decode.setAccessible(true);
46+
47+
// JDK 26+ uses encodeFromUTF16, earlier versions use encode
48+
if (JavaVersion.getFeature() >= 26) {
49+
ArrayEncoder_encodeFromUTF16 = ArrayEncoder.getDeclaredMethod("encodeFromUTF16",
50+
byte[].class, int.class, int.class, byte[].class, int.class);
51+
ArrayEncoder_encodeFromUTF16.setAccessible(true);
52+
} else {
53+
ArrayEncoder_encode = ArrayEncoder.getDeclaredMethod("encode",
54+
char[].class, int.class, int.class, byte[].class);
55+
ArrayEncoder_encode.setAccessible(true);
56+
}
4657
} catch (Exception e) {
58+
e.printStackTrace();
4759
}
4860
{
4961
Vector<TreeSet[]> vector = new Vector<TreeSet[]>();
@@ -223,8 +235,22 @@ static void encode_data(CharsetEncoder ce) throws Exception {
223235
}
224236
char[] ca = Character.toChars(i);
225237
ce.reset();
238+
226239
byte[] ba = new byte[(int)Math.ceil(ce.maxBytesPerChar()*ca.length)];
227-
int len = (int)ArrayEncoder_encode.invoke(ArrayEncoder.cast(ce),ca,0,ca.length,ba);
240+
int len;
241+
242+
// JDK 26+ uses encodeFromUTF16, earlier versions use encode
243+
if (JavaVersion.getFeature() >= 26) {
244+
// Convert char[] to UTF-16 LE byte array for the new API
245+
byte[] utf16Bytes = new byte[ca.length * 2];
246+
for (int j = 0; j < ca.length; j++) {
247+
utf16Bytes[j * 2] = (byte)(ca[j] & 0xFF);
248+
utf16Bytes[j * 2 + 1] = (byte)(ca[j] >> 8);
249+
}
250+
len = (int)ArrayEncoder_encodeFromUTF16.invoke(ArrayEncoder.cast(ce), utf16Bytes, 0, ca.length, ba, 0);
251+
} else {
252+
len = (int)ArrayEncoder_encode.invoke(ArrayEncoder.cast(ce), ca, 0, ca.length, ba);
253+
}
228254
StringBuffer sb0 = new StringBuffer();
229255
for(int j=0; j<len; j++) sb0.append(String.format("\\x%02X", (int)ba[j] & 0xFF));
230256
StringBuffer sb1 = new StringBuffer();
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*******************************************************************************
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*******************************************************************************/
14+
15+
import java.lang.reflect.Method;
16+
17+
public class JavaVersion {
18+
final static long version;
19+
final static int feature;
20+
final static int interim;
21+
final static int update;
22+
23+
static {
24+
int tempFeature = 0;
25+
int tempInterim = 0;
26+
int tempUpdate = 0;
27+
try {
28+
Class<?> runtimeClass = Class.forName("java.lang.Runtime");
29+
Method versionMid = runtimeClass.getDeclaredMethod("version", (Class<?>[])null);
30+
Object ver = versionMid.invoke(null, (Object[])null);
31+
Method featureMid = ver.getClass().getDeclaredMethod("feature", (Class<?>[])null);
32+
Method interimMid = ver.getClass().getDeclaredMethod("interim", (Class<?>[])null);
33+
Method updateMid = ver.getClass().getDeclaredMethod("update", (Class<?>[])null);
34+
tempFeature = (int)featureMid.invoke(ver, (Object[])null);
35+
tempInterim = (int)interimMid.invoke(ver, (Object[])null);
36+
tempUpdate = (int)updateMid.invoke(ver, (Object[])null);
37+
} catch (Exception e) {
38+
}
39+
feature = tempFeature;
40+
interim = tempInterim;
41+
update = tempUpdate;
42+
version = feature * 1000000L +
43+
interim * 1000L +
44+
update;
45+
}
46+
47+
public static long getVersion() {
48+
return version;
49+
}
50+
51+
public static long getFeature() {
52+
return feature;
53+
}
54+
55+
public static long getInterim() {
56+
return interim;
57+
}
58+
59+
public static long getUpdate() {
60+
return update;
61+
}
62+
63+
public static void main(String[] args) {
64+
System.out.println(getVersion());
65+
}
66+
}

0 commit comments

Comments
 (0)