Skip to content

Commit

Permalink
Fix powermock#673: Cannot set value to private static final primitive…
Browse files Browse the repository at this point in the history
… field
  • Loading branch information
Andrei Cristian Petcu committed Jun 3, 2016
1 parent a159bc0 commit 0262856
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
Expand Up @@ -2318,11 +2318,19 @@ private static void removeFinalModifierIfPresent(Field fieldToRemoveFinalFrom) t
int fieldModifiersMask = fieldToRemoveFinalFrom.getModifiers();
boolean isFinalModifierPresent = (fieldModifiersMask & Modifier.FINAL) == Modifier.FINAL;
if (isFinalModifierPresent) {
checkIfCanSetNewValue(fieldToRemoveFinalFrom);
int fieldModifiersMaskWithoutFinal = fieldModifiersMask & ~Modifier.FINAL;
sedModifiersToField(fieldToRemoveFinalFrom, fieldModifiersMaskWithoutFinal);
}
}

private static void checkIfCanSetNewValue(Field fieldToSetNewValueTo) {
boolean fieldTypeIsPrimitive = fieldToSetNewValueTo.getType().isPrimitive();
if (fieldTypeIsPrimitive) {
throw new IllegalArgumentException("You are trying to set a private static final primitive. Try using an object like Integer instead of int!");
}
}

private static void restoreModifiersToFieldIfChanged(int initialFieldModifiersMask, Field fieldToRestoreModifiersTo) throws IllegalAccessException {
int newFieldModifiersMask = fieldToRestoreModifiersTo.getModifiers();
if(initialFieldModifiersMask != newFieldModifiersMask){
Expand Down
25 changes: 19 additions & 6 deletions reflect/src/test/java/org/powermock/reflect/WhiteBoxTest.java
Expand Up @@ -16,7 +16,9 @@
package org.powermock.reflect;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.powermock.reflect.context.ClassFieldsNotInTargetContext;
import org.powermock.reflect.context.InstanceFieldsNotInTargetContext;
import org.powermock.reflect.context.MyContext;
Expand Down Expand Up @@ -79,6 +81,9 @@
*/
public class WhiteBoxTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

static {
RegisterProxyFramework.registerProxyFramework(new ProxyFramework() {
public Class<?> getUnproxiedType(Class<?> type) {
Expand Down Expand Up @@ -270,14 +275,22 @@ public void testStaticState() {
}

@Test
public void testStaticFinalState() throws NoSuchFieldException {
int modifiersBeforeSet = ClassWithInternalState.class.getDeclaredField("staticFinalStateInteger").getModifiers();
Integer newValue = ClassWithInternalState.getStaticFinalStateInteger() + 1;
public void testStaticFinalPrimitiveState() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("You are trying to set a private static final primitive. Try using an object like Integer instead of int!");

Whitebox.setInternalState(ClassWithInternalState.class, "staticFinalIntState", 123);
}

@Test
public void testStaticFinalObject() throws NoSuchFieldException {
int modifiersBeforeSet = ClassWithInternalState.class.getDeclaredField("staticFinalIntegerState").getModifiers();
Integer newValue = ClassWithInternalState.getStaticFinalIntegerState() + 1;

Whitebox.setInternalState(ClassWithInternalState.class, "staticFinalStateInteger", newValue);
Whitebox.setInternalState(ClassWithInternalState.class, "staticFinalIntegerState", newValue);

int modifiersAfterSet = ClassWithInternalState.class.getDeclaredField("staticFinalStateInteger").getModifiers();
assertEquals(newValue, ClassWithInternalState.getStaticFinalStateInteger());
int modifiersAfterSet = ClassWithInternalState.class.getDeclaredField("staticFinalIntegerState").getModifiers();
assertEquals(newValue, ClassWithInternalState.getStaticFinalIntegerState());
assertEquals(modifiersBeforeSet, modifiersAfterSet);
}

Expand Down
Expand Up @@ -23,9 +23,9 @@ public class ClassWithInternalState {

private static int staticState = 5;

private static final int staticFinalState = 15;
private static final int staticFinalIntState = 15;

private static final Integer staticFinalStateInteger = 15;
private static final Integer staticFinalIntegerState = 15;

private int internalState = 0;

Expand Down Expand Up @@ -61,12 +61,8 @@ public static int getStaticState() {
return staticState;
}

public static int getStaticFinalState() {
return staticFinalState;
}

public static Integer getStaticFinalStateInteger() {
return staticFinalStateInteger;
public static Integer getStaticFinalIntegerState() {
return staticFinalIntegerState;
}

public ClassWithPrivateMethods getClassWithPrivateMethods() {
Expand Down

0 comments on commit 0262856

Please sign in to comment.