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();
}
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();
}
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;
}
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;
}
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;
}
Aggregations