Search in sources :

Example 21 with CodeBuilder

use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.

the class WebServiceControllerBuilder method buildMethod.

private String buildMethod() {
    CodeBuilder builder = new CodeBuilder();
    builder.append("public {} execute({} request) throws Exception {\n", type(Response.class), type(Request.class));
    List<String> params = Lists.newArrayList();
    Annotation[][] annotations = method.getParameterAnnotations();
    Type[] paramTypes = method.getGenericParameterTypes();
    for (int i = 0; i < annotations.length; i++) {
        Type paramType = paramTypes[i];
        String paramTypeLiteral = type(paramType);
        PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
        if (pathParam != null) {
            params.add(pathParam.value());
            builder.indent(1).append("{} {} = ({}) request.pathParam(\"{}\", {});\n", paramTypeLiteral, pathParam.value(), paramTypeLiteral, pathParam.value(), variable(paramType));
        } else {
            params.add("bean");
            builder.indent(1).append("{} bean = ({}) request.bean({});\n", paramTypeLiteral, paramTypeLiteral, variable(paramType));
        }
    }
    if (void.class == method.getReturnType()) {
        builder.indent(1).append("delegate.{}(", method.getName());
    } else {
        builder.indent(1).append("{} response = delegate.{}(", type(method.getReturnType()), method.getName());
    }
    builder.appendCommaSeparatedValues(params).append(");\n");
    if (void.class.equals(method.getReturnType())) {
        builder.indent(1).append("return {}.empty().status({});\n", Response.class.getCanonicalName(), variable(responseStatus));
    } else {
        builder.indent(1).append("return {}.bean(response).status({});\n", Response.class.getCanonicalName(), variable(responseStatus));
    }
    builder.append("}");
    return builder.build();
}
Also used : Response(core.framework.web.Response) Type(java.lang.reflect.Type) Request(core.framework.web.Request) PathParam(core.framework.api.web.service.PathParam) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 22 with CodeBuilder

use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.

the class ObjectValidatorBuilder method validateObjectMethod.

private String validateObjectMethod(Class<?> beanClass, String parentPath) {
    String methodName = "validate" + beanClass.getSimpleName() + (index++);
    CodeBuilder builder = new CodeBuilder().append("private void {}({} bean, {} errors, boolean partial) {\n", methodName, type(beanClass), type(ValidationErrors.class));
    for (Field field : Classes.instanceFields(beanClass)) {
        if (!hasValidationAnnotation(field))
            continue;
        validateAnnotations(field);
        Type fieldType = field.getGenericType();
        Class<?> fieldClass = GenericTypes.rawClass(fieldType);
        String pathLiteral = variable(path(field, parentPath));
        builder.indent(1).append("if (bean.{} == null) {\n", field.getName());
        NotNull notNull = field.getDeclaredAnnotation(NotNull.class);
        if (notNull != null)
            builder.indent(2).append("if (!partial) errors.add({}, {});\n", pathLiteral, variable(notNull.message()));
        builder.indent(1).append("} else {\n");
        if (String.class.equals(fieldClass)) {
            buildStringValidation(builder, field, pathLiteral);
        } else if (GenericTypes.isList(fieldType)) {
            buildListValidation(builder, field, pathLiteral, parentPath);
        } else if (GenericTypes.isMap(fieldType)) {
            buildMapValidation(builder, field, pathLiteral, parentPath);
        } else if (Number.class.isAssignableFrom(fieldClass)) {
            buildNumberValidation(builder, field, pathLiteral);
        } else if (!isValueClass(fieldClass)) {
            String method = validateObjectMethod(fieldClass, path(field, parentPath));
            builder.indent(2).append("{}(bean.{}, errors, partial);\n", method, field.getName());
        }
        builder.indent(1).append("}\n");
    }
    builder.append('}');
    this.builder.addMethod(builder.build());
    return methodName;
}
Also used : Field(java.lang.reflect.Field) Type(java.lang.reflect.Type) NotNull(core.framework.api.validate.NotNull) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 23 with CodeBuilder

