getDeclaredMethod не работает в Android 9 (PIE) - PullRequest
0 голосов
/ 02 июля 2019
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. заранее спасибо

...