Fix incompatible reflection for modifying static final fields since JDK 12
package org.apache.hadoop.fs;
import java.lang.reflect.Field;
+import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class HadoopExtendedFileSystemCache
@@ -35,7 +36,7 @@ public class HadoopExtendedFileSystemCache
Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
- Field modifiersField = Field.class.getDeclaredField("modifiers");
+ Field modifiersField = getModifiersField();
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
@@ -47,4 +48,27 @@ public class HadoopExtendedFileSystemCache
throw new AssertionError(e);
}
}
+
+ private static Field getModifiersField() throws NoSuchFieldException
+ {
+ try {
+ return Field.class.getDeclaredField("modifiers");
+ }
+ catch (NoSuchFieldException e) {
+ try {
+ Method getDeclaredFields0 = Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
+ getDeclaredFields0.setAccessible(true);
+ Field[] fields = (Field[]) getDeclaredFields0.invoke(Field.class, false);
+ for (Field field : fields) {
+ if ("modifiers".equals(field.getName())) {
+ return field;
+ }
+ }
+ }
+ catch (ReflectiveOperationException ex) {
+ e.addSuppressed(ex);
+ }
+ throw e;
+ }
+ }
}
