Search in sources :

Example 6 with CodeBuilder

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

the class BeanValidatorBuilder method build.

@Nullable
public BeanValidator build() {
    validate(beanClass);
    if (Classes.instanceFields(beanClass).stream().noneMatch(this::hasValidationAnnotation))
        return null;
    builder = new DynamicInstanceBuilder<>(BeanValidator.class, beanClass.getSimpleName());
    String method = validateMethod(beanClass, null);
    var code = new CodeBuilder().append("public void validate(Object instance, {} errors, boolean partial) {\n", type(ValidationErrors.class));
    code.indent(1).append("{}(({}) instance, errors, partial);\n", method, type(beanClass));
    code.append('}');
    builder.addMethod(code.build());
    return builder.build();
}
Also used : CodeBuilder(core.framework.internal.asm.CodeBuilder) Nullable(javax.annotation.Nullable)

Example 7 with CodeBuilder

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

the class WebServiceControllerBuilder method buildMethod.

private String buildMethod(boolean deprecated) {
    var builder = new CodeBuilder();
    builder.append("public {} execute({} request) throws Exception {\n", type(Response.class), type(Request.class));
    if (deprecated)
        builder.indent(1).append("{}.logDeprecation({});\n", type(WebServiceController.class), variable(Methods.path(method)));
    Annotation[][] annotations = method.getParameterAnnotations();
    List<String> params = new ArrayList<>(annotations.length);
    Class<?>[] paramClasses = method.getParameterTypes();
    for (int i = 0; i < annotations.length; i++) {
        Class<?> paramClass = paramClasses[i];
        String paramClassLiteral = type(paramClass);
        PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
        if (pathParam != null) {
            params.add("$" + pathParam.value());
            if (String.class.equals(paramClass)) {
                builder.indent(1).append("String ${} = request.pathParam(\"{}\");\n", pathParam.value(), pathParam.value());
            } else if (Integer.class.equals(paramClass)) {
                builder.indent(1).append("Integer ${} = {}.toInt(request.pathParam(\"{}\"));\n", pathParam.value(), type(PathParamHelper.class), pathParam.value());
            } else if (Long.class.equals(paramClass)) {
                builder.indent(1).append("Long ${} = {}.toLong(request.pathParam(\"{}\"));\n", pathParam.value(), type(PathParamHelper.class), pathParam.value());
            } else if (paramClass.isEnum()) {
                builder.indent(1).append("{} ${} = ({}){}.toEnum(request.pathParam(\"{}\"), {});\n", paramClassLiteral, pathParam.value(), paramClassLiteral, type(PathParamHelper.class), pathParam.value(), variable(paramClass));
            } else {
                throw new Error("not supported path param type, type=" + paramClass.getCanonicalName());
            }
        } else {
            params.add("bean");
            builder.indent(1).append("{} bean = ({}) request.bean({});\n", paramClassLiteral, paramClassLiteral, variable(paramClass));
        }
    }
    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 == method.getReturnType()) {
        builder.indent(1).append("return {}.empty()", type(Response.class));
    } else {
        builder.indent(1).append("return {}.bean(response)", type(Response.class));
    }
    ResponseStatus status = method.getDeclaredAnnotation(ResponseStatus.class);
    if (status == null) {
        builder.append(";\n");
    } else {
        builder.append(".status({});\n", variable(status.value()));
    }
    builder.append("}");
    return builder.build();
}
Also used : Request(core.framework.web.Request) ArrayList(java.util.ArrayList) CodeBuilder(core.framework.internal.asm.CodeBuilder) Response(core.framework.web.Response) ResponseStatus(core.framework.api.web.service.ResponseStatus) PathParam(core.framework.api.web.service.PathParam)

Example 8 with CodeBuilder

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

the class QueryParamReaderBuilder method fromParamsMethod.

