在Android中,如何使用Java反射调用sun.misc.Unsafe方法? [英] In Android, how to invoke sun.misc.Unsafe methods using Java reflection?
问题描述
尽管存在类似的问题(例如 A , B 和C ),他们的答案不能解决我的问题。
Although there are similar questions (such as A, B and C), their answers do not solve my problem.
我正在使用针对Android API 18的Android Studio 1.5.1(在Android KitKat 4.4之前,所以我在处理Dalvik,而不是ART运行时)。
I am using Android Studio 1.5.1 targeting Android API 18 (before Android KitKat 4.4, so I’m dealing with Dalvik, not ART runtime).
我的问题是:
(1)当我使用以下代码时,可以打印所有 sun.misc.Android中可用的不安全方法,所以我认为我可以使用 reflection b ut我不知道如何使用 reflection 来称呼他们。
(1) When I use the following code, I can print a list of all sun.misc.Unsafe methods available in Android, so I think I have access to them using reflection but I do not know how to call them using reflection.
(2)如果可能(1),如何找到magicNumber(在下面的代码中)通过反射在Android中使用sun.misc.Unsafe方法来解决问题?
(2) If (1) is possible, how to find the magicNumber (in the code below) address using sun.misc.Unsafe methods in Android via reflection?
(3)如果(1)可行但(2)不可能,如何将整数(例如int test = 123)放在任何本地内存地址中,并通过反射使用Android中的sun.misc.Unsafe方法打印其内存地址?
(3) If (1) is possible but (2) is not possible, how to put an integer number (say int test=123) in any native memory address and print its memory address using sun.misc.Unsafe methods in Android via reflection?
String ClassName = "sun.misc.Unsafe";
int magicNumber = 0x23420023 ;
try {
Class classToInvestigate = Class.forName(ClassName);
Constructor[] aClassConstructors = classToInvestigate.getDeclaredConstructors();
for(Constructor c : aClassConstructors){
System.out.println("********************* constructor="+c);
}
Method[] aClassMethods = classToInvestigate.getDeclaredMethods();
for(Method m : aClassMethods){
System.out.println("********************* method="+m);
}
Field theUnsafe = classToInvestigate.getDeclaredField("THE_ONE");
theUnsafe.setAccessible(true);
Object unsafe = theUnsafe.get(null);
} catch (ClassNotFoundException e) {
// Class not found!
}
catch (Exception e) {
// Unknown exception
}
推荐答案
不幸的是 sun.misc.Unsafe
类未包含在android标准库中,您应该在反射之间使用它但是我找到了另一个解决方案:在我的库中的预编译直接代理类之间使用它: https://github.com / iamironz / unsafe
Unfortunately sun.misc.Unsafe
class not included in android standard library and you should use it between reflection but I found another solution: to use it between pre-compiled direct proxy class from my library: https://github.com/iamironz/unsafe
这篇关于在Android中,如何使用Java反射调用sun.misc.Unsafe方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!