Search in sources :

Example 1 with CodeBuilder

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

the class EntityDecoderBuilder method decodeListMethod.

private String decodeListMethod(Type listType) {
    String methodName = decodeMethods.get(listType);
    if (methodName != null)
        return methodName;
    Class<?> valueClass = GenericTypes.listValueClass(listType);
    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(listType, methodName);
    return methodName;
}
Also used : CodeBuilder(core.framework.internal.asm.CodeBuilder)

Example 2 with CodeBuilder

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

the class EntityDecoderBuilder method decodeMapMethod.

private String decodeMapMethod(Type mapType) {
    String methodName = decodeMethods.get(mapType);
    if (methodName != null)
        return methodName;
    Class<?> keyClass = GenericTypes.mapKeyClass(mapType);
    Type valueType = GenericTypes.mapValueType(mapType);
    methodName = "decodeMap" + keyClass.getSimpleName() + GenericTypes.rawClass(valueType).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, valueType, 2);
    if (String.class.equals(keyClass)) {
        builder.indent(2).append("map.put(fieldName, {});\n", variable);
    } else if (keyClass.isEnum()) {
        String enumCodecVariable = registerEnumCodec(keyClass);
        builder.indent(2).append("map.put({}.decodeMapKey(fieldName), {});\n", enumCodecVariable, variable);
    } else {
        throw new Error("unknown key class, class=" + keyClass.getCanonicalName());
    }
    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(mapType, methodName);
    return methodName;
}
Also used : Type(java.lang.reflect.Type) CodeBuilder(core.framework.internal.asm.CodeBuilder)

Example 3 with CodeBuilder

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

the class EntityDecoderBuilder method decodeEntityMethod.

private String decodeEntityMethod(Class<?> entityClass) {
    String methodName = decodeMethods.get(entityClass);
    if (methodName != null)
        return methodName;
    methodName = "decode" + entityClass.getSimpleName() + (index++);
    CodeBuilder builder = new CodeBuilder();
    builder.append("public {} {}(org.bson.BsonReader reader, {} wrapper, String parentField) {\n", type(entityClass), methodName, type(BsonReaderWrapper.class)).indent(1).append("boolean hasContent = wrapper.startReadEntity(parentField);\n").indent(1).append("if (!hasContent) return null;\n").indent(1).append("{} entity = new {}();\n", type(entityClass), type(entityClass)).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");
    for (Field field : Classes.instanceFields(entityClass)) {
        builder.indent(2).append("if ({}.equals(fieldName)) {\n", variable(mongoField(field)));
        String variable = decodeValue(builder, field.getGenericType(), 3);
        builder.indent(3).append("entity.{} = {};\n", field.getName(), variable);
        builder.indent(3).append("continue;\n").indent(2).append("}\n");
    }
    builder.indent(2).append("logger.warn({}, fieldPath, reader.getCurrentBsonType());\n", variable("undefined field, field={}, type={}"));
    builder.indent(2).append("reader.skipValue();\n");
    builder.indent(1).append("}\n");
    builder.indent(1).append("reader.readEndDocument();\n");
    builder.indent(1).append("return entity;\n");
    builder.append('}');
    this.builder.addMethod(builder.build());
    decodeMethods.put(entityClass, methodName);
    return methodName;
}
Also used : Field(java.lang.reflect.Field) CodeBuilder(core.framework.internal.asm.CodeBuilder)

Example 4 with CodeBuilder

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

the class EntityEncoderBuilder method encodeListMethod.

private String encodeListMethod(Type listFieldType) {
    Class<?> valueClass = GenericTypes.listValueClass(listFieldType);
    String methodName = encodeMethods.get(listFieldType);
    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(listFieldType, methodName);
    return methodName;
}
Also used : CodeBuilder(core.framework.internal.asm.CodeBuilder)

Example 5 with CodeBuilder

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

the class EntityEncoderBuilder method encodeMapMethod.

private String encodeMapMethod(Type mapFieldType) {
    String methodName = encodeMethods.get(mapFieldType);
    if (methodName != null)
        return methodName;
    Class<?> keyClass = GenericTypes.mapKeyClass(mapFieldType);
    Type valueType = GenericTypes.mapValueType(mapFieldType);
    Class<?> valueClass = GenericTypes.rawClass(valueType);
    methodName = "encodeMap" + keyClass.getSimpleName() + valueClass.getSimpleName() + (index++);
    var 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");
    if (String.class.equals(keyClass)) {
        builder.indent(2).append("String key = (String) entry.getKey();\n");
    } else if (keyClass.isEnum()) {
        String enumCodecVariable = registerEnumCodec(keyClass);
        builder.indent(2).append("String key = {}.encodeMapKey(({}) entry.getKey());\n", enumCodecVariable, type(keyClass));
    } else {
        throw new Error("unknown key class, class=" + keyClass.getCanonicalName());
    }
    builder.indent(2).append("{} value = ({}) entry.getValue();\n", type(valueClass), type(valueClass)).indent(2).append("writer.writeName(key);\n");
    encodeField(builder, "value", valueType, 2);
    builder.indent(1).append("}\n").indent(1).append("writer.writeEndDocument();\n").append('}');
    this.builder.addMethod(builder.build());
    encodeMethods.put(mapFieldType, methodName);
    return methodName;
}
Also used : Type(java.lang.reflect.Type) 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