getDeclaredMethod不适用于android 9(pie)

时间:2019-07-02 10:25:04

标签: java android android-9.0-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());

        }
    }

Logcat错误:

  

2019-07-02 03:01:01.893 29833-29861 / io.virtualapp:x E /您好:   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 /您好:   RefStaticMethod:5 java.lang.NullPointerException:尝试调用   虚方法'void java.lang.reflect.Method.setAccessible(boolean)'   在空对象引用上

具有适用于android 9(PIE)的任何解决方案,或解决此getDeclaredMethod问题的任何替代方法。预先感谢

0 个答案:

没有答案