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();
}
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;
}
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;
}
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();
}
}
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);
}
Aggregations