Search in sources :

Example 11 with BindTransform

use of com.abubusoft.kripton.processor.bind.transform.BindTransform in project kripton by xcesco.

the class BindTypeBuilder method generateSerializeOnXml.

private static void generateSerializeOnXml(BindTypeContext context, BindEntity entity) {
    // @formatter:off
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("serializeOnXml").addJavadoc("method for xml serialization\n").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(typeName(entity.getElement()), "object").addParameter(typeName(XMLSerializer.class), "xmlSerializer").addParameter(typeName(Integer.TYPE), "currentEventType").returns(Void.TYPE).addException(Exception.class);
    // @formatter:on
    methodBuilder.beginControlFlow("if (currentEventType == 0)");
    methodBuilder.addStatement("xmlSerializer.writeStartElement(\"$L\")", entity.xmlInfo.label);
    methodBuilder.endControlFlow();
    BindTransform bindTransform;
    methodBuilder.addCode("\n");
    // attributes
    methodBuilder.addCode("// Persisted fields:\n\n");
    for (BindProperty item : entity.getCollection()) {
        bindTransform = BindTransformer.lookup(item);
        methodBuilder.addCode("// field $L (mapped with $S)\n", item.getName(), item.label);
        if (item.hasTypeAdapter()) {
            methodBuilder.addCode("// field trasformation $L $L \n", item.typeAdapter.dataType, item.typeAdapter.adapterClazz);
        }
        bindTransform.generateSerializeOnXml(context, methodBuilder, "xmlSerializer", item.getPropertyType().getTypeName(), "object", item);
        methodBuilder.addCode("\n");
    }
    methodBuilder.beginControlFlow("if (currentEventType == 0)");
    methodBuilder.addStatement("xmlSerializer.writeEndElement()");
    methodBuilder.endControlFlow();
    // methodBuilder.nextControlFlow("catch($T e)",
    // typeName(Exception.class));
    // methodBuilder.addStatement("e.printStackTrace()");
    // methodBuilder.addStatement("throw (new $T(e))",
    // typeName(KriptonRuntimeException.class));
    // methodBuilder.endControlFlow();
    context.builder.addMethod(methodBuilder.build());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform) BindProperty(com.abubusoft.kripton.processor.bind.model.BindProperty)

Example 12 with BindTransform

use of com.abubusoft.kripton.processor.bind.transform.BindTransform in project kripton by xcesco.

the class ManagedPropertyPersistenceHelper method generateFieldSerialize.

/**
 * generates code to manage field serialization
 *
 * @param context
 * @param persistType
 * @param property
 * @param modifiers
 */
public static void generateFieldSerialize(BindTypeContext context, PersistType persistType, BindProperty property, Modifier... modifiers) {
    Converter<String, String> format = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL);
    String methodName = "serialize" + format.convert(property.getName());
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName).addJavadoc("for attribute $L serialization\n", property.getName()).addParameter(ParameterSpec.builder(typeName(property.getElement()), "value").build()).addModifiers(modifiers);
    switch(persistType) {
        case STRING:
            methodBuilder.returns(className(String.class));
            break;
        case BYTE:
            methodBuilder.returns(TypeUtility.arrayTypeName(Byte.TYPE));
            break;
    }
    // if property type is byte[], return directly the value
    if (ArrayTypeName.of(Byte.TYPE).equals(property.getPropertyType().getTypeName()) && persistType == PersistType.BYTE) {
        methodBuilder.addStatement("return value");
    } else {
        methodBuilder.beginControlFlow("if (value==null)");
        methodBuilder.addStatement("return null");
        methodBuilder.endControlFlow();
        methodBuilder.addStatement("$T context=$T.jsonBind()", KriptonJsonContext.class, KriptonBinder.class);
        methodBuilder.beginControlFlow("try ($T stream=new $T(); $T wrapper=context.createSerializer(stream))", KriptonByteArrayOutputStream.class, KriptonByteArrayOutputStream.class, JacksonWrapperSerializer.class);
        methodBuilder.addStatement("$T jacksonSerializer=wrapper.jacksonGenerator", JsonGenerator.class);
        if (!property.isBindedObject()) {
            methodBuilder.addStatement("jacksonSerializer.writeStartObject()");
        }
        methodBuilder.addStatement("int fieldCount=0");
        BindTransform bindTransform = BindTransformer.lookup(property);
        String serializerName = "jacksonSerializer";
        bindTransform.generateSerializeOnJackson(context, methodBuilder, serializerName, null, "value", property);
        if (!property.isBindedObject()) {
            methodBuilder.addStatement("jacksonSerializer.writeEndObject()");
        }
        methodBuilder.addStatement("jacksonSerializer.flush()");
        switch(persistType) {
            case STRING:
                methodBuilder.addStatement("return stream.toString()");
                break;
            case BYTE:
                methodBuilder.addStatement("return stream.toByteArray()");
                break;
        }
        methodBuilder.nextControlFlow("catch($T e)", Exception.class);
        methodBuilder.addStatement("throw(new $T(e.getMessage()))", KriptonRuntimeException.class);
        methodBuilder.endControlFlow();
    }
    context.builder.addMethod(methodBuilder.build());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform)

