Search in sources :

Example 11 with CodeBuilder

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

the class EntityDecoderBuilder method build.

public EntityDecoder<T> build() {
    builder.addField("private final {} logger = {}.getLogger({});", type(Logger.class), type(LoggerFactory.class), variable(EntityDecoder.class));
    String methodName = decodeEntityMethod(entityClass);
    CodeBuilder builder = new CodeBuilder().append("public Object decode(org.bson.BsonReader reader) {\n").indent(1).append("{} wrapper = new {}(reader);\n", type(BsonReaderWrapper.class), type(BsonReaderWrapper.class)).indent(1).append("return {}(reader, wrapper, {});\n", methodName, variable("")).append("}");
    this.builder.addMethod(builder.build());
    return this.builder.build();
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 12 with CodeBuilder

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

the class EntityEncoderBuilder method build.

public EntityEncoder<T> build() {
    String methodName = encodeEntityMethod(entityClass);
    CodeBuilder builder = new CodeBuilder();
    builder.append("public void encode(org.bson.BsonWriter writer, Object entity) {\n").indent(1).append("{} wrapper = new {}(writer);\n", type(BsonWriterWrapper.class), type(BsonWriterWrapper.class)).indent(1).append("{}(writer, wrapper, ({}) entity);\n", methodName, entityClass.getCanonicalName()).append("}");
    this.builder.addMethod(builder.build());
    return this.builder.build();
}
Also used : CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 13 with CodeBuilder

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

the class EntityEncoderBuilder method encodeEntityMethod.

private String encodeEntityMethod(Class<?> entityClass) {
    String methodName = encodeMethods.get(entityClass);
    if (methodName != null)
        return methodName;
    methodName = "encode" + entityClass.getSimpleName() + (index++);
    CodeBuilder builder = new CodeBuilder();
    builder.append("private void {}(org.bson.BsonWriter writer, {} wrapper, {} entity) {\n", methodName, type(BsonWriterWrapper.class), type(entityClass));
    builder.indent(1).append("writer.writeStartDocument();\n");
    for (Field field : Classes.instanceFields(entityClass)) {
        String fieldVariable = "entity." + field.getName();
        builder.indent(1).append("writer.writeName({});\n", variable(mongoField(field)));
        encodeField(builder, fieldVariable, field.getGenericType(), 1);
    }
    builder.indent(1).append("writer.writeEndDocument();\n").append('}');
    this.builder.addMethod(builder.build());
    encodeMethods.put(entityClass, methodName);
    return methodName;
}
Also used : Field(java.lang.reflect.Field) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 14 with CodeBuilder

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

the class EntityEncoderBuilder method encodeListMethod.

private String encodeListMethod(Class<?> valueClass) {
    String methodName = encodeMethods.get(Types.list(valueClass));
    if (methodName != null)
        return methodName;
    methodName = "encodeList" + valueClass.getSimpleName() + (index++);
    CodeBuilder builder = new CodeBuilder();
    builder.append("private void {}(org.bson.BsonWriter writer, {} wrapper, java.util.List list) {\n", methodName, type(BsonWriterWrapper.class));
    builder.indent(1).append("writer.writeStartArray();\n").indent(1).append("for (java.util.Iterator iterator = list.iterator(); iterator.hasNext(); ) {\n").indent(2).append("{} value = ({}) iterator.next();\n", type(valueClass), type(valueClass));
    encodeField(builder, "value", valueClass, 2);
    builder.indent(1).append("}\n").indent(1).append("writer.writeEndArray();\n").append('}');
    this.builder.addMethod(builder.build());
    encodeMethods.put(Types.list(valueClass), methodName);
    return methodName;
}
Also used : CodeBuilder(core.framework.impl.asm.CodeBuilder)

Example 15 with CodeBuilder

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

the class EntityEncoderBuilder method encodeMapMethod.

private String encodeMapMethod(Class<?> valueClass) {
    String methodName = encodeMethods.get(Types.map(String.class, valueClass));
    if (methodName != null)
        return methodName;
    methodName = "encodeMap" + valueClass.getSimpleName() + (index++);
    CodeBuilder builder = new CodeBuilder();
    builder.append("private void {}(org.bson.BsonWriter writer, {} wrapper, java.util.Map map) {\n", methodName, type(BsonWriterWrapper.class));
    builder.indent(1).append("writer.writeStartDocument();\n").indent(1).append("for (java.util.Iterator iterator = map.entrySet().iterator(); iterator.hasNext(); ) {\n").indent(2).append("java.util.Map.Entry entry = (java.util.Map.Entry) iterator.next();\n").indent(2).append("String key = (String) entry.getKey();\n").indent(2).append("{} value = ({}) entry.getValue();\n", type(valueClass), type(valueClass)).indent(2).append("writer.writeName(key);\n");
    encodeField(builder, "value", valueClass, 2);
    builder.indent(1).append("}\n").indent(1).append("writer.writeEndDocument();\n").append('}');
    this.builder.addMethod(builder.build());
    encodeMethods.put(Types.map(String.class, valueClass), methodName);
    return methodName;
}
Also used : 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