use of core.framework.impl.asm.CodeBuilder in project core-ng-project by neowu.

the class QueryParamMapperBuilder method toBeanMethod.

private String toBeanMethod() {
    String beanClassLiteral = type(beanClass);
    CodeBuilder builder = new CodeBuilder().append("public Object fromParams(java.util.Map params) {\n");
    builder.indent(1).append("{} bean = new {}();\n", beanClassLiteral, beanClassLiteral);
    for (Field field : Classes.instanceFields(beanClass)) {
        String fieldName = field.getName();
        Class<?> fieldClass = field.getType();
        String name = field.getDeclaredAnnotation(QueryParam.class).name();
        if (String.class.equals(fieldClass)) {
            builder.indent(1).append("bean.{} = (String)params.get(\"{}\");\n", fieldName, name);
        } else if (Integer.class.equals(fieldClass)) {
            builder.indent(1).append("bean.{} = {}.toInt((String)params.get(\"{}\"));\n", fieldName, helper, name);
        } else if (Long.class.equals(fieldClass)) {
            builder.indent(1).append("bean.{} = {}.toLong((String)params.get(\"{}\"));\n", fieldName, helper, name);
        } else if (Double.class.equals(fieldClass)) {
            builder.indent(1).append("bean.{} = {}.toDouble((String)params.get(\"{}\"));\n", fieldName, helper, name);
        } else if (BigDecimal.class.equals(fieldClass)) {
            builder.indent(1).append("bean.{} = {}.toBigDecimal((String)params.get(\"{}\"));\n", fieldName, helper, name);
        } else if (Boolean.class.equals(fieldClass)) {
            builder.indent(1).append("bean.{} = {}.toBoolean((String)params.get(\"{}\"));\n", fieldName, helper, name);
        } else if (LocalDateTime.class.equals(fieldClass)) {
            builder.indent(1).append("bean.{} = {}.toDateTime((String)params.get(\"{}\"));\n", fieldName, helper, name);
        } else if (LocalDate.class.equals(fieldClass)) {
            builder.indent(1).append("bean.{} = {}.toDate((String)params.get(\"{}\"));\n", fieldName, helper, name);
        } else if (ZonedDateTime.class.equals(fieldClass)) {
            builder.indent(1).append("bean.{} = {}.toZonedDateTime((String)params.get(\"{}\"));\n", fieldName, helper, name);
        } else if (fieldClass.isEnum()) {
            builder.indent(1).append("bean.{} = ({}){}.toEnum((String)params.get(\"{}\"), {});\n", fieldName, type(fieldClass), helper, name, variable(fieldClass));
        }
    }
    builder.indent(1).append("return bean;\n");
    builder.append("}");
    return builder.build();
}
Also used : Field(java.lang.reflect.Field) QueryParam(core.framework.api.web.service.QueryParam) LocalDate(java.time.LocalDate) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Aggregations

CodeBuilder (core.framework.impl.asm.CodeBuilder)23 Field (java.lang.reflect.Field)9 Type (java.lang.reflect.Type)3 PathParam (core.framework.api.web.service.PathParam)2 QueryParam (core.framework.api.web.service.QueryParam)2 Column (core.framework.db.Column)2 LocalDate (java.time.LocalDate)2 NotNull (core.framework.api.validate.NotNull)1 Path (core.framework.api.web.service.Path)1 PrimaryKey (core.framework.db.PrimaryKey)1 Table (core.framework.db.Table)1 HTTPMethod (core.framework.http.HTTPMethod)1 DynamicInstanceBuilder (core.framework.impl.asm.DynamicInstanceBuilder)1 TemplateContext (core.framework.impl.template.TemplateContext)1 Request (core.framework.web.Request)1 Response (core.framework.web.Response)1 BigDecimal (java.math.BigDecimal)1 Function (java.util.function.Function)1 ObjectId (org.bson.types.ObjectId)1 Logger (org.slf4j.Logger)1