Example 13 with BindTransform

use of com.abubusoft.kripton.processor.bind.transform.BindTransform in project kripton by xcesco.

the class ManagedPropertyPersistenceHelper method generateFieldParser.

/**
 * generates code to manage field parsing
 *
 * @param context
 * @param persistType
 * @param property
 * @param modifiers
 */
public static void generateFieldParser(BindTypeContext context, PersistType persistType, BindProperty property, Modifier... modifiers) {
    Converter<String, String> format = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL);
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("parse" + format.convert(property.getName())).addJavadoc("for attribute $L parsing\n", property.getName()).returns(typeName(property.getElement()));
    methodBuilder.addModifiers(modifiers);
    switch(persistType) {
        case STRING:
            methodBuilder.addParameter(ParameterSpec.builder(className(String.class), "input").build());
            break;
        case BYTE:
            methodBuilder.addParameter(ParameterSpec.builder(TypeUtility.arrayTypeName(Byte.TYPE), "input").build());
            break;
    }
    // if property type is byte[], return directly the value
    if (ArrayTypeName.of(Byte.TYPE).equals(property.getPropertyType().getTypeName()) && persistType == PersistType.BYTE) {
        methodBuilder.addStatement("return input");
    } else {
        methodBuilder.beginControlFlow("if (input==null)");
        methodBuilder.addStatement("return null");
        methodBuilder.endControlFlow();
        methodBuilder.addStatement("$T context=$T.jsonBind()", KriptonJsonContext.class, KriptonBinder.class);
        methodBuilder.beginControlFlow("try ($T wrapper=context.createParser(input))", JacksonWrapperParser.class);
        methodBuilder.addStatement("$T jacksonParser=wrapper.jacksonParser", JsonParser.class);
        methodBuilder.addCode("// START_OBJECT\n");
        methodBuilder.addStatement("jacksonParser.nextToken()");
        if (!property.isBindedObject()) {
            methodBuilder.addCode("// value of \"element\"\n");
            methodBuilder.addStatement("jacksonParser.nextValue()");
        }
        String parserName = "jacksonParser";
        BindTransform bindTransform = BindTransformer.lookup(property);
        methodBuilder.addStatement("$T result=null", property.getPropertyType().getTypeName());
        bindTransform.generateParseOnJackson(context, methodBuilder, parserName, null, "result", property);
        methodBuilder.addStatement("return result");
        methodBuilder.nextControlFlow("catch($T e)", Exception.class);
        methodBuilder.addStatement("throw(new $T(e.getMessage()))", KriptonRuntimeException.class);
        methodBuilder.endControlFlow();
    }
    context.builder.addMethod(methodBuilder.build());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) BindTransform(com.abubusoft.kripton.processor.bind.transform.BindTransform)

Aggregations

BindTransform (com.abubusoft.kripton.processor.bind.transform.BindTransform)13 BindProperty (com.abubusoft.kripton.processor.bind.model.BindProperty)11 MethodSpec (com.squareup.javapoet.MethodSpec)9 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonParser (com.fasterxml.jackson.core.JsonParser)2 Bind (com.abubusoft.kripton.annotation.Bind)1 BindAdapter (com.abubusoft.kripton.annotation.BindAdapter)1 BindDisabled (com.abubusoft.kripton.annotation.BindDisabled)1 BindType (com.abubusoft.kripton.annotation.BindType)1 BindXml (com.abubusoft.kripton.annotation.BindXml)1 BindEntity (com.abubusoft.kripton.processor.bind.model.BindEntity)1 ByteArrayBindTransform (com.abubusoft.kripton.processor.bind.transform.ByteArrayBindTransform)1 ObjectBindTransform (com.abubusoft.kripton.processor.bind.transform.ObjectBindTransform)1 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)1 IncompatibleAnnotationException (com.abubusoft.kripton.processor.exceptions.IncompatibleAnnotationException)1 IncompatibleAttributesInAnnotationException (com.abubusoft.kripton.processor.exceptions.IncompatibleAttributesInAnnotationException)1 InvalidDefinition (com.abubusoft.kripton.processor.exceptions.InvalidDefinition)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1 Elements (javax.lang.model.util.Elements)1