Skip to content

Commit

Permalink
delete Whitebox.setInternalFinalState() method since ineffective
Browse files Browse the repository at this point in the history
Change-Id: I0497e1206b59131a219d2941237a7b3a6a46df6f
  • Loading branch information
javeme committed Apr 24, 2022
1 parent 6b0d97e commit 0f2aef6
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Objects;

Expand Down Expand Up @@ -86,42 +85,6 @@ public static <T> T getInternalState(Object target, String fieldName) {
}
}

public static void setInternalFinalState(Object target,
String fieldName, Object value) {
Class<?> c = target instanceof Class<?> ?
(Class<?>) target : target.getClass();
try {
Field field = c.getDeclaredField(fieldName);

// Remove final like FieldUtils.removeFinalModifier()
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
int oldModifiers = field.getModifiers();
int nonFinal = oldModifiers & ~Modifier.FINAL;
modifiersField.setInt(field, nonFinal);

// Reset overrideFieldAccessor
Field overrideFAField = Field.class.getDeclaredField("overrideFieldAccessor");
overrideFAField.setAccessible(true);
if (overrideFAField.get(field) != null) {
setInternalState(field, "overrideFieldAccessor", null);
}

try {
field.setAccessible(true);
field.set(target, value);
} finally {
// Resume old modifiers (final)
modifiersField.setInt(field, oldModifiers);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(String.format(
"Can't set value of '%s' against object '%s'",
fieldName, target), e);
}
}

private static Field getFieldFromHierarchy(Class<?> clazz, String field) {
Field f = getField(clazz, field);
while (f == null && clazz != Object.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,41 +107,6 @@ public void testSetInternalFinalState() {
Assert.assertEquals(2, Whitebox.getInternalState(test1, "ivalueFinal"));
// FIXME: seems don't take effect!!!
Assert.assertEquals(1, test1.ivalueFinal);

Whitebox.setInternalFinalState(test1, "ivalueFinal", 3);
Assert.assertEquals(3, Whitebox.getInternalState(test1, "ivalueFinal"));
// FIXME: seems don't take effect!!!
Assert.assertEquals(1, test1.ivalueFinal);
}

@Test
public void testSetInternalStaticFinalState() {
// Assert.assertThrows(RuntimeException.class, () -> {
// Whitebox.setInternalState(Test4.class, "staticFinalValue", 11);
// }, e -> {
// Assert.assertContains("Can't set value of 'staticFinalValue'",
// e.getMessage());
// });

Assert.assertEquals(1, Test4.staticFinalValue);
// Can only call at the first reflect time
Whitebox.setInternalFinalState(Test4.class, "staticFinalValue", 11);
Assert.assertEquals(11, Whitebox.getInternalState(Test4.class,
"staticFinalValue"));

Whitebox.setInternalFinalState(Test4.class, "staticFinalValue", 12);
Assert.assertEquals(12, Whitebox.getInternalState(Test4.class,
"staticFinalValue"));

// FIXME: seems don't take effect!!!
Assert.assertEquals(1, Test4.staticFinalValue);

Assert.assertThrows(RuntimeException.class, () -> {
Whitebox.setInternalState(Test4.class, "staticFinalValue", 1);
}, e -> {
Assert.assertContains("Can't set value of 'staticFinalValue'",
e.getMessage());
});
}

@Test
Expand Down Expand Up @@ -268,11 +233,6 @@ private String value() {
}
}

private static class Test4 {

private static final int staticFinalValue = 1;
}

@SuppressWarnings("unused")
private static class TestSubClass extends Test1 {

Expand Down

0 comments on commit 0f2aef6

Please sign in to comment.