HBASE-25516 [JDK17] reflective access Field.class.getDeclaredField("modifiers") not supported (#3443)

Signed-off-by: Duo Zhang <zhangduo@apache.org>
Signed-off-by: Michael Stack <stack@apache.org>
(cherry picked from commit 29cd782d25)
This commit is contained in:
Wei-Chiu Chuang 2021-07-07 01:00:57 -07:00 committed by Duo Zhang
parent 667af49955
commit bfee537c94
6 changed files with 43 additions and 5 deletions

View File

@ -25,6 +25,7 @@ import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo; import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean; import java.lang.management.ThreadMXBean;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -212,4 +213,38 @@ public class ReflectionUtils {
return parameterTypes; return parameterTypes;
} }
public static Field getModifiersField() throws IllegalAccessException, NoSuchFieldException {
// this is copied from https://github.com/powermock/powermock/pull/1010/files to work around
// JDK 12+
Field modifiersField = null;
try {
modifiersField = Field.class.getDeclaredField("modifiers");
} catch (NoSuchFieldException e) {
try {
Method getDeclaredFields0 =
Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
boolean accessibleBeforeSet = getDeclaredFields0.isAccessible();
getDeclaredFields0.setAccessible(true);
Field[] fields = (Field[]) getDeclaredFields0.invoke(Field.class, false);
getDeclaredFields0.setAccessible(accessibleBeforeSet);
for (Field field : fields) {
if ("modifiers".equals(field.getName())) {
modifiersField = field;
break;
}
}
if (modifiersField == null) {
throw e;
}
} catch (NoSuchMethodException ex) {
e.addSuppressed(ex);
throw e;
} catch (InvocationTargetException ex) {
e.addSuppressed(ex);
throw e;
}
}
return modifiersField;
}
} }

View File

@ -84,7 +84,7 @@ public class TestByteBufferUtils {
private static void setUnsafe(String fieldName, boolean value) throws Exception { private static void setUnsafe(String fieldName, boolean value) throws Exception {
Field field = ByteBufferUtils.class.getDeclaredField(fieldName); Field field = ByteBufferUtils.class.getDeclaredField(fieldName);
field.setAccessible(true); field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers"); Field modifiersField = ReflectionUtils.getModifiersField();
modifiersField.setAccessible(true); modifiersField.setAccessible(true);
int oldModifiers = field.getModifiers(); int oldModifiers = field.getModifiers();
modifiersField.setInt(field, oldModifiers & ~Modifier.FINAL); modifiersField.setInt(field, oldModifiers & ~Modifier.FINAL);

View File

@ -51,7 +51,8 @@ public class TestBytes extends TestCase {
private static void setUnsafe(boolean value) throws Exception { private static void setUnsafe(boolean value) throws Exception {
Field field = Bytes.class.getDeclaredField("UNSAFE_UNALIGNED"); Field field = Bytes.class.getDeclaredField("UNSAFE_UNALIGNED");
field.setAccessible(true); field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
Field modifiersField = ReflectionUtils.getModifiersField();
modifiersField.setAccessible(true); modifiersField.setAccessible(true);
int oldModifiers = field.getModifiers(); int oldModifiers = field.getModifiers();
modifiersField.setInt(field, oldModifiers & ~Modifier.FINAL); modifiersField.setInt(field, oldModifiers & ~Modifier.FINAL);

View File

@ -44,6 +44,7 @@ import org.apache.hadoop.hbase.net.Address;
import org.apache.hadoop.hbase.testclassification.LargeTests; import org.apache.hadoop.hbase.testclassification.LargeTests;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.JVMClusterUtil; import org.apache.hadoop.hbase.util.JVMClusterUtil;
import org.apache.hadoop.hbase.util.ReflectionUtils;
import org.apache.hadoop.hbase.util.VersionInfo; import org.apache.hadoop.hbase.util.VersionInfo;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass; import org.junit.AfterClass;
@ -273,7 +274,7 @@ public class TestRSGroupsKillRS extends TestRSGroupsBase {
private static void setFinalStatic(Field field, Object newValue) throws Exception { private static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true); field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers"); Field modifiersField = ReflectionUtils.getModifiersField();
modifiersField.setAccessible(true); modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue); field.set(null, newValue);

View File

@ -321,7 +321,7 @@ public class HFileSystem extends FilterFileSystem {
try { try {
Field nf = DFSClient.class.getDeclaredField("namenode"); Field nf = DFSClient.class.getDeclaredField("namenode");
nf.setAccessible(true); nf.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers"); Field modifiersField = ReflectionUtils.getModifiersField();
modifiersField.setAccessible(true); modifiersField.setAccessible(true);
modifiersField.setInt(nf, nf.getModifiers() & ~Modifier.FINAL); modifiersField.setInt(nf, nf.getModifiers() & ~Modifier.FINAL);

View File

@ -129,6 +129,7 @@ import org.apache.hadoop.hbase.util.JVMClusterUtil;
import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread; import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread; import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
import org.apache.hadoop.hbase.util.Pair; import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.hbase.util.ReflectionUtils;
import org.apache.hadoop.hbase.util.RegionSplitter; import org.apache.hadoop.hbase.util.RegionSplitter;
import org.apache.hadoop.hbase.util.RegionSplitter.SplitAlgorithm; import org.apache.hadoop.hbase.util.RegionSplitter.SplitAlgorithm;
import org.apache.hadoop.hbase.util.RetryCounter; import org.apache.hadoop.hbase.util.RetryCounter;
@ -2668,7 +2669,7 @@ public class HBaseTestingUtility extends HBaseZKTestingUtility {
logDirField = TaskLog.class.getDeclaredField("LOG_DIR"); logDirField = TaskLog.class.getDeclaredField("LOG_DIR");
logDirField.setAccessible(true); logDirField.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers"); Field modifiersField = ReflectionUtils.getModifiersField();
modifiersField.setAccessible(true); modifiersField.setAccessible(true);
modifiersField.setInt(logDirField, logDirField.getModifiers() & ~Modifier.FINAL); modifiersField.setInt(logDirField, logDirField.getModifiers() & ~Modifier.FINAL);