[英]Using reflection for setting new value for private member
I am creating some android app just for fun (it's not really app it's just like sandbox). 我正在创建一些android应用只是为了好玩(它并不是真正的应用,就像沙盒一样)。
For the first time I use reflection for setting some new value in private field of object of Android SDK class. 我第一次使用反射在Android SDK类的对象的私有字段中设置一些新值。
It looks like this: 看起来像这样:
try {
f = obj.getClass().getDeclaredField("<field_name>");
f.setAccessible(true);
f.set(obj, <new_value>);
} catch (Exception e) {
// log
}
I know it's a bad practice for using (and I will change it ASAP), but now it's this way. 我知道使用它是一种不好的做法(我会尽快对其进行更改),但是现在是这种方式。 It works fine on my 3 devices and emulator. 它在我的3个设备和仿真器上都能正常工作。
So my question is: In which cases I can not set a new value for existing field? 所以我的问题是:在哪些情况下,我不能为现有字段设置新值?
I followed by sorce codes of Android and came to this file: AccessCheck.c . 我后面跟随Android的源代码,并转到此文件: AccessCheck.c 。
dvmCheckFieldAccess
method is responsible for throwing an IlligalAccessExceptin. dvmCheckFieldAccess
方法负责引发IlligalAccessExceptin。 This is what I ask. 这就是我的要求。 If a method returns false
IlligalAccessExceptin will be thrown. 如果方法返回false
则将抛出IlligalAccessExceptin。
I think the main thing for this case it is: 我认为这种情况的主要原因是:
if (accessFlags & ACC_PRIVATE)
return false;
ACC_PRIVATE
flag have to same for all Android devices if they use original DVM (Dalvik VM), so it tells us the same thing suggested @executifs. 如果所有Android设备都使用原始DVM(Dalvik VM),则ACC_PRIVATE
标志必须相同。因此,它告诉我们@executifs建议的相同内容。 Correct me if I'm wrong. 如果我错了纠正我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.