Implementato dynamic load module per VGAlimenti.

Implementato framework per gestire le path.
This commit is contained in:
2019-02-08 19:10:32 +01:00
parent d46a92eecf
commit 996f5978e9
24 changed files with 286 additions and 13 deletions

View File

@@ -0,0 +1,78 @@
package it.integry.integrywmsnative.core.class_router;
import android.util.Pair;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import it.integry.integrywmsnative.core.class_router.exceptions.MethodParameterTypeNotMatchException;
import it.integry.integrywmsnative.core.class_router.exceptions.MethodParamsCountException;
import it.integry.integrywmsnative.core.class_router.exceptions.MethodPathAlreadyDeclaredException;
import it.integry.integrywmsnative.core.class_router.exceptions.MethodPathNotRegisteredException;
public class BaseRouter {
private static List<Pair<String, MethodDTO>> mRoutePaths = new ArrayList<>();
private static boolean checkIfMethodExists(String path) {
boolean methodAlreadyDeclared = false;
for(int i = 0; i < mRoutePaths.size() && !methodAlreadyDeclared; i++) {
if(mRoutePaths.get(i).first.equalsIgnoreCase(path)) methodAlreadyDeclared = true;
}
return methodAlreadyDeclared;
}
private static int getMethodIndex(String path) {
for(int i = 0; i < mRoutePaths.size(); i++) {
if(mRoutePaths.get(i).first.equalsIgnoreCase(path)) return i;
}
return -1;
}
public static void registerPath(String path, MethodDTO method) throws MethodPathAlreadyDeclaredException {
if(checkIfMethodExists(path)) {
throw new MethodPathAlreadyDeclaredException(path);
}
mRoutePaths.add(new Pair<>(path, method));
}
public static void deregisterPath(String path) {
if(checkIfMethodExists(path)) {
mRoutePaths.remove(getMethodIndex(path));
}
}
public static void invokePath(String path, Object... params) throws MethodPathNotRegisteredException, MethodParamsCountException, MethodParameterTypeNotMatchException, InvocationTargetException, IllegalAccessException {
if(!checkIfMethodExists(path)) {
throw new MethodPathNotRegisteredException(path);
}
MethodDTO methodDTO = mRoutePaths.get(getMethodIndex(path)).second;
Class[] methodParameters = methodDTO.getMethod().getParameterTypes();
if(methodParameters.length != params.length) {
throw new MethodParamsCountException(path);
}
for(int i = 0; i < params.length; i++) {
if(params[i].getClass() != methodParameters[i] && methodParameters[i].cast(params[i]) == null) {
throw new MethodParameterTypeNotMatchException(params[i].getClass().getCanonicalName(), path, methodParameters[i].getCanonicalName());
}
}
methodDTO.getMethod().invoke(null, params);
}
}

View File

@@ -0,0 +1,29 @@
package it.integry.integrywmsnative.core.class_router;
import java.lang.reflect.Method;
public class MethodDTO {
private Method method;
public Method getMethod() {
return method;
}
public MethodDTO setMethod(Method method) {
this.method = method;
return this;
}
public static MethodDTO fromName(Class clazz, String methodName) {
Method[] methods = clazz.getMethods();
for (Method m : methods) {
if (methodName.equals(m.getName())) {
return new MethodDTO().setMethod(m);
}
}
return null;
}
}

View File

@@ -0,0 +1,9 @@
package it.integry.integrywmsnative.core.class_router.exceptions;
public class MethodParameterTypeNotMatchException extends Exception{
public MethodParameterTypeNotMatchException(String path, String type1, String type2) {
super(String.format("The given parameter (%s) to %s don't match the required type %s", type1, path, type2));
}
}

View File

@@ -0,0 +1,9 @@
package it.integry.integrywmsnative.core.class_router.exceptions;
public class MethodParamsCountException extends Exception {
public MethodParamsCountException(String path) {
super(String.format("Given parameters to path %s do not match the real method declaration", path));
}
}

View File

@@ -0,0 +1,11 @@
package it.integry.integrywmsnative.core.class_router.exceptions;
public class MethodPathAlreadyDeclaredException extends Exception {
public MethodPathAlreadyDeclaredException(String path) {
super(String.format("Method %s is already declared in this scope", path));
}
}

View File

@@ -0,0 +1,9 @@
package it.integry.integrywmsnative.core.class_router.exceptions;
public class MethodPathNotRegisteredException extends Exception{
public MethodPathNotRegisteredException(String path){
super(String.format("Method path %s isn't registered at the moment. Please call BaseRouter.registerPath before call invoke.", path));
}
}

View File

@@ -3,21 +3,19 @@ package it.integry.integrywmsnative.core.context;
import android.content.Context;
import com.crashlytics.android.Crashlytics;
import com.danielpuiu.ghostfish.GhostFish;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import java.lang.reflect.Method;
import io.fabric.sdk.android.Fabric;
import it.integry.integrywmsnative.BuildConfig;
import it.integry.integrywmsnative.core.REST.consumers.ColliMagazzinoRESTConsumer;
import it.integry.integrywmsnative.core.REST.model.DistribuzioneColloDTO;
import it.integry.integrywmsnative.core.REST.watcher.ServerStatusChecker;
import it.integry.integrywmsnative.core.barcode_reader.BarcodeManager;
import it.integry.integrywmsnative.core.data_recover.ColliDataRecover;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.model.secondary.GestioneEnum;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.integrywmsnative.core.settings.Stash;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.core.utility.UtilityResources;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.core.utility.UtilityToast;
@@ -47,6 +45,8 @@ public class AppContext {
this.initLogger();
this.initRecoverColli();
this.initReflections();
}
@@ -90,4 +90,27 @@ public class AppContext {
ColliDataRecover.init(mContext);
}
private void initReflections() {
GhostFish.create(mContext);
try {
String initMethod = "init";
Class dynamicContextClass = Class.forName("it.integry.wms.dynamic_customization.DynamicContext");
Method[] methods = dynamicContextClass.getMethods();
for (Method m : methods) {
if (initMethod.equals(m.getName())) {
// for static methods we can use null as instance of class
m.invoke(null);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -1,8 +1,11 @@
package it.integry.integrywmsnative.core.context;
import android.content.Context;
import android.text.SpannableString;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import it.integry.integrywmsnative.core.class_router.BaseRouter;
import it.integry.integrywmsnative.core.settings.SettingsManager;
import it.integry.integrywmsnative.view.dialogs.DialogSimpleMessageHelper;
@@ -19,6 +22,14 @@ public class MainContext {
this.initDBData(() -> {
onContextInitialized.run();
});
try {
BaseRouter.invokePath("testToast", mContext);
} catch (Exception ex) {
Log.e("PIF", ex.getMessage());
}
}