Search in sources :

Example 1 with EzyMethod

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

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

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

the class EzyExceptionHandlerImplementer method makeHandleExceptionMethodContent.

protected String makeHandleExceptionMethodContent() {
    EzyMethod method = getHandleExceptionMethod();
    EzyFunction function = new EzyFunction(method).throwsException();
    EzyBody body = function.body();
    Class<?>[] exceptionClasses = handlerMethod.getExceptionClasses();
    EzyClassTree exceptionTree = new EzyClassTree(exceptionClasses);
    for (Class<?> exceptionClass : exceptionTree.toList()) {
        EzyInstruction instructionIf = new EzyInstruction("\t", "\n", false).append("if(arg4 instanceof ").append(exceptionClass.getName()).append(") {");
        body.append(instructionIf);
        EzyInstruction instructionHandle = new EzyInstruction("\t\t", "\n");
        instructionHandle.append("this.exceptionHandler.").append(handlerMethod.getName()).bracketopen();
        appendHandleExceptionMethodArguments(instructionHandle, exceptionClass);
        instructionHandle.bracketclose();
        body.append(instructionHandle);
        body.append(new EzyInstruction("\t", "\n", false).append("}"));
    }
    body.append(new EzyInstruction("\t", "\n", false).append("else {"));
    body.append(new EzyInstruction("\t\t", "\n").append("throw arg4"));
    body.append(new EzyInstruction("\t", "\n", false).append("}"));
    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)

Example 4 with EzyMethod

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

the class ExceptionHandlerImplementer method makeHandleExceptionMethodContent.

