反射替换静态最终字符串

时间:2013-12-02 17:12:30

标签: java android reflection

我完全按照以下方式使用反射:

Reflection

但每次我收到此错误而我都不知道原因。

12-02 17:53:58.650: W/System.err(10646): java.lang.NoSuchFieldException: modifiers
12-02 17:53:58.650: W/System.err(10646):    at java.lang.Class.getDeclaredField(Class.java:631)
12-02 17:53:58.650: W/System.err(10646):    at com.example.ref.MainActivity.setFinalStatic(MainActivity.java:56)
12-02 17:53:58.650: W/System.err(10646):    at com.example.ref.MainActivity.onCreate(MainActivity.java:26)
12-02 17:53:58.650: W/System.err(10646):    at android.app.Activity.performCreate(Activity.java:5206)
12-02 17:53:58.650: W/System.err(10646):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
12-02 17:53:58.650: W/System.err(10646):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
12-02 17:53:58.650: W/System.err(10646):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-02 17:53:58.650: W/System.err(10646):    at android.app.ActivityThread.access$600(ActivityThread.java:140)

我的代码如下:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.i("aaa", "Before = " + securedField);
    try {
        Field stringField = MainActivity.class.getDeclaredField("securedField");
        stringField.setAccessible(true);
        setFinalStatic(stringField, new String("Screwed Data!"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.i("aaa", "After = " + securedField);
}

private void setFinalStatic(Field field, Object newValue) throws Exception {
    field.setAccessible(true);
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(null, newValue);
}

有人知道问题出在哪里吗?谢谢!

1 个答案:

答案 0 :(得分:2)

查看android版java.lang.reflect.Field的源代码。

它没有私有字段modifiers。相反,它看起来像这样

public int getModifiers() {
    return accessFlags & 0xffff;  // mask out bits not used by Java
}

您尝试将“普通”jvm的实现细节调整为android jvm。因为它是另一个jvm的实现细节,所以它不适用于android。

但是你要做的事情无论如何都行不通。请记住,编译器会内联常量。

这不起作用

class Greeting {

    public static final String SALUTATION = "Hello";

    public void greet(String name) {
        System.out.println(SALUTATION + " " + name);
    }
}

public class Main {

    public static void main(String[] args) throws Exception {
        Greeting greeting = new Greeting();
        greeting.greet("John");

        Field declaredField = Greeting.class.getDeclaredField("SALUTATION");
        setFinalStatic(declaredField, "Hey");

        greeting.greet("John");
    }

    private static void setFinalStatic(Field field, Object newValue)
            throws Exception {
        field.setAccessible(true);
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        field.set(null, newValue);
    }

} 

输出

Hello John
Hello John

另请查看JLS, 13.4.9. final Fields and Constants了解详情。