Search in sources :

Example 1 with CodeBuilder

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

the class EntityDecoderBuilder method decodeMapMethod.

private String decodeMapMethod(Class<?> valueClass) {
    String methodName = decodeMethods.get(Types.map(String.class, valueClass));
    if (methodName != null)
        return methodName;
    methodName = "decodeMap" + valueClass.getSimpleName() + (index++);
    CodeBuilder builder = new CodeBuilder();
    builder.append("private java.util.Map {}(org.bson.BsonReader reader, {} wrapper, String parentField) {\n", methodName, type(BsonReaderWrapper.class)).indent(1).append("java.util.Map map = wrapper.startReadMap(parentField);\n").indent(1).append("if (map == null) return null;\n").indent(1).append("reader.readStartDocument();\n").indent(1).append("while (reader.readBsonType() != org.bson.BsonType.END_OF_DOCUMENT) {\n").indent(2).append("String fieldName = reader.readName();\n").indent(2).append("String fieldPath = parentField + \".\" + fieldName;\n");
    String variable = decodeValue(builder, valueClass, 2);
    builder.indent(2).append("map.put(fieldName, {});\n", variable);
    builder.indent(1).append("}\n").indent(1).append("reader.readEndDocument();\n").indent(1).append("return map;\n").append('}');
    this.builder.addMethod(builder.build());
    decodeMethods.put(Types.map(String.class, valueClass), methodName);
    return methodName;
}
Also used : CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 2 with CodeBuilder

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

the class EntityDecoderBuilder method decodeListMethod.

private String decodeListMethod(Class<?> valueClass) {
    String methodName = decodeMethods.get(Types.list(valueClass));
    if (methodName != null)
        return methodName;
    methodName = "decodeList" + valueClass.getSimpleName() + (index++);
    CodeBuilder builder = new CodeBuilder();
    builder.append("private java.util.List {}(org.bson.BsonReader reader, {} wrapper, String fieldPath) {\n", methodName, type(BsonReaderWrapper.class));
    builder.indent(1).append("java.util.List list = wrapper.startReadList(fieldPath);\n").indent(1).append("if (list == null) return null;\n");
    builder.indent(1).append("reader.readStartArray();\n");
    builder.indent(1).append("while (reader.readBsonType() != org.bson.BsonType.END_OF_DOCUMENT) {\n");
    String variable = decodeValue(builder, valueClass, 2);
    builder.indent(2).append("list.add({});\n", variable);
    builder.indent(1).append("}\n");
    builder.indent(1).append("reader.readEndArray();\n");
    builder.indent(1).append("return list;\n");
    builder.append('}');
    this.builder.addMethod(builder.build());
    decodeMethods.put(Types.list(valueClass), methodName);
    return methodName;
}
Also used : CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 3 with CodeBuilder

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

the class EntityIdHandlerBuilder method setMethod.

private String setMethod() {
    CodeBuilder builder = new CodeBuilder();
    builder.append("public void set(Object value, Object id) {\n").indent(1).append("{} entity = ({}) value;\n", type(entityClass), type(entityClass)).indent(1).append("entity.{} = ({}) id;\n", idField.getName(), type(idField.getType())).append("}");
    return builder.build();
}
Also used : CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 4 with CodeBuilder

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

the class WebServiceClientBuilder method buildMethod.

private String buildMethod(Method method) {
    CodeBuilder builder = new CodeBuilder();
    Type returnType = method.getGenericReturnType();
    Map<String, Integer> pathParamIndexes = Maps.newHashMap();
    Type requestBeanType = null;
    Integer requestBeanIndex = null;
    builder.append("public {} {}(", type(returnType), method.getName());
    Annotation[][] annotations = method.getParameterAnnotations();
    Class<?>[] parameterTypes = method.getParameterTypes();
    for (int i = 0; i < parameterTypes.length; i++) {
        Class<?> paramClass = parameterTypes[i];
        if (i > 0)
            builder.append(", ");
        builder.append("{} param{}", type(paramClass), i);
        PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
        if (pathParam != null) {
            pathParamIndexes.put(pathParam.value(), i);
        } else {
            requestBeanIndex = i;
            requestBeanType = method.getGenericParameterTypes()[i];
        }
    }
    builder.append(") {\n");
    builder.indent(1).append("java.lang.reflect.Type requestType = {};\n", requestBeanType == null ? "null" : variable(requestBeanType));
    builder.indent(1).append("Object requestBean = {};\n", requestBeanIndex == null ? "null" : "param" + requestBeanIndex);
    builder.indent(1).append("java.util.Map pathParams = new java.util.HashMap();\n");
    pathParamIndexes.forEach((name, index) -> builder.indent(1).append("pathParams.put({}, param{});\n", variable(name), index));
    String returnTypeLiteral = returnType == void.class ? type(Void.class) : type(returnType);
    String path = method.getDeclaredAnnotation(Path.class).value();
    // to pass path as string literal, the escaped char will not be transferred, like \\, currently not convert is because only type regex may contain special char
    builder.indent(1).append("String serviceURL = client.serviceURL({}, pathParams);\n", variable(path));
    HTTPMethod httpMethod = HTTPMethods.httpMethod(method);
    builder.indent(1).append("{} response = ({}) client.execute({}, serviceURL, requestType, requestBean, {});\n", returnTypeLiteral, returnTypeLiteral, variable(httpMethod), variable(returnType));
    if (returnType != void.class)
        builder.indent(1).append("return response;\n");
    builder.append("}");
    return builder.build();
}
Also used : Path(core.framework.api.web.service.Path) CodeBuilder(core.framework.impl.asm.CodeBuilder) Type(java.lang.reflect.Type) HTTPMethod(core.framework.http.HTTPMethod) PathParam(core.framework.api.web.service.PathParam)

Example 5 with CodeBuilder

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

the class EntitySchemaGenerator method schemeStatements.

private List<String> schemeStatements() {
    List<String> statements = Lists.newArrayList();
    CodeBuilder 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(Length.class)));
        if (primaryKey != null) {
            if (primaryKey.autoIncrement())
                builder.append(" AUTO_INCREMENT");
            if (!Strings.isEmpty(primaryKey.sequence())) {
                statements.add("CREATE SEQUENCE IF NOT EXISTS " + primaryKey.sequence());
            }
            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.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