Search in sources :

Example 6 with EzyMethod

use of com.tvd12.ezyfox.reflect.EzyMethod 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 7 with EzyMethod

use of com.tvd12.ezyfox.reflect.EzyMethod 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 8 with EzyMethod

use of com.tvd12.ezyfox.reflect.EzyMethod 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 9 with EzyMethod

use of com.tvd12.ezyfox.reflect.EzyMethod in project ezyhttp by youngmonkeys.

the class RequestHandlerMethodTest method test.

@Test
public void test() throws Exception {
    // given
    RequestHandlerMethod sut = new RequestHandlerMethod("/get", new EzyMethod(InternalController.class.getDeclaredMethod("getSomething")));
    // when
    // then
    Asserts.assertFalse(sut.isPayment());
    Asserts.assertNull(sut.getFeature());
    System.out.println(sut);
}
Also used : RequestHandlerMethod(com.tvd12.ezyhttp.server.core.reflect.RequestHandlerMethod) EzyMethod(com.tvd12.ezyfox.reflect.EzyMethod) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 10 with EzyMethod

use of com.tvd12.ezyfox.reflect.EzyMethod 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

EzyMethod (com.tvd12.ezyfox.reflect.EzyMethod)18 EzyFunction (com.tvd12.ezyfox.asm.EzyFunction)6 EzyBody (com.tvd12.ezyfox.asm.EzyFunction.EzyBody)6 EzyInstruction (com.tvd12.ezyfox.asm.EzyInstruction)6 ArrayList (java.util.ArrayList)6 Test (org.testng.annotations.Test)6 EzyClass (com.tvd12.ezyfox.reflect.EzyClass)4 EzyClassTree (com.tvd12.ezyfox.reflect.EzyClassTree)4 CtClass (javassist.CtClass)4 RequestHandlerMethod (com.tvd12.ezyhttp.server.core.reflect.RequestHandlerMethod)3 EzyTryCatch (com.tvd12.ezyfox.core.annotation.EzyTryCatch)2 EzyExceptionHandlerMethod (com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerMethod)2 TryCatch (com.tvd12.ezyhttp.server.core.annotation.TryCatch)2 ExceptionHandlerMethod (com.tvd12.ezyhttp.server.core.reflect.ExceptionHandlerMethod)2 BaseTest (com.tvd12.test.base.BaseTest)2 EzyContext (com.tvd12.ezyfoxserver.context.EzyContext)1 EzyExceptionHandlerImplementer (com.tvd12.ezyfoxserver.support.asm.EzyExceptionHandlerImplementer)1 EzyUncaughtExceptionHandler (com.tvd12.ezyfoxserver.support.handler.EzyUncaughtExceptionHandler)1 EzyExceptionHandlerProxy (com.tvd12.ezyfoxserver.support.reflect.EzyExceptionHandlerProxy)1 EzyRequestHandlerMethod (com.tvd12.ezyfoxserver.support.reflect.EzyRequestHandlerMethod)1