Skip to content
Permalink
Browse files

[ci maven-central-release] Fix changing final fields on Java 12 (#1010)

  • Loading branch information...
Vampire authored and thekingnothing committed Oct 16, 2019
1 parent 62b495c commit 3eb219272868a3273ccb8ba5aab6839013d8f933
@@ -58,16 +58,7 @@ public void run(RunNotifier notifier) {
try {
super.run(notifier);
} finally {
try {
Whitebox.setInternalState(description, "fAnnotations", new Annotation[]{});
} catch (RuntimeException err) {
if (err.getCause() instanceof java.lang.NoSuchFieldException
&& err.getCause().getMessage().equals("modifiers")) {
// on JDK12 you cannot change 'modifiers'
} else {
throw err;
}
}
Whitebox.setInternalState(description, "fAnnotations", new Annotation[]{});
}
}
}
@@ -2347,7 +2347,33 @@ private static void restoreModifiersToFieldIfChanged(int initialFieldModifiersMa

private static void sedModifiersToField(Field fieldToRemoveFinalFrom, int fieldModifiersMaskWithoutFinal) throws IllegalAccessException {
try {
Field modifiersField = Field.class.getDeclaredField("modifiers");
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;
}
}
boolean accessibleBeforeSet = modifiersField.isAccessible();
modifiersField.setAccessible(true);
modifiersField.setInt(fieldToRemoveFinalFrom, fieldModifiersMaskWithoutFinal);

0 comments on commit 3eb2192

Please sign in to comment.
You can’t perform that action at this time.