private String fromParamsMethod() {
    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();
        builder.indent(1).append("String ${} = (String)params.get(\"{}\");\n", fieldName, name);
        // query param won't have null values, null means the key doesn't exist
        builder.indent(1).append("if (${} != null) {\n", fieldName);
        if (String.class.equals(fieldClass)) {
            builder.indent(2).append("bean.{} = {}.toString(${});\n", fieldName, helper, fieldName);
        } else if (Integer.class.equals(fieldClass)) {
            builder.indent(2).append("bean.{} = {}.toInt(${});\n", fieldName, helper, fieldName);
        } else if (Long.class.equals(fieldClass)) {
            builder.indent(2).append("bean.{} = {}.toLong(${});\n", fieldName, helper, fieldName);
        } else if (Double.class.equals(fieldClass)) {
            builder.indent(2).append("bean.{} = {}.toDouble(${});\n", fieldName, helper, fieldName);
        } else if (BigDecimal.class.equals(fieldClass)) {
            builder.indent(2).append("bean.{} = {}.toBigDecimal(${});\n", fieldName, helper, fieldName);
        } else if (Boolean.class.equals(fieldClass)) {
            builder.indent(2).append("bean.{} = {}.toBoolean(${});\n", fieldName, helper, fieldName);
        } else if (LocalDateTime.class.equals(fieldClass)) {
            builder.indent(2).append("bean.{} = {}.toDateTime(${});\n", fieldName, helper, fieldName);
        } else if (LocalDate.class.equals(fieldClass)) {
            builder.indent(2).append("bean.{} = {}.toDate(${});\n", fieldName, helper, fieldName);
        } else if (LocalTime.class.equals(fieldClass)) {
            builder.indent(2).append("bean.{} = {}.toTime(${});\n", fieldName, helper, fieldName);
        } else if (ZonedDateTime.class.equals(fieldClass)) {
            builder.indent(2).append("bean.{} = {}.toZonedDateTime(${});\n", fieldName, helper, fieldName);
        } else if (fieldClass.isEnum()) {
            builder.indent(2).append("bean.{} = ({}){}.toEnum(${}, {});\n", fieldName, type(fieldClass), helper, fieldName, variable(fieldClass));
        }
        builder.indent(1).append("}\n", name);
    }
    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) ZonedDateTime(java.time.ZonedDateTime) LocalDate(java.time.LocalDate) CodeBuilder(core.framework.internal.asm.CodeBuilder)

Example 9 with CodeBuilder

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

the class QueryParamWriterBuilder method toParamsMethod.

private String toParamsMethod() {
    CodeBuilder builder = new CodeBuilder().append("public java.util.Map toParams(Object value) {\n");
    builder.indent(1).append("java.util.Map params = new java.util.HashMap();\n");
    builder.indent(1).append("{} bean = ({})value;\n", type(beanClass), type(beanClass));
    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("params.put(\"{}\", bean.{});\n", name, fieldName);
        } else {
            builder.indent(1).append("params.put(\"{}\", {}.toString(bean.{}));\n", name, helper, fieldName);
        }
    }
    builder.indent(1).append("return params;\n");
    builder.append("}");
    return builder.build();
}
Also used : Field(java.lang.reflect.Field) QueryParam(core.framework.api.web.service.QueryParam) CodeBuilder(core.framework.internal.asm.CodeBuilder)

Example 10 with CodeBuilder

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

the class EntitySchemaGenerator method schemeStatements.

private List<String> schemeStatements() {
    List<String> statements = Lists.newArrayList();
    var builder = new CodeBuilder().append("CREATE TABLE ");
    Table table = entityClass.getDeclaredAnnotation(Table.class);
    builder.append(table.name()).append(" (");
    List<String> primaryKeys = Lists.newArrayList();
    for (Field field : Classes.instanceFields(entityClass)) {
        Column column = field.getDeclaredAnnotation(Column.class);
        PrimaryKey primaryKey = field.getDeclaredAnnotation(PrimaryKey.class);
        builder.append(column.name()).append(' ');
        builder.append(columnType(field.getType(), field.getDeclaredAnnotation(Size.class)));
        if (primaryKey != null) {
            if (primaryKey.autoIncrement())
                builder.append(" AUTO_INCREMENT");
            primaryKeys.add(column.name());
        }
        if (field.isAnnotationPresent(NotNull.class)) {
            builder.append(" NOT NULL");
        }
        builder.append(", ");
    }
    builder.append("PRIMARY KEY(").appendCommaSeparatedValues(primaryKeys).append("))");
    statements.add(builder.build());
    return statements;
}
Also used : Field(java.lang.reflect.Field) Table(core.framework.db.Table) Column(core.framework.db.Column) PrimaryKey(core.framework.db.PrimaryKey) CodeBuilder(core.framework.internal.asm.CodeBuilder)

Aggregations

CodeBuilder (core.framework.internal.asm.CodeBuilder)22 Field (java.lang.reflect.Field)9 Type (java.lang.reflect.Type)4 Column (core.framework.db.Column)3 ArrayList (java.util.ArrayList)3 PathParam (core.framework.api.web.service.PathParam)2 QueryParam (core.framework.api.web.service.QueryParam)2 PrimaryKey (core.framework.db.PrimaryKey)2 LocalDate (java.time.LocalDate)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ErrorCause (co.elastic.clients.elasticsearch._types.ErrorCause)1 BulkResponseItem (co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem)1 NotNull (core.framework.api.validate.NotNull)1 ResponseStatus (core.framework.api.web.service.ResponseStatus)1 Table (core.framework.db.Table)1 SearchException (core.framework.search.SearchException)1 Request (core.framework.web.Request)1 Response (core.framework.web.Response)1 ActionDocument (core.log.domain.ActionDocument)1