Skip to content

Commit 49a4dcb

Browse files
fapiftaivandika3
authored andcommitted
HDDS-11392. ChecksumByteBufferImpl's static initializer fails with java 17+ (apache#7135)
(cherry picked from commit 814f78f)
1 parent b8b2144 commit 49a4dcb

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/JavaUtils.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,24 @@ public final class JavaUtils {
3030
* is equal or greater than the parameter.
3131
*
3232
* @param version 8, 9, 10 etc.
33-
* @return comparison with system property, always true for 8
33+
* @return comparison with system property, always true for any int up to 8
3434
*/
3535
public static boolean isJavaVersionAtLeast(int version) {
3636
return JAVA_SPEC_VER >= version;
3737
}
3838

39+
/**
40+
* Query to see if major version of Java specification of the system
41+
* is equal or less than the parameter.
42+
*
43+
* @param version 8, 9, 10 etc.
44+
* @return comparison with system property
45+
*/
46+
public static boolean isJavaVersionAtMost(int version) {
47+
return JAVA_SPEC_VER <= version;
48+
}
49+
50+
3951
/**
4052
* Private constructor.
4153
*/

hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/ChecksumByteBufferImpl.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.ozone.common;
1919

20+
import org.apache.hadoop.hdds.JavaUtils;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
2223

@@ -38,12 +39,14 @@ public class ChecksumByteBufferImpl implements ChecksumByteBuffer {
3839

3940
static {
4041
Field f = null;
41-
try {
42-
f = ByteBuffer.class
43-
.getDeclaredField("isReadOnly");
44-
f.setAccessible(true);
45-
} catch (NoSuchFieldException e) {
46-
LOG.error("No isReadOnly field in ByteBuffer", e);
42+
if (JavaUtils.isJavaVersionAtMost(8)) {
43+
try {
44+
f = ByteBuffer.class
45+
.getDeclaredField("isReadOnly");
46+
f.setAccessible(true);
47+
} catch (NoSuchFieldException e) {
48+
LOG.error("No isReadOnly field in ByteBuffer", e);
49+
}
4750
}
4851
IS_READY_ONLY_FIELD = f;
4952
}

0 commit comments

Comments
 (0)