dimanche 21 juillet 2019

Java Reflection IllegalAccessException [duplicate]

This question already has an answer here:

I want to change the value of a static final field, and my code has no problem when the test method has nothing else. IllegalAccessException is only thrown when the field has been getField before changing it. I wonder if there is a way to resolve it or just don't getField before changing the static field?

Tests.java: nothing is thrown

public static final String b = "default";

System.out.println("######## setField() ########");
System.out.println(b);
Reflections.setField(Tests.class, "b", "changed");
System.out.println(b);

Tests.java: IllegalAccessException is thrown

public static final String b = "default";

System.out.println("######## setField() ########");
System.out.println(Reflections.getField(Tests.class, "b"));
System.out.println(b);
Reflections.setField(Tests.class, "b", "changed");
System.out.println(b);

Reflections.java

public static void setField(Class<?> clazz, String name, Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    setField(null, value, clazz, name);
}

private static void setField(Object instance, Object value, Class<?> clazz, String name) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    requireNonNull(clazz);
    requireNonNull(name);

    Field field = clazz.getDeclaredField(name);
    field.setAccessible(true);
    breakFinal(field);
    field.set(instance, value);
}

public static Object getField(Class<?> clazz, String name) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    return getField(null, clazz, name);
}

private static Object getField(Object instance, Class<?> clazz, String name) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    requireNonNull(clazz);
    requireNonNull(name);

    Field field = clazz.getDeclaredField(name);
    field.setAccessible(true);
    return field.get(instance);
}

public static void breakFinal(Field field) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    requireNonNull(field);

    Field modifiers = Field.class.getDeclaredField("modifiers");
    modifiers.setAccessible(true);
    if((field.getModifiers() & Modifier.FINAL) == Modifier.FINAL) {
        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    }
}

It shouldn't throw any exception, what's wrong?

java.lang.IllegalAccessException: Can not set static final java.lang.String field cn.glycol.tutils.Tests.b to java.lang.String
    at sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(Unknown Source)
    at sun.reflect.UnsafeQualifiedStaticObjectFieldAccessorImpl.set(Unknown Source)
    at java.lang.reflect.Field.set(Unknown Source)
    at cn.glycol.tutils.reflections.Reflections.setField(Reflections.java:245)
    at cn.glycol.tutils.reflections.Reflections.setField(Reflections.java:221)
    at cn.glycol.tutils.Tests.test(Tests.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)





Aucun commentaire:

Enregistrer un commentaire