Search in sources :

Example 1 with ICCTypeWrapped

use of logisticspipes.proxy.computers.interfaces.ICCTypeWrapped in project LogisticsPipes by RS485.

the class BaseWrapperClass method invokeMethod.

public Object[] invokeMethod(String methodName, Context context, Arguments args) throws Exception {
    if (object == null) {
        throw new Exception("This LP object is not persistable");
    }
    int length = args.count();
    Object[] arguments = new Object[length];
    for (int i = 0; i < length; i++) {
        if (args.isString(i)) {
            arguments[i] = args.checkString(i);
        } else {
            Object tmp = args.checkAny(i);
            if (tmp instanceof BaseWrapperClass) {
                tmp = ((BaseWrapperClass) tmp).getObject();
            }
            if (tmp instanceof ICCTypeWrapped) {
                tmp = ((ICCTypeWrapped) tmp).getObject();
            }
            arguments[i] = tmp;
        }
    }
    Method match = null;
    for (Method method : info.commands.values()) {
        if (!method.getName().equalsIgnoreCase(methodName)) {
            continue;
        }
        if (!argumentsMatch(method, arguments)) {
            continue;
        }
        match = method;
        break;
    }
    if (match == null) {
        StringBuilder error = new StringBuilder();
        error.append("No such method.");
        boolean handled = false;
        for (Method method : info.commands.values()) {
            if (!method.getName().equalsIgnoreCase(methodName)) {
                continue;
            }
            if (handled) {
                error.append("\n");
            }
            handled = true;
            error.append(method.getName());
            error.append("(");
            boolean a = false;
            for (Class<?> clazz : method.getParameterTypes()) {
                if (a) {
                    error.append(", ");
                }
                error.append(clazz.getName());
                a = true;
            }
            error.append(")");
        }
        if (!handled) {
            error = new StringBuilder();
            error.append("Internal Excption (Code: 1, ");
            error.append(methodName);
            error.append(")");
        }
        throw new UnsupportedOperationException(error.toString());
    }
    if (match.getAnnotation(CCDirectCall.class) != null) {
        if (!isDirectCall) {
            throw new PermissionException();
        }
    }
    if (match.getAnnotation(CCCommand.class).needPermission()) {
        if (info.securityMethod != null) {
            try {
                info.securityMethod.invoke(object);
            } catch (InvocationTargetException e) {
                if (e.getTargetException() instanceof Exception) {
                    throw (Exception) e.getTargetException();
                }
                throw e;
            }
        }
    }
    Object result;
    try {
        result = match.invoke(object, arguments);
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof Exception) {
            throw (Exception) e.getTargetException();
        }
        throw e;
    }
    return CCObjectWrapper.createArray(CCObjectWrapper.getWrappedObject(result, BaseWrapperClass.WRAPPER));
}
Also used : PermissionException(logisticspipes.security.PermissionException) ICCTypeWrapped(logisticspipes.proxy.computers.interfaces.ICCTypeWrapped) Method(java.lang.reflect.Method) PermissionException(logisticspipes.security.PermissionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CCDirectCall(logisticspipes.proxy.computers.interfaces.CCDirectCall) CCCommand(logisticspipes.proxy.computers.interfaces.CCCommand)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 CCCommand (logisticspipes.proxy.computers.interfaces.CCCommand)1 CCDirectCall (logisticspipes.proxy.computers.interfaces.CCDirectCall)1 ICCTypeWrapped (logisticspipes.proxy.computers.interfaces.ICCTypeWrapped)1 PermissionException (logisticspipes.security.PermissionException)1