java - getDeclaredMethod 在 android 9(PIE)中不起作用
问题描述
public RefStaticMethod(Class<?> cls, Field field) {
try {
if (field.isAnnotationPresent(MethodParams.class)) {
Class<?>[] types = field.getAnnotation(MethodParams.class).value();
for (int i = 0; i < types.length; i++) {
Class<?> clazz = types[i];
if (clazz.getClassLoader() == getClass().getClassLoader()) {
try {
Class.forName(clazz.getName());
Class<?> realClass = (Class<?>) clazz.getField("TYPE").get(null);
types[i] = realClass;
} catch (Throwable e){
Log.e("hello", "RefStaticMethod: 1" );
throw new RuntimeException(e);
}
}
}
this.method = cls.getDeclaredMethod(field.getName(), types);
this.method.setAccessible(true);
} else if (field.isAnnotationPresent(MethodReflectParams.class)) {
boolean arrayset = false;
String[] typeNames = field.getAnnotation(MethodReflectParams.class).value();
Class<?>[] types = new Class<?>[typeNames.length];
Class<?>[] types2 = new Class<?>[typeNames.length];
for (int i = 0; i < typeNames.length; i++) {
Class<?> type = getProtoType(typeNames[i]);
if (type == null) {
try {
type = Class.forName(typeNames[i]);
} catch (ClassNotFoundException e) {
Log.e("hello", "RefStaticMethod: 2" );
e.printStackTrace();
}
}
types[i] = type;
if ("java.util.HashSet".equals(typeNames[i])) {
arrayset = true;
Class<?> type2 = type;
try {
type2 = Class.forName("android.util.ArraySet");
} catch (ClassNotFoundException e) {
Log.e("hello", "RefStaticMethod: 3" );
e.printStackTrace();
}
if (type2 != null) {
types2[i] = type2;
} else {
types2[i] = type;
}
} else {
types2[i] = type;
}
}
try {
this.method = cls.getDeclaredMethod(field.getName(), types);
} catch (Exception e) {
Log.e("hello", "RefStaticMethod: 4 "+e.toString() );
e.printStackTrace();
if (arrayset) {
this.method = cls.getDeclaredMethod(field.getName(), types2);
}
}
this.method.setAccessible(true);
} else {
for (Method method : cls.getDeclaredMethods()) {
if (method.getName().equals(field.getName())) {
this.method = method;
this.method.setAccessible(true);
break;
}
}
}
if (this.method == null) {
throw new NoSuchMethodException(field.getName());
}
}
catch(Exception e)
{
Log.e("hello", "RefStaticMethod: 5 "+e.toString());
}
}
日志错误:
2019-07-02 03:01:01.893 29833-29861/io.virtualapp:x E/hello: RefStaticMethod: 4 java.lang.NoSuchMethodException: collectCertificates [class android.content.pm.PackageParser$Package, int] 2019-07 -02 03:01:01.896 29833-29861/io.virtualapp:x E/hello: RefStaticMethod: 5 java.lang.NullPointerException: 尝试调用虚拟方法 'void java.lang.reflect.Method.setAccessible(boolean)' on空对象引用
有任何适用于 android 9(PIE) 的解决方案或任何替代方法来解决这个 getDeclaredMethod 问题。提前致谢
解决方案
推荐阅读
- amazon-web-services - 有条件的红移运行总数
- c++ - 为什么从 C++ 20 开始,基于范围的 for 循环错误消息仍然不是很好?
- python - 有没有办法在 2d numpy 数组中找到结点像素?
- python-3.x - 如何将字符串从 script_1 发送到 script_2?
- python - 在命名空间中的 Selenium 中找不到 XML 路径 - 厌倦了所有可能性
- salesforce - Salesforce,LWC 闪电输入类型 =“时间”移动视图中的奇怪行为
- django - Django REST 框架中的异步长时间运行函数
- android - 多次触发 TextWatcher 事件
- python - 做了一个简单的聊天机器人。如何区分“hi”作为问候语和“...hi...”等词?
- mysql - 从 Flutter 上传视频文件到 Django 后端