Search in sources :

Example 1 with ExceptionHandlerMethod

use of com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerMethod in project ezyhttp by youngmonkeys.

the class RequestHandlerImplementer method makeHandleExceptionMethodContent.

protected String makeHandleExceptionMethodContent() {
    EzyMethod method = getHandleExceptionMethod();
    EzyFunction function = new EzyFunction(method).throwsException();
    EzyBody body = function.body();
    Map<Class<?>, ExceptionHandlerMethod> exceptionHandlerMethodMap = controller.getExceptionHandlerMethodMap();
    Set<Class<?>> exceptionClasses = exceptionHandlerMethodMap.keySet();
    EzyClassTree exceptionTree = new EzyClassTree(exceptionClasses);
    for (Class<?> exceptionClass : exceptionTree.toList()) {
        ExceptionHandlerMethod m = exceptionHandlerMethodMap.get(exceptionClass);
        EzyInstruction instructionIf = new EzyInstruction("\t", "\n", false).append("if (arg1 instanceof ").append(exceptionClass.getName()).append(") {");
        body.append(instructionIf);
        EzyInstruction instructionHandle = new EzyInstruction("\t\t", "\n");
        Class<?> returnType = m.getReturnType();
        if (returnType != void.class) {
            instructionHandle.answer();
        }
        instructionHandle.append("this.controller.").append(m.getName()).bracketopen();
        appendHandleExceptionMethodArguments(m, instructionHandle, exceptionClass);
        instructionHandle.bracketclose();
        body.append(instructionHandle);
        if (returnType == void.class) {
            body.append(new EzyInstruction("\t\t", "\n").append("return null"));
        }
        body.append(new EzyInstruction("\t", "\n", false).append("}"));
    }
    body.append(new EzyInstruction("\t", "\n").append("throw arg1"));
    return function.toString();
}
Also used : EzyClassTree(com.tvd12.ezyfox.reflect.EzyClassTree) EzyBody(com.tvd12.ezyfox.asm.EzyFunction.EzyBody) 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) ExceptionHandlerMethod(com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerMethod)

Example 2 with ExceptionHandlerMethod

use of com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerMethod in project ezyhttp by youngmonkeys.

the class ControllerProxy method fetchExceptionHandlerMethods.

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

Example 3 with ExceptionHandlerMethod

use of com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerMethod in project ezyhttp by youngmonkeys.

the class ExceptionHandlerProxy method fetchExceptionHandlerMethods.

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

Example 4 with ExceptionHandlerMethod

use of com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerMethod in project ezyhttp by youngmonkeys.

the class ExceptionHandlerImplementerTest method test.

@Test
public void test() {
    ExceptionHandlerImplementer.setDebug(true);
    ExceptionHandlerProxy exceptionHandler = new ExceptionHandlerProxy(new GlobalExceptionHandler());
    for (ExceptionHandlerMethod method : exceptionHandler.getExceptionHandlerMethods()) {
        ExceptionHandlerImplementer implementer = new ExceptionHandlerImplementer(exceptionHandler, method);
        implementer.implement();
    }
}
Also used : GlobalExceptionHandler(com.tvd12.ezyhttp.server.core.test.controller.GlobalExceptionHandler) ExceptionHandlerImplementer(com.tvd12.ezyhttp.server.core.asm.ExceptionHandlerImplementer) ExceptionHandlerProxy(com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerProxy) ExceptionHandlerMethod(com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerMethod) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 5 with ExceptionHandlerMethod

use of com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerMethod in project ezyhttp by youngmonkeys.

the class ExceptionHandlerImplementerTest method implementOneFailed.

@Test
public void implementOneFailed() throws Exception {
    // given
    ExceptionHandlerProxy handler = new ExceptionHandlerProxy(new ExceptionHandler());
    ExceptionHandlerMethod handlerMethod = new ExceptionHandlerMethod(new EzyMethod(ExceptionHandler.class.getDeclaredMethod("handle", Exception.class)));
    ExceptionHandlerImplementer sut = new ExceptionHandlerImplementer(handler, handlerMethod);
    // when
    Throwable e = Asserts.assertThrows(sut::implement);
    // then
    Asserts.assertThat(e).isEqualsType(IllegalStateException.class);
}
Also used : ExceptionHandlerImplementer(com.tvd12.ezyhttp.server.core.asm.ExceptionHandlerImplementer) ExceptionHandlerProxy(com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerProxy) EzyMethod(com.tvd12.ezyfox.reflect.EzyMethod) ExceptionHandlerMethod(com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerMethod) Test(org.testng.annotations.Test)

Aggregations

ExceptionHandlerMethod (com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerMethod)7 ExceptionHandlerProxy (com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerProxy)5 Test (org.testng.annotations.Test)5 EzyMethod (com.tvd12.ezyfox.reflect.EzyMethod)4 ExceptionHandlerImplementer (com.tvd12.ezyhttp.server.core.asm.ExceptionHandlerImplementer)4 BaseTest (com.tvd12.test.base.BaseTest)4 TryCatch (com.tvd12.ezyhttp.server.core.annotation.TryCatch)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 GlobalExceptionHandler (com.tvd12.ezyhttp.server.boot.test.controller.GlobalExceptionHandler)1 UncaughtExceptionHandler (com.tvd12.ezyhttp.server.core.handler.UncaughtExceptionHandler)1 ControllerProxy (com.tvd12.ezyhttp.server.core.reflect.ControllerProxy)1 GlobalExceptionHandler (com.tvd12.ezyhttp.server.core.test.controller.GlobalExceptionHandler)1 HomeController (com.tvd12.ezyhttp.server.core.test.controller.HomeController)1 GlobalExceptionHandler (com.tvd12.ezyhttp.server.jetty.test.controller.GlobalExceptionHandler)1 HashMap (java.util.HashMap)1