M
M
mrgrigo2019-01-11 15:06:58
Java
mrgrigo, 2019-01-11 15:06:58

Why does invoke only work once?

Hello. For some reason, the invoke method works only once (it was checked on the output to the console - "Getter" outputs, "Setter" no longer exists). More precisely, only SettingList is called, and it is always called when I require it. But the trouble with the rest of the tasks - they are neither sleep nor spirit that they want something from them.
UPD: Infection found. When trying to reflectively call SettingInsert.asyncTaskInvoke, I caught the following exception:
I/InstantiationException( 5350): java.lang.Class has no zero argument constructor
The class of the asynchronous task, from which the method is called reflectively:

package apps.oksi.pro.useless;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import apps.oksi.pro.R;
import apps.oksi.pro.annotate.AsyncTaskInvoke;
import apps.oksi.pro.bus.Bus;
import apps.oksi.pro.handling.AsyncException;
import apps.oksi.pro.handling.Handle;

public class Async extends AsyncTask<Void, Void, Void> {

    private Class reflect;
    private Context context;
    private AppCompatActivity activity;
    private Bus bus;
    private Object result;
    private Object[] parameters;

    public Async(AppCompatActivity appCompatActivity, Class invoke, Object... params) {
        activity = appCompatActivity;
        reflect = invoke;
        context = OKSi.getCacheContext();
        bus = (Bus) activity;
        int parametersSize = 1;
        if (params != null) {
            parametersSize += params.length;
        }
        parameters = new Object[parametersSize];
        parameters[0] = reflect;
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i ++) {
                parameters[i + 1] = params[i];
            }
        }
    }

    private String validateExceptionMessage(String message) {
        if (message == null) return context.getResources().getString(R.string.asyncException);
        else return message;
    }

    private String getInvocationTargetExceptionMessage(InvocationTargetException e) {
        return validateExceptionMessage(e.getMessage());
    }

    private String getInstantiationExceptionMessage(InstantiationException e) {
        return validateExceptionMessage(e.getMessage());
    }

    private String getIllegalAccessExceptionMessage(IllegalAccessException e) {
        return validateExceptionMessage(e.getMessage());
    }

    @Override
    protected void onPreExecute() { super.onPreExecute(); }

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            Object handler = reflect.newInstance();
            for (Method method : reflect.getMethods()) {
                AsyncTaskInvoke handleCustomException = (AsyncTaskInvoke)
                        method.getAnnotation(AsyncTaskInvoke.class);
                if (handleCustomException != null) {
                    try {
                        if (parameters != null) {
                            result = method.invoke(handler, reflect, parameters);
                        } else {
                            result = method.invoke(handler, reflect);
                        }
                    } catch (IllegalAccessException e) {
                        result = Handle.handle(AsyncException.class, getIllegalAccessExceptionMessage(e));
                    } catch (InvocationTargetException e) {
                        result = Handle.handle(AsyncException.class, getInvocationTargetExceptionMessage(e));
                    }
                }
            }
        } catch (InstantiationException e) {
            result = Handle.handle(AsyncException.class, getInstantiationExceptionMessage(e));
        } catch (IllegalAccessException e) {
            result = Handle.handle(AsyncException.class, getIllegalAccessExceptionMessage(e));
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void v) {
        super.onPostExecute(v);
        if (result instanceof AsyncException) {
            bus.onBusError(reflect, result);
        } else {
            bus.onBusReady(reflect, result);
        }
    }

}

The class receiving the data:
package apps.oksi.pro.workers;

import android.util.Log;

import apps.oksi.pro.annotate.AsyncTaskInvoke;
import apps.oksi.pro.useless.OKSi;
import apps.oksi.pro.useless.Sqlite3;

public class SettingList {

    @AsyncTaskInvoke
    public static Object asyncTaskInvoke(Class reflect, Object... limits) {
        int offset = (int) limits[1];
        int limit = (int) limits[2];
        Sqlite3 db = OKSi.getSqlite3();
        Log.i("Getted", "asd");
        return db.settings().getSettings(offset, limit);
    }

}

Class that writes data:
package apps.oksi.pro.workers;

import android.util.Log;

import apps.oksi.pro.annotate.AsyncTaskInvoke;
import apps.oksi.pro.sqlite3.Setting;
import apps.oksi.pro.useless.OKSi;
import apps.oksi.pro.useless.Sqlite3;

public class SettingInsert {

    @AsyncTaskInvoke
    public static Void asyncTaskInvoke(Class reflect, Object... params) {
        Setting setting = (Setting) params[0];
        Sqlite3 db = OKSi.getSqlite3();
        db.settings().insert(setting);
        Log.i("Setted", "asd");
        return null;
    }

}

Application class:
package apps.oksi.pro.useless;

import android.app.Application;
import android.app.Dialog;
import android.arch.persistence.room.Room;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import apps.oksi.pro.bus.Bus;
import apps.oksi.pro.sqlite3.Setting;
import apps.oksi.pro.sqlite3.SiteOnline;

public class OKSi extends Application implements Bus {

    private static Context context;
    private static Context activityContext;
    private static AppCompatActivity appCompatActivity;
    private static OKSi cacheCore;
    private static SiteOnline webservice;
    private static Setting setting;
    private static Sqlite3 sqlite3;
    private static Dialog dialog;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
        context = base;
        cacheCore = this;
        sqlite3 = Room.databaseBuilder(base, Sqlite3.class, Const.SQLITE_NAME).build();
    }

    @Override
    public void onBusReady(Class reflect, Object objects) {

    }

    @Override
    public void onBusError(Class reflect, Object exception) {

    }

    public static Context getCacheContext() {
        return context;
    }

    public static OKSi getCacheCore() {
        return cacheCore;
    }

    public static Bus getBus() {
        return (Bus) cacheCore;
    }

    public static void setupSetting(Setting v) { setting = v; }

    public static void setupWebservice(SiteOnline v) { webservice = v; }

    public static Setting extractSetting() { return setting; }

    public static SiteOnline extractWebservice() { return webservice; }

    public static Sqlite3 getSqlite3() { return sqlite3; }

    public static Context getActivityContext() { return activityContext; }

    public static void setupActivityContext(Context v) { activityContext = v; }

    public static AppCompatActivity getAppCompatActivity() { return appCompatActivity; }

    public static void setupAppCompatActivity(AppCompatActivity v) { appCompatActivity = v;}

    public static void setupDialog(AlertDialog.Builder builder) {
        uninstallDialog();
        dialog = builder.show();
    }

    public static void uninstallDialog() {
        if (dialog != null) {
            dialog.cancel();
            dialog = null;
        }
    }

}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question