protected String makeHandleExceptionMethodContent() {
    EzyMethod method = getHandleExceptionMethod();
    EzyFunction function = new EzyFunction(method);
    EzyBody body = function.body();
    Class<?>[] exceptionClasses = handlerMethod.getExceptionClasses();
    EzyClassTree exceptionTree = new EzyClassTree(exceptionClasses);
    for (Class<?> exceptionClass : exceptionTree.toList()) {
        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 = handlerMethod.getReturnType();
        if (returnType != void.class) {
            instructionHandle.answer();
        }
        instructionHandle.append("this.exceptionHandler.").append(handlerMethod.getName()).bracketopen();
        appendHandleExceptionMethodArguments(handlerMethod, 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 toThrowExceptionFunction(method, function);
}
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)

Example 5 with EzyMethod

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

the class RequestHandlerImplementer method makeHandleRequestMethodContent.

@SuppressWarnings("MethodLength")
protected String makeHandleRequestMethodContent() {
    EzyMethod method = getHandleRequestMethod();
    EzyFunction function = new EzyFunction(method).throwsException();
    EzyBody body = function.body();
    int paramCount = 0;
    int headerCount = 0;
    int parameterCount = 0;
    int pathVariableCount = 0;
    int cookieCount = 0;
    Parameter[] parameters = handlerMethod.getParameters();
    for (Parameter parameter : parameters) {
        Class<?> parameterType = parameter.getType();
        Class<?> genericType = getGenericType(parameter);
        String genericTypeClass = genericType != null ? genericType.getName() + ".class" : "null";
        EzyInstruction instruction = new EzyInstruction("\t", "\n").clazz(parameterType).append(" ").append(PARAMETER_PREFIX).append(paramCount).equal();
        boolean hasAnnotation = false;
        RequestParam requestParamAnno = parameter.getAnnotation(RequestParam.class);
        if (requestParamAnno != null) {
            String paramKey = RequestParamAnnotations.getParamKeyString(requestParamAnno, parameterCount);
            String defaultValue = requestParamAnno.defaultValue();
            String getValueExpression = defaultValue.equals(EzyStrings.NULL) ? "arg0.getParameter(" + paramKey + ")" : "arg0.getParameter(" + paramKey + ", " + quote(defaultValue) + ")";
            String valueExpression = "this.deserializeParameter(" + paramKey + ", " + getValueExpression + ", " + parameterType.getTypeName() + ".class" + ", " + genericTypeClass + ")";
            instruction.cast(parameterType, valueExpression);
            ++parameterCount;
            hasAnnotation = true;
        }
        RequestHeader requestHeaderAnno = parameter.getAnnotation(RequestHeader.class);
        if (requestHeaderAnno != null) {
            String headerKey = RequestHeaderAnnotations.getHeaderKeyString(requestHeaderAnno, headerCount);
            String defaultValue = requestHeaderAnno.defaultValue();
            String getValueExpression = defaultValue.equals(EzyStrings.NULL) ? "arg0.getHeader(" + headerKey + ")" : "arg0.getHeader(" + headerKey + ", " + quote(defaultValue) + ")";
            String valueExpression = "this.deserializeHeader(" + headerKey + ", " + getValueExpression + ", " + parameterType.getTypeName() + ".class" + ", " + genericTypeClass + ")";
            instruction.cast(parameterType, valueExpression);
            ++headerCount;
            hasAnnotation = true;
        }
        PathVariable pathVariableAnno = parameter.getAnnotation(PathVariable.class);
        if (pathVariableAnno != null) {
            String varNameKey = PathVariableAnnotations.getVariableNameKeyString(pathVariableAnno, pathVariableCount);
            String valueExpression = "this.deserializePathVariable(" + varNameKey + ", arg0.getPathVariable(" + varNameKey + ")" + ", " + parameterType.getTypeName() + ".class" + ", " + genericTypeClass + ")";
            instruction.cast(parameterType, valueExpression);
            ++pathVariableCount;
            hasAnnotation = true;
        }
        RequestCookie requestCookieAnno = parameter.getAnnotation(RequestCookie.class);
        if (requestCookieAnno != null) {
            String cookieKey = RequestCookieAnnotations.getCookieKeyString(requestCookieAnno, cookieCount);
            String defaultValue = requestCookieAnno.defaultValue();
            String getValueExpression = defaultValue.equals(EzyStrings.NULL) ? "arg0.getCookieValue(" + cookieKey + ")" : "arg0.getCookieValue(" + cookieKey + ", " + quote(defaultValue) + ")";
            String valueExpression = "this.deserializeCookie(" + cookieKey + ", " + getValueExpression + ", " + parameterType.getTypeName() + ".class" + ", " + genericTypeClass + ")";
            instruction.cast(parameterType, valueExpression);
            ++cookieCount;
            hasAnnotation = true;
        }
        RequestBody requestBodyAnno = parameter.getAnnotation(RequestBody.class);
        if (requestBodyAnno != null) {
            instruction.brackets(parameterType).append("this.deserializeBody(").append("arg0, ").clazz(parameterType, true).append(")");
            hasAnnotation = true;
        }
        if (!hasAnnotation) {
            String valueExpression = "arg0";
            if (parameterType != RequestArguments.class) {
                String argumentKey = RequestParameters.getArgumentKeyString(parameter);
                valueExpression = "arg0.getArgument(" + argumentKey + ")";
            }
            instruction.cast(parameterType, valueExpression);
        }
        body.append(instruction);
        ++paramCount;
    }
    EzyInstruction instruction = new EzyInstruction("\t", "\n");
    Class<?> returnType = handlerMethod.getReturnType();
    if (returnType != void.class) {
        instruction.answer();
    }
    StringBuilder answerExpression = new StringBuilder();
    answerExpression.append("this.controller.").append(handlerMethod.getName()).append("(");
    for (int i = 0; i < paramCount; ++i) {
        answerExpression.append(PARAMETER_PREFIX).append(i);
        if (i < paramCount - 1) {
            answerExpression.append(", ");
        }
    }
    answerExpression.append(")");
    if (returnType != void.class) {
        instruction.valueOf(returnType, answerExpression.toString());
    } else {
        instruction.append(answerExpression);
    }
    body.append(instruction);
    if (returnType == void.class) {
        body.append(new EzyInstruction("\t", "\n").append("return null"));
    }
    return function.toString();
}
Also used : EzyBody(com.tvd12.ezyfox.asm.EzyFunction.EzyBody) RequestParam(com.tvd12.ezyhttp.server.core.annotation.RequestParam) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) EzyInstruction(com.tvd12.ezyfox.asm.EzyInstruction) Parameter(java.lang.reflect.Parameter) RequestHeader(com.tvd12.ezyhttp.server.core.annotation.RequestHeader) EzyFunction(com.tvd12.ezyfox.asm.EzyFunction) EzyMethod(com.tvd12.ezyfox.reflect.EzyMethod) PathVariable(com.tvd12.ezyhttp.server.core.annotation.PathVariable) RequestBody(com.tvd12.ezyhttp.server.core.annotation.RequestBody)

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