From f91869d89d0e65911b700d75299f74f90dbd1355 Mon Sep 17 00:00:00 2001 From: Istvan Fajth Date: Thu, 29 Aug 2024 23:33:15 +0200 Subject: [PATCH] HDDS-11392. ChecksumByteBufferImpl's static initializer fails with java 17+ --- .../java/org/apache/hadoop/hdds/JavaUtils.java | 14 +++++++++++++- .../ozone/common/ChecksumByteBufferImpl.java | 14 ++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/JavaUtils.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/JavaUtils.java index 63c29ba7c912..804e6552488c 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/JavaUtils.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/JavaUtils.java @@ -30,12 +30,24 @@ public final class JavaUtils { * is equal or greater than the parameter. * * @param version 8, 9, 10 etc. - * @return comparison with system property, always true for 8 + * @return comparison with system property, always true for any int up to 8 */ public static boolean isJavaVersionAtLeast(int version) { return JAVA_SPEC_VER >= version; } + /** + * Query to see if major version of Java specification of the system + * is equal or less than the parameter. + * + * @param version 8, 9, 10 etc. + * @return comparison with system property + */ + public static boolean isJavaVersionAtMost(int version) { + return JAVA_SPEC_VER <= version; + } + + /** * Private constructor. */ diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/ChecksumByteBufferImpl.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/ChecksumByteBufferImpl.java index 1d596bf70077..a52359783270 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/ChecksumByteBufferImpl.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/common/ChecksumByteBufferImpl.java @@ -44,12 +44,14 @@ public class ChecksumByteBufferImpl implements ChecksumByteBuffer { static { Field f = null; - try { - f = ByteBuffer.class - .getDeclaredField("isReadOnly"); - f.setAccessible(true); - } catch (NoSuchFieldException e) { - LOG.error("No isReadOnly field in ByteBuffer", e); + if (JavaUtils.isJavaVersionAtMost(8)) { + try { + f = ByteBuffer.class + .getDeclaredField("isReadOnly"); + f.setAccessible(true); + } catch (NoSuchFieldException e) { + LOG.error("No isReadOnly field in ByteBuffer", e); + } } IS_READY_ONLY_FIELD = f;