Permalink
Browse files

Dropped the support for setting static final fields through the Deenc…

…apsulation API, since it cannot be implemented in a way that reliably works for all versions of Java and in all usage scenarios; closes #281.
  • Loading branch information...
rliesenfeld committed May 31, 2016
1 parent fbb3e53 commit 3819065893d14df57c9a700ac80b64b0847db294
Showing with 6 additions and 40 deletions.
  1. +1 −28 main/src/mockit/internal/util/FieldReflection.java
  2. +5 −12 main/test/mockit/DeencapsulationTest.java
@@ -12,14 +12,6 @@
public final class FieldReflection
{
- private static final Field MODIFIERS_FIELD;
- static
- {
- try { MODIFIERS_FIELD = Field.class.getDeclaredField("modifiers"); }
- catch (NoSuchFieldException e) { throw new RuntimeException(e); }
- MODIFIERS_FIELD.setAccessible(true);
- }
-
private FieldReflection() {}
@Nullable
@@ -191,26 +183,7 @@ private static String getTypeName(@Nonnull Type type)
public static void setFieldValue(@Nonnull Field field, @Nullable Object targetObject, @Nullable Object value)
{
- try {
- if (isStatic(field.getModifiers()) && isFinal(field.getModifiers())) {
- setStaticFinalField(field, value);
- }
- else {
- ensureThatMemberIsAccessible(field);
- field.set(targetObject, value);
- }
- }
- catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- }
- }
-
- private static void setStaticFinalField(@Nonnull Field field, @Nullable Object value) throws IllegalAccessException
- {
- int nonFinalModifiers = MODIFIERS_FIELD.getInt(field) - FINAL;
- MODIFIERS_FIELD.setInt(field, nonFinalModifiers);
-
ensureThatMemberIsAccessible(field);
- field.set(null, value);
+ try { field.set(targetObject, value); } catch (IllegalAccessException e) { throw new RuntimeException(e); }
}
}
@@ -15,6 +15,8 @@
import mockit.internal.util.*;
import static mockit.Deencapsulation.*;
+import static org.hamcrest.CoreMatchers.*;
+
@SuppressWarnings("unused")
public final class DeencapsulationTest
{
@@ -26,8 +28,6 @@
final int initialValue = -1;
private static final Integer constantField = 123;
- private static final String compileTimeConstantField = "test";
- static final boolean FLAG = false;
private static StringBuilder buffer;
private static char static1;
@@ -313,18 +313,11 @@ public void setFinalInstanceFields()
}
@Test
- public void setStaticFinalFields() throws Exception
+ public void attemptToSetAStaticFinalField()
{
- setField(Subclass.class, "constantField", 54);
- setField(Subclass.class, "changed");
- setField(Subclass.class, true);
+ thrown.expectCause(isA(IllegalAccessException.class));
- assertEquals(54, getField(Subclass.class, "constantField"));
- assertEquals("changed", getField(Subclass.class, String.class));
- assertTrue(getField(Subclass.class, boolean.class));
-
- //noinspection ConstantJUnitAssertArgument
- assertFalse(Subclass.FLAG); // in this case, the compile-time constant gets embedded in client code
+ setField(Subclass.class, "constantField", 54);
}
@Test

0 comments on commit 3819065

Please sign in to comment.