Search in sources :

Example 1 with EzyExceptionHandlerMethod

use of com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerMethod in project ezyfox-server by youngmonkeys.

the class EzyExceptionHandlerProxy method fetchExceptionHandlerMethods.

public List<EzyExceptionHandlerMethod> fetchExceptionHandlerMethods() {
    List<EzyExceptionHandlerMethod> list = new ArrayList<>();
    List<EzyMethod> methods = clazz.getPublicMethods(m -> m.isAnnotated(EzyTryCatch.class));
    for (EzyMethod method : methods) {
        EzyExceptionHandlerMethod m = new EzyExceptionHandlerMethod(method);
        list.add(m);
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) EzyTryCatch(com.tvd12.ezyfox.core.annotation.EzyTryCatch) EzyMethod(com.tvd12.ezyfox.reflect.EzyMethod)

Example 2 with EzyExceptionHandlerMethod

use of com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerMethod in project ezyfox-server by youngmonkeys.

the class EzyRequestControllerProxy method fetchExceptionHandlerMethods.

public List<EzyExceptionHandlerMethod> fetchExceptionHandlerMethods() {
    List<EzyExceptionHandlerMethod> list = new ArrayList<>();
    List<EzyMethod> methods = clazz.getMethods(m -> m.isAnnotated(EzyTryCatch.class));
    for (EzyMethod method : methods) {
        EzyExceptionHandlerMethod m = new EzyExceptionHandlerMethod(method);
        list.add(m);
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) EzyTryCatch(com.tvd12.ezyfox.core.annotation.EzyTryCatch) EzyMethod(com.tvd12.ezyfox.reflect.EzyMethod)

Example 3 with EzyExceptionHandlerMethod

use of com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerMethod in project ezyfox-server by youngmonkeys.

the class EzyExceptionHandlersImplementer method implement.

public Map<Class<?>, EzyUncaughtExceptionHandler> implement(Object exceptionHandler) {
    Map<Class<?>, EzyUncaughtExceptionHandler> handlers = new HashMap<>();
    EzyExceptionHandlerProxy proxy = new EzyExceptionHandlerProxy(exceptionHandler);
    for (EzyExceptionHandlerMethod method : proxy.getExceptionHandlerMethods()) {
        EzyExceptionHandlerImplementer implementer = newImplementer(proxy, method);
        EzyUncaughtExceptionHandler handler = implementer.implement();
        for (Class<?> exceptionClass : method.getExceptionClasses()) {
            handlers.put(exceptionClass, handler);
        }
    }
    return handlers;
}
Also used : EzyExceptionHandlerProxy(com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerProxy) HashMap(java.util.HashMap) EzyExceptionHandlerMethod(com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerMethod) EzyUncaughtExceptionHandler(com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler)

Example 4 with EzyExceptionHandlerMethod

use of com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerMethod in project ezyfox-server by youngmonkeys.

the class EzyRequestHandlerImplementer method makeHandleExceptionMethodContent.

protected String makeHandleExceptionMethodContent() {
    EzyMethod method = getHandleExceptionMethod();
    EzyFunction function = new EzyFunction(method).throwsException();
    EzyBody body = function.body();
    Map<Class<?>, EzyExceptionHandlerMethod> exceptionHandlerMethodMap = controller.getExceptionHandlerMethodMap();
    Set<Class<?>> exceptionClasses = exceptionHandlerMethodMap.keySet();
    EzyClassTree exceptionTree = new EzyClassTree(exceptionClasses);
    for (Class<?> exceptionClass : exceptionTree.toList()) {
        EzyExceptionHandlerMethod m = exceptionHandlerMethodMap.get(exceptionClass);
        EzyInstruction instructionIf = new EzyInstruction("\t", "\n", false).append("if(arg3 instanceof ").append(exceptionClass.getName()).append(") {");
        body.append(instructionIf);
        EzyInstruction instructionHandle = new EzyInstruction("\t\t", "\n");
        instructionHandle.append("this.controller.").append(m.getName()).bracketopen();
        appendHandleExceptionMethodArguments(m, instructionHandle, exceptionClass);
        instructionHandle.bracketclose();
        body.append(instructionHandle);
        body.append(new EzyInstruction("\t", "\n", false).append("}"));
    }
    if (exceptionClasses.size() > 0) {
        body.append(new EzyInstruction("\t", "\n", false).append("else {"));
        body.append(new EzyInstruction("\t\t", "\n").append("throw arg3"));
        body.append(new EzyInstruction("\t", "\n", false).append("}"));
    } else {
        body.append(new EzyInstruction("\t", "\n").append("throw arg3"));
    }
    return function.toString();
}
Also used : EzyClassTree(com.tvd12.ezyfox.reflect.EzyClassTree) EzyBody(com.tvd12.ezyfox.asm.EzyFunction.EzyBody) EzyExceptionHandlerMethod(com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerMethod) EzyInstruction(com.tvd12.ezyfox.asm.EzyInstruction) EzyFunction(com.tvd12.ezyfox.asm.EzyFunction) CtClass(javassist.CtClass) EzyClass(com.tvd12.ezyfox.reflect.EzyClass) EzyMethod(com.tvd12.ezyfox.reflect.EzyMethod)

Example 5 with EzyExceptionHandlerMethod

use of com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerMethod in project ezyfox-server by youngmonkeys.

the class EzyAbstractHandlerImplementer method appendHandleExceptionMethodArguments.

protected void appendHandleExceptionMethodArguments(EzyExceptionHandlerMethod method, EzyInstruction instruction, Class<?> exceptionClass, String commandArg, String dataArg, String exceptionArg) {
    int paramCount = 0;
    Parameter[] parameters = method.getParameters();
    Class<?> requestDataType = method.getRequestDataType();
    for (Parameter parameter : parameters) {
        Class<?> parameterType = parameter.getType();
        if (parameterType == requestDataType) {
            instruction.brackets(requestDataType).append(dataArg);
        } else if (EzyContext.class.isAssignableFrom(parameterType)) {
            instruction.append("arg0");
        } else if (parameterType == EzyUserSessionEvent.class) {
            instruction.append("arg1");
        } else if (parameterType == EzyUser.class) {
            instruction.append("arg1.getUser()");
        } else if (parameterType == EzySession.class) {
            instruction.append("arg1.getSession()");
        } else if (parameterType == String.class) {
            instruction.append(commandArg);
        } else if (Throwable.class.isAssignableFrom(parameterType)) {
            instruction.brackets(exceptionClass).append(exceptionArg);
        } else if (parameterType == boolean.class) {
            instruction.append("false");
        } else if (parameterType.isPrimitive()) {
            instruction.append("0");
        } else {
            instruction.append("null");
        }
        if ((paramCount++) < (parameters.length - 1)) {
            instruction.append(", ");
        }
    }
}
Also used : EzyUser(com.tvd12.ezyfoxserver.entity.EzyUser) Parameter(java.lang.reflect.Parameter) EzyContext(com.tvd12.ezyfoxserver.context.EzyContext)

Aggregations

EzyMethod (com.tvd12.ezyfox.reflect.EzyMethod)4 EzyExceptionHandlerMethod (com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerMethod)3 EzyTryCatch (com.tvd12.ezyfox.core.annotation.EzyTryCatch)2 EzyUncaughtExceptionHandler (com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler)2 EzyExceptionHandlerProxy (com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerProxy)2 ArrayList (java.util.ArrayList)2 EzyFunction (com.tvd12.ezyfox.asm.EzyFunction)1 EzyBody (com.tvd12.ezyfox.asm.EzyFunction.EzyBody)1 EzyInstruction (com.tvd12.ezyfox.asm.EzyInstruction)1 EzyClass (com.tvd12.ezyfox.reflect.EzyClass)1 EzyClassTree (com.tvd12.ezyfox.reflect.EzyClassTree)1 EzyContext (com.tvd12.ezyfoxserver.context.EzyContext)1 EzyUser (com.tvd12.ezyfoxserver.entity.EzyUser)1 EzyExceptionHandlerImplementer (com.tvd12.ezyfoxserver.support.asm.EzyExceptionHandlerImplementer)1 Parameter (java.lang.reflect.Parameter)1 HashMap (java.util.HashMap)1 CtClass (javassist.CtClass)1 Test (org.testng.annotations.Test)1