I try to change the int value of a private static final int for unittesting. I looked at http://zarnekow.blogspot.de/2013/01/java-hacks-changing-final-fields.html and http://java-performance.info/updating-final-and-static-final-fields/ and other stackoverflow examples like: changing final variables through reflection, why difference between static and non-static final variable
30 Seconds is a default timer. i want to set it with setOverReflectionMax_SECONDS(3); to 3 seconds. But it does not work, any hints ?
My baseclass with
public class BaseClass {
private static final int MAX_SECONDS = 30;
}
and other class
public final class MyClass extends BaseClass {
public static List<Field> getFields(final Class<?> clazz){
final List<Field> list = new ArrayList<>();
list.addAll(Arrays.asList(clazz.getDeclaredFields()));
if(clazz.getSuperclass() != null){
list.addAll(getFields(clazz.getSuperclass()));
}
return list;
}
public static void setOverReflectionMax_SECONDS(final int newValue) {
final List<Field> fields = getFields(MyClass.class);
for (final Field field : fields) {
if (field.getName().equals("MAX_SECONDS")) {
field.setAccessible(true); //EDIT 4
Field modifiersField;
try {
modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
} catch (final Exception e2) {
e2.printStackTrace();
}
}
}
}
}
Edit1: wrong classname
Edit2: I cant change BaseClass (got only the .class file)
Edit3: new Exception
Class MyClass can not access a member of class BaseClass with modifiers "private static"
Edit4 : see code , fixes the exception, but it does not change the int